我有一个以下枚举类型:
public enum SearchLocationTypes {REGISTRATION, WORKPLACE}
我为这个enum + formatter注释工厂创建了两个:converter和formatter(以防万一)。
格式化:
public class SearchLocationTypesFormatter implements Formatter<SearchLocationTypes> {
@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();
}
}
厂:
public class SearchLocationTypesAnnotationFormatterFactory
implements AnnotationFormatterFactory<SearchLocationTypesFormat> {
@Override
public Set<Class<?>> getFieldTypes() {
return new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {SearchLocationTypes.class}));
}
@Override
public Printer<?> getPrinter(SearchLocationTypesFormat a, Class<?> type) {
return new SearchLocationTypesFormatter();
}
@Override
public Parser<?> getParser(SearchLocationTypesFormat a, Class<?> type) {
return new SearchLocationTypesFormatter();
}
}
注释:
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SearchLocationTypesFormat {}
转换器:
public class SearchLocationTypesConverter implements Converter<SearchLocationTypes, String> {
@Override
public String convert(SearchLocationTypes type) {
String strType = "";
switch(type) {
case REGISTRATION:
strType = "rg";
break;
case WORKPLACE:
strType = "wp";
break;
}
return strType;
}
}
servlet.xml的一部分:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="ru.riado.web.format.SearchLocationTypesConverter" />
</set>
</property>
<property name="formatters">
<set>
<bean class="ru.riado.web.format.SearchLocationTypesAnnotationFormatterFactory" />
<bean class="ru.riado.web.format.SearchSortTypesFormatter" />
</set>
</property>
</bean>
当我从JSP调用以下代码时,一切都按预期工作。
实施例:
假设我有一个bean“form”,其属性“locType”设置为“REGISTRATION”。
public class Form {
@SearchLocationTypesFormat
private SearchLocationTypes locType;
// setters and getters
}
现在,我正在使用以下代码,其输出为“rg”:
<s:eval expression="form.locType">
但是弹簧形式不会将它的值转换为“rg”。而不是将其设置为“REGISTRATION”,就像它使用默认转换器一样。我试图关闭默认转换,但这没有帮助。
有谁能告诉我我在这里缺少什么?
答案 0 :(得分:0)
我首先重构你的代码:
public enum SearchLocationTypes {
REGISTRATION("rg"),
WORKPLACE("wp");
private String abbr;
SearchLocationTypes(String abbr) {
this.abbr = abbr;
}
@Override
public String toString() {
return abbr;
}
public static SearchLocationTypes fromString(String from) {
for (SearchLocationTypes locType : SearchLocationTypes.values()) {
if (locType.abbr.equals(from)) {
return locType;
}
}
throw new IllegalArgumentException();
}
}
和Formatter
(以及Converter
与print方法相同):
public class SearchLocationTypesFormatter implements
Formatter<SearchLocationTypes> {
@Override
public String print(SearchLocationTypes type, Locale locale) {
return type == null ? "" : type.toString();
}
@Override
public SearchLocationTypes parse(String strType, Locale locale)
throws ParseException {
return SearchLocationTypes.fromString(strType);
}
}
现在,您可以在jsp中使用form:radiobuttons
或form:options
:
<form:select path="locType">
<form:options />
</form:select>
默认情况下,表单值是枚举的名称(),表单标签是枚举的toString()(来自https://jira.springsource.org/browse/SPR-3389)。
我无法查看,但现在尝试<s:eval expression="form.locType">
。