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

  • Spring MVC Binding problems

    Hello,

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

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

    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

    Step2
    I used custom object instead of strings

    Preferences {
    Interest[] interests;
    }

    Interest{
    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

    Preferences{
    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,
    mbartyzel

  • #2
    Can you post the relevant code?

    Comment


    • #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 ) {
      this.name = name;
      }

      public String toString() {
      return name;
      }
      }

      PropertyEditor is:
      public class InterestEditor extends PropertyEditorSupport {

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


      The controller part is:
      @Controller
      @RequestMapping( "/processForm.do" )
      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";
      }

      @InitBinder
      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" />
      </form:form>

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

      Thanks,
      mb

      Comment


      • #4
        anybody knows?

        Comment


        • #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.

          Comment


          • #6
            Thanks, you are right!

            Comment


            • #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.

              Comment


              • #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!
                Best
                Ben

                Comment

                Working...
                X