1.枚举类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
public enum GoodsPromoteEnum {
/**
* 0 精品
*/
fine( "精品" , 0 ),
/**
* 1 限时购
*/
limit( "限时购" , 1 ),
/**
* 2 特价
*/
cheap( "特价" , 2 );
private String value;
private int index;
private GoodsPromoteEnum(String value, int index) {
this .value = value;
this .index = index;
}
public static GoodsPromoteEnum get(String value){
for (GoodsPromoteEnum p : GoodsPromoteEnum.values()) {
if (p.getValue().equals(value)) {
return p;
}
}
return null ;
}
public static GoodsPromoteEnum get( int index){
for (GoodsPromoteEnum p : GoodsPromoteEnum.values()) {
if (p.getIndex() == index) {
return p;
}
}
return null ;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this .value = value;
}
public int getIndex() {
return index;
}
public void setIndex( int index) {
this .index = index;
}
}
|
2.编写自定义处理类,继承Converter接口
1
2
3
4
5
6
7
8
9
10
11
|
public class StringToGoodsConverter implements Converter<String, GoodsPromoteEnum> {
@Override
public GoodsPromoteEnum convert(String value) {
if (StringUtils.isBlank(value)) {
return null ;
}
return GoodsPromoteEnum.get(value);
}
}
|
3.在springmvc配置文件里配置
1
2
3
4
5
6
7
8
|
<!--自定义枚举类封装 -->
< bean id = "conversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean" >
< property name = "converters" >
< set >
< bean class = "com.tentcoo.zbh.util.StringToGoodsConverter" />
</ set >
</ property >
</ bean >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/chreless2/p/springmvc.html