java - Spring MVC : Mapping Form input to complex backing object containing HashMap

标签 java spring jsp spring-mvc

我有一个场景,其中来自 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>

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

实现此目的最简洁的方法是什么?如果能提供详细的步骤说明就好了。

最佳答案

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

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有很好的描述。

编辑:

抱歉,我忘记了您问题中的 map 部分。看看this forum post例如代码。

关于java - Spring MVC : Mapping Form input to complex backing object containing HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490108/

相关文章:

java - 项目欧拉数 3 : Largest prime factor

java - 如何使用工厂子类中的子类来覆盖父类(super class)工厂中的抽象类?

spring 3.1 - netbeans - 部署失败

java - JSP 错误,在 Eclipse Juno 中禁用

java - JSP 应用程序的浏览器插件或调试面板

java - spring security 在重定向到 logout.jsp 时给出错误

java - Spring Data Rest 存储库中的自定义方法会生成什么 url?

Java - 如何简单地分配大量颜色?

java - 如何在spring中获取访问配置文件的路径

java - 突然我的 spring.xml 不会实例化我的类并且发生错误异常