Announcement Announcement Module
Collapse
No announcement yet.
<form:select> fails to bind to java.util.Set property Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    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) {
        	model.addAttribute(liftWorkoutDao.findLiftTypeById(liftTypeId));
        	return "liftType";
        }
        
        @InitBinder("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:

    Code:
                    <td>
                                	<form:select path="muscleGroups" size="15" multiple="true" items="${childlessMuscleGroups}"
                                		itemValue="muscleGroupId" itemLabel="name"/>
                                    <font color="red"><form:errors path="muscleGroups"/></font>                                    
                                </td>
    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):

    Code:
    org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from 'com.espey.common.dto.workout.lift.MuscleGroup' to 'java.lang.String'
    	org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:181)
    	org.springframework.core.convert.support.CollectionToStringConverter.convert(CollectionToStringConverter.java:65)
    	org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:36)
    	org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:184)
    	org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:124)
    	org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:227)
    	org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
    	org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    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, 08:11 PM.

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

    Comment


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

      Comment

      Working...
      X