apache.commons.beanutils转换器的使用
来源:互联网 发布:淘宝米乐麻麻代购真假 编辑:程序博客网 时间:2024/05/16 10:19
有一个枚举类型的类:
public enum Gender {MAN{public String getName(){return "男";}},WOMEN{public String getName(){return "女";}};public abstract String getName();}
为他定义一个转换器:
import org.apache.commons.beanutils.Converter;import cn.aaron.bean.user.Gender;public class GenderConverter implements Converter{@SuppressWarnings("unchecked")public Object convert(Class clazz, Object value) {if(value==null || "".equals((String)value)) return null;if(value instanceof Gender) return value;//如果要转换为枚举类型try{//如果要转换为String类型return Gender.valueOf((String) value);}catch (Exception e) {}return null;}}
最后在Filter里面注册一下就行了:
public class SetCodeFilter implements Filter{public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterchain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request;req.setCharacterEncoding("UTF-8");filterchain.doFilter(request, response);}public void init(FilterConfig config) throws ServletException {ConvertUtils.register(new DateConverter(), Date.class);ConvertUtils.register(new SexConverter(), Sex.class);ConvertUtils.register(new GenderConverter(), Gender.class);//此处就是注册Gender转换器ConvertUtils.register(new DeliverWayConverter(), DeliverWay.class);ConvertUtils.register(new PaymentWayConverter(), PaymentWay.class);try{Properties prop = new Properties();prop.put("runtime.log", config.getServletContext().getRealPath("/WEB-INF/log/velocity.log"));prop.put("file.resource.loader.path", config.getServletContext().getRealPath("/WEB-INF/vm"));prop.put("input.encoding", "UTF-8");prop.put("output.encoding", "UTF-8");Velocity.init(prop);}catch( Exception e ){e.printStackTrace();}}}
- apache.commons.beanutils转换器的使用
- apache.commons.beanutils.BeanUtils的使用
- apache Commons-beanutils的使用
- 自定义org.apache.commons.beanutils的类型转换器
- 自定义org.apache.commons.beanutils的类型转换器
- Apache Commons beanutils使用
- org.apache.commons.beanutils.BeanUtils使用示例
- apache.commons.beanutils.BeanUtils
- apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用
- 使用org.apache.commons.beanutils个人感受
- 使用org.apache.commons.beanutils个人感受
- org.apache.commons.beanutils.PropertyUtils使用明细
- Apache commons beanutils简介和使用
- Apache Commons BeanUtils组件介绍使用
- commons-beanutils包的使用
- Commons BeanUtils的使用
- 初用apache.commons.beanutils.BeanUtils
- 初用apache.commons.beanutils.BeanUtils
- 简要区分URI、URL和URN
- 在J2EE项目中处理浏览器的缓存
- 使用Filter控制缓存WebCache
- Fucking The Year 2011
- JFreeChart
- apache.commons.beanutils转换器的使用
- 毕设进度
- 解决Ext的Tooltip在IE9中不能显示
- 巴巴运动网权限模块的设置
- 巴巴运动网的性能优化
- 巴巴运动网订单模块的锁定管理
- 读书笔记:一个普通IT人的十年回顾
- 读书笔记:谁都可以进外企
- Linux 下将一个目录拷贝到另一个目录下