Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditor converts single value but not Collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditor converts single value but not Collection

    I'm having some trouble with a converter in a Spring MVC project (version 3.0.6) running on top of Hibernate 3.6.6. I have a custom object "ActionPlan" which has a plan owner and a list of plan users, all of whom are represented by "PlanUser" objects. The relevant portions of the ActionPlan object are as follows:
    Code:
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "plans_users", joinColumns = @JoinColumn(name = "plan_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Set<Planuser> planUsers = new LinkedHashSet<Planuser>();
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "plan_owner_id")
    private Planuser planOwner;
    The Controller for my edit form is loading a converter as follows:
    Code:
    	@InitBinder
    	public void initBinder(WebDataBinder binder) {
    		...
    		binder.registerCustomEditor(PlanUser.class, new PlanUserEditor());
    		...
    	}
    
    
    	private class PlanUserEditor extends PropertyEditorSupport {
    		
            @Override
            public void setAsText(String text) {
            	if(StringUtils.isEmpty(text)) {
            		setValue(null);
            	} else {
                	PlanUser user = new PlanUser();
                	user.setUserID(Integer.parseInt(text));
                    setValue(user);
            	}
            }
            
            @Override
            public String getAsText() {
            	if(this.getValue() == null || !(this.getValue() instanceof PlanUser) ) {
            		return null;
            	} else {
            		PlanUser user = (PlanUser)this.getValue();
        			return user.getUserID().toString();
            	}
               }
    	}
    Finally, the relevant portion of my edit form is as follows ("userList" is set in the controller as a complete list of all available PlanUser objects from the database):
    Code:
    		<tr>
    			<td>Plan Owner</td>
    			<td>
    				<form:select path="planOwner">
    					<form:options items="${userList}" itemLabel="displayName" itemValue="userID" />
    				</form:select>
    			</td>
    		</tr>				
    		
    		<tr>
    			<td>Plan Users</td>
    			<td>
    				<form:select path="planUsers">
    					<form:options items="${userList}" itemLabel="displayName" itemValue="userID" />
    				</form:select>
    			</td>
    		</tr>
    It seems that the single value planOwner is working just fine - the value comes through and I can set it without any problems. However, when is comes to the planUsers Set, it doesn't seem to be parsing out the individual values. I get the following error message:

    org.apache.jasper.JasperException: org.springframework.core.convert.ConversionFailedE xception: Unable to convert value "[3]" from type 'java.util.Set' to type 'java.lang.String'; nested exception is org.springframework.core.convert.ConverterNotFound Exception: No converter found capable of converting from 'com.myownbadself.PlanUser_$$_javassist_0' to 'java.lang.String'
    Any help would be much appreciated.

    Thanks,
    Alex

  • #2
    I actually got around this by adding another custom editor that recognizes the Set and turns it into a String containing a comma-separated list of the IDs. This seems like a hack to me, surely there must be a cleaner way to do this?

    Comment

    Working...
    X