Results 1 to 3 of 3

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

  1. #1
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default <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> ?
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  2. #2
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default 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>
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

  3. #3
    Join Date
    Aug 2004
    Location
    Stockholm
    Posts
    466

    Default 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!
    Sincerely,
    /The Cantor

    "Murphy was an optimist"
    (The O'Toole commentary on Murphy's Law)

Posting Permissions

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