使用BeanUtils实现表单数据到JavaBean数据之间的封装
一个用于WEB层的工具方法,实现表单参数到JavaBean的封装
1.依赖的JAR文件
commons-beanutils-1.8.0.jar
commons-logging.jar
?
2.代码
package org.monday.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.ConvertUtils;import org.apache.commons.beanutils.Converter;/** * WEB层工具类 * * @author Monday */public class WebUtil {/** * 实现表单数据到JavaBean数据之间的封装 */@SuppressWarnings("unchecked")public static <T> T request2Bean(HttpServletRequest request, Class<T> beanClass) {try {/** 得到要转换Bean的类型 */T bean = beanClass.newInstance();/** 获取表单参数 */Map map = request.getParameterMap();/** 注册类型转换器 */ConvertUtils.register(new Converter() {/** 实现convert方法 */public Object convert(Class type, Object value) {if (value == null) {return null;}String str = (String) value;if ("".equals(str.trim())) {return null;}SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {return sdf.parse(str);} catch (ParseException e) {throw new RuntimeException(e);}}}, Date.class);/** 构建Bean */BeanUtils.populate(bean, map);return bean;} catch (Exception e) {throw new RuntimeException(e);}}}
?
?
其实,这些代码没什么。使用Struts2的ModelDriver可以很容易实现。
只是不想使用框架而已。最近有要开始找工作了,写写基础,复习复习.