Announcement Announcement Module
No announcement yet.
Spring MVC - Command Object that contains POJO Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC - Command Object that contains POJO

    Is command objects that contain pojos supported in Spring MVC.

    I am using a simpleFormController and a JSP that has a command object that contains an inner POJO.

    When I submit my form I noticed that all simple type attributes are properly set. But my inner POJO is being set to null. What is the process for getting spring to set the inner pojo with correct values.

    When I load the page initially spring has no problem getting values from the inner pojo.

  • #2
    So when the page is loaded the fields get the correct attributes from the inner pojo but when you submit the form the changes arnt in the inner pojo?

    Is the whole pojo null or just the attributes?

    How are you mapping the fields in the form?

    are you using the <form:input path="addressLine1"/> components or regular html <input> components?


    • #3
      Yes the POJO is being set to NULL. In the constructor for the command object I initialize the POJO to an empty object. I put print statements inside of the setPOJO(POJO mypojo) method and realized someone is calling set NULL.

      I've been able to get past this problem now. In the setPOJO method I added if(mypojo != null) { this.mypojo = mypojo }. My guess is that spring is setting all the values in the command object and doesnt have a value for the POJO so passing null. Then because the POJO is null spring cant call Command.getPOJO().setField(xxxxx).

      I do have another question since you mentioned the actual form element on the JSP>

      What is the difference between this:
      <form:input path="command.pojo.field1"/>

      and this:
      <spring:bind path="command.pojo.field1">
      <input type="text" name='<c:out value="${status.expression}"/>' value='<c:out value="${status.value}"/>'>
      <span class="error"><c:out value="${status.errorMessage}"/></span>

      I haven't found a good tutorial on which style to use and when.


      • #4
        Use form tags when you can, if those don't cut it use the bind tags. The form tags are a lot easier to work with. (instead of 3 lines you write 1).

        If your POJO would be null during binding you would get an exception (NullValueInPathException). So I suspect something wrong in your setup or jsp.