Announcement Announcement Module
Collapse
No announcement yet.
<form:options> does not bind correct value on displaying data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • <form:options> does not bind correct value on displaying data

    SWF: 2.2.1
    Spring: 3.0.5

    I have the following problem: when I retrieve an object everything is bound correctly except a value dislplayed in a <form:options> tag. i.e. when I create the view and the underlying object is persisted, everything works ok, but when I retrieve the object, the first value in the drop.down is displayed instead of the correct one. The docs say that <form:options> should take care of this.

    It is a mixed MVC and Spring Web Flow application.
    I have registered a Formatter like so:

    Code:
    @Component
    public class InstitutionFormatter implements Formatter<Institution> {
    	
    	@Autowired
    	private InstitutionDao institutionDao;
    
    	@Override
    	public String print(Institution object, Locale locale) {
    		System.out.println("QQQ-print: " + object.getId());
    		return String.valueOf(object.getId());
    	}
    
    	@Override
    	public Institution parse(String text, Locale locale) throws ParseException {
    		if(text == null) {
    			return null;
    		} else {
    			System.out.println("QQQ-parse: " + text);
    			return institutionDao.findById(Long.valueOf(text));
    		}
    	}
    
    }
    webmvc-config.xml
    Code:
    <mvc:annotation-driven conversion-service="applicationConversionService"/>
    	
    	<!-- Conversion services -->
    	<bean id="applicationConversionService" class="se.kmh.edudb.web.propertyeditor.CustomFormattingConversionServiceFactoryBean"/>
    webFlow-config.xml
    Code:
    <webflow:flow-builder-services 	development="true" 
    									id="flowBuilderServices" 
    									view-factory-creator="mvcViewFactoryCreator" 
    									conversion-service="defaultConversionService"/>
    
    	<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
    		<constructor-arg ref="applicationConversionService"/>
    	</bean>
    This is according to the docs. The formatter factory:
    Code:
    public class CustomFormattingConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {
    	
    	@Autowired
    	private InstitutionFormatter institutionFormatter;
    
    	@Override
    	protected void installFormatters(FormatterRegistry registry) {
    		super.installFormatters(registry);
    		registry.addFormatterForFieldType(Institution.class, institutionFormatter);
    	}
    	
    }
    Still the <form:options> doesn't do its job to bind correctly when I display. Does anybody know why, and what are the alternatives? Plain old <c:forEach> ?

  • #2
    The JSP snippet

    Code:
    <tr>
    			<td class="label">Institution:</td>
    			<td>
    			<form:select cssClass="" path="institution">
    				<form:options items="${institutions}" itemValue="id" itemLabel="name" />
    			</form:select>
    			</td>
    		</tr>

    Comment


    • #3
      Solved it!

      Got excellent help from this thread:
      http://forum.springsource.org/showthread.php?t=77285

      The trick is to implement equals() and hashCode(), otherwise the drop-down will not know which value to display.

      Cheers!

      Comment

      Working...
      X