Announcement Announcement Module
No announcement yet.
Spring MVC Binding problems Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC Binding problems


    I have a problem with data conversion between page and controller.

    Step 1
    Let's assume we have sample from your guide (paragraph

    Preferences {
    String[] interests

    and on the jsp I wrote form:checkbox path="interests"
    It worked fine - the framework added or removed strings to or from the array

    I used custom object instead of strings

    Preferences {
    Interest[] interests;

    String name
    //constructor here

    then I wrote InterestEditor and registered it in method annotated as InitBinder, you know...

    It still worked fine

    Step 3
    I changed Interests array into Interests list

    List interests

    and it stopped working. After submitting the form, strings are being added to the list. But I expected Interest objects. What did I do wrong?

    Thanks for advice,

  • #2
    Can you post the relevant code?


    • #3
      The model objects are:
      public class Employee {

      private List interests = new ArrayList();

      public List getInterests() {

      return interests;

      public void setInterests( List interests ) {

      this.interests = interests;

      public class Interest {

      private String name;

      public Interest( String name ) { = name;

      public String toString() {
      return name;

      PropertyEditor is:
      public class InterestEditor extends PropertyEditorSupport {

      public void setAsText( String text ) throws IllegalArgumentException {
      setValue( new Interest( text ) );

      The controller part is:
      @RequestMapping( "/" )
      public class MyFormController2 {

      @RequestMapping( method = RequestMethod.POST )
      public String processForm( @ModelAttribute Employee employee, Errors errors ) {

      System.out.println( "Iterest is class of" + employee.getInterests().get(0).getClass().getName( ) );

      return "home";

      public void initBinder( WebDataBinder binder ) {
      binder.registerCustomEditor( Interest.class, new InterestEditor() );


      @RequestMapping( method = RequestMethod.GET )
      public String setupForm( Model model ) {
      model.addAttribute( new Employee() );
      return "form";


      The form.jsp code is:
      <form:form modelAttribute="employee">
      <form:input path="name"/>
      dancing: <form:checkbox path="interests" value="dancing" />
      traveling: <form:checkbox path="interests" value="travelling" />
      swimming: <form:checkbox path="interests" value="swimming" />

      <input type="submit" value="Submit" />

      So now try to invoke / URL, then chceck some checkboxes and submit. Notice what the controller will log on the sysout.



      • #4
        anybody knows?


        • #5
          Why would you think this would work? Spring has no way of knowing that the List is supposed to contain Interest objects, so it's not going to bother invoking your custom property editor.

          Contrast this to when you had an Interest[] array, where Spring has the runtime type information available to know which PropertyEditor to invoke.

          Have a look at CustomCollectionEditor if you need to do something like what you're describing.


          • #6
            Thanks, you are right!


            • #7
              Override equals() for proper data binding

              Also, since it's a custom object, don't forget to override the equals() method! Otherwise if you submit to the same page, the checkboxes won't stay checked.

              I couldn't figure this one out for hours (I'm slow and new to Spring MVC), and I'm so glad I stumbled across this thread, as it sparked the idea. You guys are awesome.


              • #8
                binding problem

                Right...I had the exactly same problem when I tried to implement it in my dynamic website writing a business plan . After I read the posts in this forum I have succeeded to solve the problem.
                Good luck there!