页面表单数据的自动封装到javaBean中
先定义一个Bean类
package com.test;
public class Bean {
private String name;
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
http://www.doczj.com/doc/7d492460f5335a8102d22069.html = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
再定义一个封装数据类用于把数据封装到Bean中
package com.test;
import http://www.doczj.com/doc/7d492460f5335a8102d22069.htmlng.reflect.InvocationTargetException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import http://www.doczj.com/doc/7d492460f5335a8102d22069.htmlmons.beanutils.ConvertUtils;
import http://www.doczj.com/doc/7d492460f5335a8102d22069.htmlmons.beanutils.PropertyUtils;
public class ParseHtml {
public Object parseRequest(HttpServletRequest request,Object bean){
//取得所有参数列表
Enumeration enum = request.getParameterNames();
//遍历所有参数列表
while(enum.hasMoreElements()){
Object obj = enum.nextElement();
try {
//取得这个参数在Bean中的数据类开
Class cls = PropertyUtils.getPropertyType(bean, obj.toString());
//把相应的数据转换成对应的数据类型
Object beanValue = ConvertUtils.convert(request.getParameter(obj.toString()), cls);