Announcement Announcement Module
No announcement yet.
<form:select> fails to bind to java.util.Set property Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • <form:select> fails to bind to java.util.Set property

    I have a form that uses form:form tags. In particular I bind a multi-select box to a java.util.Set property. It works fine to save a new record, but when I attempt to render an existing element, I get an error message about converting. I'm using @InitBinder and a custom property editor, as listed in the below @Controller code:

    @Controller @RequestMapping("/admin")
    public class LiftTypeFormController {
        Log log = LogFactory.getLog(getClass());
        private LiftWorkoutDao liftWorkoutDao;
        private MuscleGroupEditor muscleGroupEditor;
        @RequestMapping(value = "/liftType/{liftTypeId}", method = RequestMethod.GET)
        public String editLiftType(@PathVariable Integer liftTypeId, Model model) {
        	return "liftType";
        protected void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, new DecimalFormat(), true));
            binder.registerCustomEditor(MuscleGroup.class, muscleGroupEditor);
    Here is the relevant JSP fragment:

                                	<form:select path="muscleGroups" size="15" multiple="true" items="${childlessMuscleGroups}"
                                		itemValue="muscleGroupId" itemLabel="name"/>
                                    <font color="red"><form:errors path="muscleGroups"/></font>                                    
    If I take it down to just <form:select path="muscleGroups"/> and nothing else, I still get the following error (but only on edit, not on add new):

    org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from 'com.espey.common.dto.workout.lift.MuscleGroup' to 'java.lang.String'
    I can also replace the <form:select> tag with old school <spring:bind> and I get the same error. Again, I know the PropertyEditor works since the form works for adding new items, but for some reason it doesn't work on rendering an existing form. I added debugging and can see that the @InitBinder method is invoked prior to rendering the page too. For what it's worth, I've also added a CustomCollection editor and had the exact same result.

    If I should be using a different approach like formatting or conversion to do this, can someone please point me in the right direction?
    Last edited by jespey; Nov 23rd, 2010, 07:11 PM.

  • #2
    You shouldn't use editors but define a MuscleGroupToStringConverter and register it using Spring's Converter SPI. Read the reference documentation.


    • #3
      Thanks, I had a feeling I should do that but wasn't sure if there was a quick fix for this.