【问题标题】:Spring MVC : Mapping Form input to complex backing object containing HashMapSpring MVC:将表单输入映射到包含 HashMap 的复杂支持对象
【发布时间】:2015-06-11 23:05:59
【问题描述】:

我有一个场景,其中来自 jsp 表单的输入字段列表将映射到支持对象 Resolution 中的 HashMap 字段。

    @Controller
    @RequestMapping("/rms") 
    class ResolutionManagementController{

        private static final String ISSUE_FORM_PATH="/view/resolutionForm";
        private static final String SHOW_RESOLUTION_PATH="/view/showResolution";

        @Autowired
        IIssueManagementService issueService;

        @Autowired
        ResolutionManagementService resolutionService;

        @RequestMapping(value="/resolution/form",method=RequestMethod.GET)
        String resolutionForm(Model model){
                List<Issue> issues = issueService.listIssue();
                model.addAttribute("issues",issues);
                model.addAttribute("resolution", new Resolution());
                return ISSUE_FORM_PATH;     
        }

        @RequestMapping(value="/resolution",method=RequestMethod.POST)
        String resolve(Resolution resolution,Model model){
            List<SupportExecutive> executives = resolutionService.addResolution(resolution);
            model.addAttribute("executives",executives);
            return SHOW_RESOLUTION_PATH;
        }   

    }

支持表单对象

class Resolution{
        private String resolutionId;
        private String categoryId;
        private Map<Issue,SupportExecutive> allotments;

        //getters and setters

    } 

resolutionForm.jsp

<form:form action="/rms/resolution" commandName="resolution">

        <c:forEach var="issue" items="${issues}">
            <tr>
                <td>
                    ${issue.issueTitle}
                </td>
                <td>
<!-- What should i do so that issueTitle is converted
to Issue Object and stored as map key in allotments Map in resolution 
object and the value entered by the user is converted to SupportExecutive 
object and stored as corresponding value object of the allotments map-->
                    <input type="text" name="${issue.issueTitle}>"
                </td>
            </tr>
        </c:forEach>


    </form:form>

我试图查看属性编辑器,但我无法理解如何使用属性编辑器获取键和值并存储在地图中。或者他们可能不适合这里。

实现这一目标的最干净的方法是什么?如果能提供涉及步骤的详细说明就好了。

【问题讨论】:

  • 为什么不使用更合适的对象(例如带有问题数据和分配列表的 DTO)。避免jsp“代码”总是更好
  • 你能解释一下,你希望我如何在这里使用 DTO。它如何帮助简化将表单数据绑定到分配地图?
  • 我明白你想说什么。我个人避免使用 DTO,因为它们不是域模型的一部分。在请求处理资源(域对象,例如:add/student、update/student、add/employee、update/employee)时,我尝试尽可能遵循 Rest 原则,但我们从不添加/dto 或 update/ dto。那是一种复杂的事情。更多的是个人选择。

标签: java spring jsp spring-mvc


【解决方案1】:

这里概述了为您的域类连接属性编辑器的一种方法:

package your.controller.package;

import org.springframework.web.bind.annotation.InitBinder;
import java.beans.PropertyEditorSupport;
...
public class YourController {
    ...
    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Resolution.class, new ResolutionFormEditor());
    }

    private static class ResolutionFormEditor extends PropertyEditorSupport {
        // convert a Resolution object to a string type
        @Override public String getAsText() {
            Resolution r = (Resolution) this.getValue();
            return r != null ? r.resolutionId() : "";
        }  

        // convert string representation to Resolution object
        @Override public void setAsText(String text) {
            Resolution r = resolutionService.findById(text);
            this.setValue(r);
        }
    }
    ...
} 

Spring property editor documentation 有很好的描述。

编辑:

抱歉,我忘记了您问题的地图部分。看一下this forum post的例子代码。

【讨论】:

  • 在您的示例中,接收到的表单中的输入参数在哪里。 的 name 属性必须作为 key,用户输入的 value 必须作为 value。在 UI 中会有一个表格形式的问题标题列表,用户只需针对每个问题输入主管的姓名。现在问题标题成为分配映射的键,输入的执行者的名称成为映射的值。但键和值必须转换为对应的对象,如 Issue 和 SupportExecutive 对象。
  • 说如果我得到 HttpServletRequest.getParameterMap() 我可以提取参数名称(比如 issuetitle)并将其转换为 Issue iss = new Issue(); iss.setTitle(问题标题)。这成为解决对象中分配映射的关键。同样,他们收到的针对 issueTitle 的值(比如 executionName)可以转换为 SupportExecutive execution = new SupportExecutive(), execution.setName(executiveName), execution 是分配映射的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多