Results 1 to 4 of 4

Thread: Multiple CustomCollectionEditors?!

  1. #1

    Default Multiple CustomCollectionEditors?!

    Hi everyone,

    I'm currently developing a small Project/ChangeManagement Tool for sure with Spring helping me in many places.

    I've now a bean with multiple properties and two Sets of other beans:

    Code:
    public class ProjectBean extends EntityBean{
      private String name;
      private String description;
      private UserBean coordinator;
      private Set<UserBean> teamMembers;
      private Set<EnvironmentBean> environments;
      ... some more properties ...
      accessors
    }
    To create/edit projects I've a form using Spring MVC and Springs form taglibrary (I love it...together with JSTL it's really great). This form contains to select boxes for the teamMembers and environments:

    Code:
    snip
    		  <tr>
    		    <td><B>Environments:</B><br></td>
    			<td>
    				<form:select path="environments" multiple="true">
    				 	<form:options items="${environments}" itemValue="id" itemLabel="name"/>
    				</form:select>
    				<span class="error-red"><form:errors path="environments"/></span>
    		    </td>
    		  </tr>
    		  <tr>
    		    <td><B>Team Members:</B><br></td>
    			<td>
    				<form:select path="teamMembers" multiple="true">
    				 	<form:options items="${users}" itemValue="id" itemLabel="fullName"/>
    				</form:select>
    				<span class="error-red"><form:errors path="teamMembers"/></span>
    		    </td>
    		  </tr>
    snip
    To convert the Parameters for both sets I need to CustomEditors. I did this for the environments already and it works fine.

    Controller:
    Code:
    snip
    	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){
    		binder.registerCustomEditor(UserBean.class, new UserPropertyEditor(userManager));
    		binder.registerCustomEditor(EnvironmentBean.class, new EnvironmentPropertyEditor(environmentManager));
    		binder.registerCustomEditor(Set.class, new EnvironmentCollectionPropertyEditor(Set.class, environmentManager));
    		binder.registerCustomEditor(Date.class,new CustomDateEditor(
    				new SimpleDateFormat("dd/MM/yyyy"), true));
    	}
    snip
    Editor:
    Code:
    public class EnvironmentCollectionPropertyEditor extends CustomCollectionEditor {
    	private EnvironmentManager environmentManager;
    	
    	/**
    	 * @param collectionType
    	 */
    	public EnvironmentCollectionPropertyEditor(Class collectionType, EnvironmentManager environmentManager) {
    		super(collectionType);
    		this.environmentManager = environmentManager;
    	}
    	
    	protected Object convertElement(Object element){
    		if(element != null) {
    			Integer id = new Integer((String)element);
    			return environmentManager.getEnvironmentById(id);
    		}
    		return null;
    	}
    
    }
    But how can I have two editors for Set.class? One has to take care for the environments as shown above, and the other should take care for the teamMembers? But there's no way to separate the binder Objects? Is there?

    I tried to find something here and in the docs (both helped me to get my PropertyEditors to work), but found nothing which suits to my issue.

    Cheers,
    Thomas

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Well how about specifing for which property they are? Have you taken a look at the javadocs of the DataBinder (the super class of the ServletRequestDataBinder)?

    There are 2 registerCustomEditor methods here and here. You are using the first one, the one which only takes a class and a PropertyEditor as parameters. The other one also takes string as a parameter, this string holds the fieldname for which it applies!

    Code:
    binder.registerCustomEditor(Set.class, "environments",new EnvironmentCollectionPropertyEditor(Set.class, environmentManager));
    binder.registerCustomEditor(Set.class, "teamMembers",new TeamMembersCollectionPropertyEditor(Set.class, teamMemberManager));
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    *blushes*

    Marten, thats simply it. I did not have a look at the JavaDocs. Stupid me...I don't know why. Works like a charm now. Thanks a lot!
    Last edited by ThomasBecker; Aug 15th, 2007 at 08:35 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Your welcome, glad that it worked.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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