Converting from String to custom Object for Spring MVC form Data binding?

后端 未结 5 1140
萌比男神i
萌比男神i 2020-11-29 01:19

I am using Spring MVC\'s SimpleFormController in conjunction with Spring MVC\'s form JTL to create a form to edit a Generic object.

On my form I have a drop down whe

相关标签:
5条回答
  • 2020-11-29 01:38

    The annotation version of the answer, in your controller:

    @org.springframework.web.bind.annotation.InitBinder("yourFormName")
    protected void initBinder(
                 org.springframework.web.bind.WebDataBinder binder) {
        binder.registerCustomEditor(Server.class, "serverId", new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Server s = ...; // do whatever needed to convert
                setValue(s);
            }
        });
    
    0 讨论(0)
  • 2020-11-29 01:42

    Using "server.id" can be a possible solution. So spring automatically binds selected value or vice versa.

    <form:form commandName="generic">
        <form:select path="server.id">
            <form:options items="${servers}" itemValue="id" itemLabel="name"/>
        </form:select>
    </form:form>
    
    0 讨论(0)
  • 2020-11-29 01:43

    I think you are correct. You need to register a custom editor on the binder like you done before so you can convert a String to a Server instance. There is an example of a custom PropertyEditor here in the Spring reference docs that should get you started.

    I agree that the MVC documentation is not the best. I have had to do a lot of Googling and searching on the Spring forums.

    0 讨论(0)
  • 2020-11-29 01:46

    Just as a supplement to Mark's answer, here is what I ended up doing in my controller.

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Server.class, "serverId", new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Server type = (Server) em.createNamedQuery("Server.findById")
                    .setParameter("id", Short.parseShort(text)).getSingleResult();
                setValue(type);
            }
        });
    }
    

    You can also do this using Spring injection, as opposed to anonymous classes. This outlined by the link in Mark's answer.

    You you may also be able to extend ClassEditor (see below) instead of PropertyEditorSupport. The Javadoc states;

    Property editor for java.lang.Class, to enable the direct population of a Class property without recourse to having to use a String class name property as bridge.

    Don't know if I fully understand the benefit of this, but something to keep in mind.

    Useful Javadocs

    • Property Editor Support
    • Spring Data Binder
    • Spring Property Editors
    • Spring Class Editor
    0 讨论(0)
  • 2020-11-29 01:55

    Another example:

    http://empire5.com/development/binding-a-custom-object-in-spring-3/

    0 讨论(0)
提交回复
热议问题