Announcement Announcement Module
Collapse
No announcement yet.
Data binding not working on form submit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data binding not working on form submit

    I am using Spring 3.0.

    My data model looks something like this...

    Code:
    public class Report {
    
      ... standard bean properties ...
    
     private User user;
    
     ... typical getter setter ...
    
    }
    
    public class User {
      ... omitted for clarity
    }
    Basically a user owns a report. When a report is created the current user is bound to the report. When attempting to edit the report when the user submits the form, the user object on the report gets set to null and of course hibernate throws a fit because that object cannot be null.

    Here is how I have the controller setup

    Code:
    @Controller
    @RequestMapping("/report")
    public class ReportController {
    
        @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
        public String editReport(@PathVariable Long id, Model model) {
            Report r = backend.getReport(id); // fully loads object
    
            model.addAttribute("report", report);
            return "report/edit";
        }
    
        @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
        public String process(@ModelAttribute("report") Report r) {
            backend.save(r);
            return "redirect:/report/show" + r.getId();
        }
    }
    When I run through things in my debugger, the model object after I addAttribute has the fully loaded object in the mapping. And in the form jsp page, I can display the report.user.username etc. So the user object gets passed there. But in the debugger on the process method, the user object is null on the passed in r object.

    I don't allow the user object to be editted and therefore I don't place any input fields for that property. But I wouldn't expect that value would be nulled when binding correct?

    Do I need to do any special data binding for this? I have been searching around but couldn't find anything that really gave me a clear answer.

    What am I doing wrong?
    Last edited by predhme; Mar 17th, 2010, 10:13 AM. Reason: clarification, spring version

  • #2
    If the html form that submits the Report information does not have user elements they wont get submitted to the server and hence wont get bound by spring MVC.

    you need to store the user information as html input type hidden that way they'll be posted back to the server (your form should be submitting a hidden input with name = "user.username" for it to be bound to report.getUser().setUsername()). how ever this approach could open up the possibility of impersonation (client modifies the user information b4 submitting the form). To avoid that you can put the user information in he session and pull it out of there.
    Last edited by amit.kapps; Mar 17th, 2010, 10:47 AM.

    Comment


    • #3
      Originally posted by amit.kapps View Post
      If the html form that submits the Report information does not have user elements they wont get submitted to the server and hence wont get bound by spring MVC.

      you need to store the user information as html input type hidden that way they'll be posted back to the server. how ever this approach could open up the possibility of impersonation (client modifies the user information b4 submitting the form). To avoid that you can put the user information in he session and pull it out of there.
      I omitted a lot of the object information for clarity. However, there are a few system status properties on the report object as well as logs, and other things that the user has no business editing.

      So, basically from what your saying, I need to provide html hidden inputs for all the values and etc? That seems highly inefficient. Is there not a way to merge the changes with the original object?

      Comment


      • #4
        Try exploring @SessionAttributes. I havent used it personally but I believe it should be able to do just that.
        you specify the model attrib that you want bound to the Session. And when you bind back to that model attrib, Spring should retain the model's property values from the step in the previous conversation.

        Comment


        • #5
          Does spring actually re-instantiate an object with the data from the form? Because I noticed my id field and version (inherited from hibernate) etc are all null on the process method. It seems silly that I would have to store all the information in the Session to retain it. Potentially am I setting up my forms/controllers incorrectly? Assuming my controllers are identical to what I posted previously and I have a form as such:

          Code:
          <%@ include file="/main.jsp" %>
          
          <form:form modelAttribute="template" method="post">
              <table>
                  <tr>
                      <th>Title</th>
                      <th>Category</th>
                  </tr>
                  <tr>
                      <td><form:input path="name" size="50"/></td>
                      <td><form:input path="category" size="50"/></td>
                  </tr>
              </table>
          
              <input type="submit" value="Update report"/>
          
          </form:form>
          
          <%@ include file="/footer.jsp" %>
          Which omits a large set of properties. Things like the ID number, version number from hibernate, a String collection of logs, etc. All those things are re-initialized and/or null depending on the model object.
          Last edited by predhme; Mar 17th, 2010, 11:32 AM. Reason: wrapped code in code tag

          Comment


          • #6
            Spring creates a new instance if you don't store the object in the session OR retrieve it again from the database.

            Either store it in the session (@SessionAttributes) or annotate a method with @ModelAttribute instead of your get method.

            Comment


            • #7
              Originally posted by Marten Deinum View Post
              Spring creates a new instance if you don't store the object in the session OR retrieve it again from the database.

              Either store it in the session (@SessionAttributes) or annotate a method with @ModelAttribute instead of your get method.
              Alright, thanks for the information.

              Comment

              Working...
              X