Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring 3.1.1 form tag not using formatter/converter
    text
    copied!<p><br> I have a following enum type:</p> <pre><code>public enum SearchLocationTypes {REGISTRATION, WORKPLACE} </code></pre> <p>I have created both: converter and formatter for this enum + formatter annotation factory (just in case).</p> <p>Formatter:</p> <pre><code>public class SearchLocationTypesFormatter implements Formatter&lt;SearchLocationTypes&gt; { @Override public String print(SearchLocationTypes type, Locale locale) { String strType = ""; switch(type) { case REGISTRATION: strType = "rg"; break; case WORKPLACE: strType = "wp"; break; } return strType; } @Override public SearchLocationTypes parse(String strType, Locale locale) throws ParseException { if(strType.equals("rg")) return SearchLocationTypes.REGISTRATION; if(strType.equals("wp")) return SearchLocationTypes.WORKPLACE; else throw new IllegalArgumentException(); } } </code></pre> <p>Factory:</p> <pre><code>public class SearchLocationTypesAnnotationFormatterFactory implements AnnotationFormatterFactory&lt;SearchLocationTypesFormat&gt; { @Override public Set&lt;Class&lt;?&gt;&gt; getFieldTypes() { return new HashSet&lt;Class&lt;?&gt;&gt;(Arrays.asList(new Class&lt;?&gt;[] {SearchLocationTypes.class})); } @Override public Printer&lt;?&gt; getPrinter(SearchLocationTypesFormat a, Class&lt;?&gt; type) { return new SearchLocationTypesFormatter(); } @Override public Parser&lt;?&gt; getParser(SearchLocationTypesFormat a, Class&lt;?&gt; type) { return new SearchLocationTypesFormatter(); } } </code></pre> <p>Annotation:</p> <pre><code>@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface SearchLocationTypesFormat {} </code></pre> <p>Converter:</p> <pre><code>public class SearchLocationTypesConverter implements Converter&lt;SearchLocationTypes, String&gt; { @Override public String convert(SearchLocationTypes type) { String strType = ""; switch(type) { case REGISTRATION: strType = "rg"; break; case WORKPLACE: strType = "wp"; break; } return strType; } } </code></pre> <p>part of servlet.xml:</p> <pre><code>&lt;mvc:annotation-driven conversion-service="conversionService" /&gt; &lt;bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"&gt; &lt;property name="converters"&gt; &lt;set&gt; &lt;bean class="ru.riado.web.format.SearchLocationTypesConverter" /&gt; &lt;/set&gt; &lt;/property&gt; &lt;property name="formatters"&gt; &lt;set&gt; &lt;bean class="ru.riado.web.format.SearchLocationTypesAnnotationFormatterFactory" /&gt; &lt;bean class="ru.riado.web.format.SearchSortTypesFormatter" /&gt; &lt;/set&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>When i invoke the following code from the JSP everything works just as expected. <br>Example: Let's say i have a bean "form" with property "locType" that is set to 'REGISTRATION'.</p> <pre><code>public class Form { @SearchLocationTypesFormat private SearchLocationTypes locType; // setters and getters } </code></pre> <p>Now, i'm using the following code and its output is "rg":</p> <pre><code>&lt;s:eval expression="form.locType"&gt; </code></pre> <p>But spring form doesn't convert it's value to "rg". Instead of this it sets the value to "REGISTRATION", just as if it is using default converter. I tried to turn the default conversion off, but that didn't help.</p> <p>Could anyone please tell me what i'm missing here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload