Results 1 to 8 of 8

Thread: Spring MVC Binding problems

  1. #1
    Join Date
    Aug 2008
    Posts
    12

    Angry 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. #2
    Join Date
    Aug 2008
    Location
    St Louis, MO
    Posts
    51

    Default

    Can you post the relevant code?

  3. #3
    Join Date
    Aug 2008
    Posts
    12

    Question

    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

  4. #4
    Join Date
    Aug 2008
    Posts
    12

    Default

    anybody knows?

  5. #5
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    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.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  6. #6
    Join Date
    Aug 2008
    Posts
    12

    Default

    Thanks, you are right!

  7. #7
    Join Date
    Jan 2009
    Posts
    1

    Default 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. #8
    Join Date
    Jan 2009
    Posts
    2

    Default 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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •