SpringMVC 自动封装枚举类的方法
springmvc默认无法自动封装枚举类,解决方法如下:
1.枚举类
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接口
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配置文件里配置
<!--自定义枚举类封装 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.tentcoo.zbh.util.StringToGoodsConverter" />
</set>
</property>
</bean>
SpringMVC 自动封装枚举类的方法的更多相关文章
- 枚举类valueOf方法的疑问
枚举类中valueOf方法只有一个参数而Enum类中有两个参数,请问Enum实例类中的valueOf方法是从何处继承而来? 答案:jvm进行编译的时候添加的.
- java枚举类Enum方法简介(valueof,value,ordinal)
Enum作为Sun全新引进的一个关键字,看起来很象是特殊的class, 它也可以有自己的变量,可以定义自己的方法,可以实现一个或者多个接口. 当我们在声明一个enum类型时,我们应该注意到en ...
- (转)SpringMVC学习(七)——Controller类的方法返回值
http://blog.csdn.net/yerenyuan_pku/article/details/72511844 本文所有案例代码的编写均建立在前文SpringMVC学习(六)——SpringM ...
- SpringMVC自动封装List对象 —— 自定义参数解析器
前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...
- 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- 使用springMvc对象属性自动封装从jsp向controller传值
controller通过ModelAndView向前台传传递信息 jsp也可以通过model向controller传递信息 这只是其中的一个办法,还有其他几种方式进行前后端的数据交互 如何才能让spr ...
- Enum 枚举类
目录 Enum 枚举类 基础 定义与用途 基本方法 示例 进阶 实现原理 枚举与Class对象 自定义枚举类和构造方法及toString() Enum中使用抽象方法来实现枚举实例的多态性 Enum与接 ...
- 根据值获取枚举类对象工具类EnumUtils
枚举类 public enum Sex { man("M","男"),woman("W","女"); private S ...
随机推荐
- C++ 中queue(队列)的用法
#include <iostream>#include <queue>#include <assert.h>/*调用的时候要有头文件: #include<st ...
- cache缓存帮助类
public class CacheHelper { /// <summary> /// 创建缓存项的文件 /// </summary> /// <param name= ...
- 【转】使用Eclipse搭建Python开发环境
因为要进行自动化测试,所以要搭建Python开发环境.这里将使用Eclipse+pyDev进行搭建,在此作为笔记记录下来. 需要的组件: 1.Eclipse SDK 3.7(这里将不再叙述Eclips ...
- jquery div 下拉框焦点事件
这章与上一张<jquery input 下拉框(模拟select控件)焦点事件>类似 这章讲述div的焦点事件如何使用 div的焦点事件与input的焦点事件区别在于 需要多添加一个属性: ...
- asdddddddddddddddd
<a href="www.baidu.com">sad</a>
- 【转载】Python的包管理工具Pip
接触了Ruby,发现它有个包管理工具RubyGem很好用,并且有很完备的文档系统http://rdoc.info 发现Python下也有同样的工具,包括easy_install和Pip.不过,我没有细 ...
- JSPatch常见问题解答
原文地址:https://github.com/bang590/JSPatch/wiki/JSPatch%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E7 ...
- 【转】android 属性动画之 ObjectAnimator
原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083 前面一篇博客讲解了 android 简单动画之 animtion,这里 ...
- B-index、bitmap-index、text-index使用场景详解
索引的种类:B-tree索引.Bitmap索引.TEXT index1. B-tree索引介绍: B-tree 是一种常见的数据结构,也称多路搜索树,并不是二叉树.B-tree 结构可以显著减少定位 ...
- 控制器(Controller) – ASP.NET MVC 4 系列
创建一个 ASP.NET MVC 4 Web Application 项目,将程序命名为 MvcMusicStore,如下图: 控制器 MVC 模式中,控制器主要负责响应用 ...