Announcement Announcement Module
Collapse
No announcement yet.
CustomCollectionEditor returns nested collections from form:select component Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CustomCollectionEditor returns nested collections from form:select component

    Hi guys,

    I was trying to do a very simple user form editing page, but had trouble to submit multiple selected values in form:select.

    Basically, the User entity has a list of Interest entity with getter/setter.

    User.java
    Code:
    private List<Interest> interests = new LinkedList<Interest>();
    The JSP page has a select component bound to the user's interests.

    form.jsp
    Code:
    <form:select path="interests" multiple="true" items="${interestList}"
    					itemLabel="name" itemValue="id"/>
    The ${interestList} is correctly populated in the controller. When I visit this page, the existing interests are correctly selected.

    Somehow the submission doesn't go well.
    I have a CustomeCollectionEditor in the Controller:

    UserController.java
    Code:
    	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    	binder.registerCustomEditor(List.class, "interests",
    		new CustomCollectionEditor(List.class) {
    			@Override
    			protected Object convertElement(Object object) {
    				if (object != null && object instanceof String) {
    					String idString = object.toString();
    					int id = Integer.parseInt(idString);
    					return getInterestById(id);
    				}
    
    				return object;
    			}
    		});
    }
    Inside the convertElement, the correct Interest object is returned.

    The problem is, when I validate the User object after the form submission, the submitted interests is a two-level List. Each single interest is wrapped in an ArrayList and then added to User.interests.

    The debug print out is like this:

    [[model.Interest@294ff81a], [model.Interest@da99836]]

    I went deeper into the Spring MVC and data conversion code, and it seems that it tries to convert each Interest object into List<Interest>.

    I've been trying to debug this for two weeks and seems all the solutions from internet doesn't work either. I start to suspect this might be a bug with the new conversion service.

    Thanks a lot!

  • #2
    Experiencing the same behavoiur with CustomCollectionEditor with Spring 3.0.6.

    Comment


    • #3
      Originally posted by acisser View Post
      Experiencing the same behavoiur with CustomCollectionEditor with Spring 3.0.6.
      I had the same problem but I figured it out....finally !
      The idea is this:
      The only problem is that you need to tell Spring how to convert a String element (which is coming on form submit) into your custom object, in your case Interest object. After this is done, then Spring knows automatically how to create a List<Interest> list!

      So this is the only implementation you need to do in your controller (no CustomCollectionEditor needed !):

      Code:
      @InitBinder
         protected void initBinder(WebDataBinder binder) {
              binder.registerCustomEditor(Interest.class, "interests", new PropertyEditorSupport() {
                 
                  @Override
                  public void setAsText(String text) throws IllegalArgumentException {
                      Interest interest = createAnInterestFromText(text); //TODO
                      setValue(interest);
                  }
                  
              });
          }

      Comment

      Working...
      X