Julien
Julien

Reputation: 2756

Using <form:select> and <form:option> with converter but conversion does not happens, why?

I have this form for creating an Enrichment:

<form:form method="post" action="..." modelAttribute="enrichment">
    ...
    <form:select path="tag"> 
        <form:options items="${tagList}" itemValue="id" itemLabel="label" />
    </form:select>
    ...

The Enrichment class has a Tag attribute. So when th user has selected a tag in the Tag list, tag.id (wich is a String) is sent throught the form. I don't think I could directly send a tag object am I wright? So I wrote a Converter to convert a String to a Tag, according to http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#core-convert-Converter-API. So I did this :

public class IdToTagConverter implements Converter<String, Tag> {

    @Autowired
    TagService tagService;

    public Tag convert(String id) {     
        return tagService.findTagById(Integer.parseInt(id));
    }
}

And I created the bean :

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="exemple.IdToTagConverter"/>
        </list>
    </property>
</bean>

And I thought it would do the convertion automatically. But the error message is still here :

[Failed to convert property value of type 'java.lang.String' to required type 'exemple.Tag' for property 'tag'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [exemple.Tag] for property 'tag': no matching editors or conversion strategy found]

What did I miss?

Upvotes: 1

Views: 1777

Answers (2)

Julien
Julien

Reputation: 2756

Found the solution here : http://forum.springsource.org/showthread.php?84003-Converters-no-matching-editors-or-conversion-strategy-found

I just replaced

<mvc:annotation-driven />

by

<mvc:annotation-driven conversion-service="conversionService" />

and it worked. Why? Spring MVC Voodoo.

Upvotes: 1

Akshay
Akshay

Reputation: 3198

Looks like Spring is not aware of your converter, or Conversion Service. Follow this part of the documentation to register your custom converter - > http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/validation.html#format-configuring-FormattingConversionService

Upvotes: 0

Related Questions