以下是填充formbean的源码
package com.chusiyou.struts.core;
import java.lang.reflect.Field;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
public class FormFullUtil {
public static void full(HttpServletRequest request, String formClassName,
String formName) {
try {
//得到要填充的FormBean对象
Object object = Class.forName(formClassName).newInstance();
//得到要填充的所有属性
Field[] fields=object.getClass().getDeclaredFields();
//得到所有请求参数
Enumeration enumeratrion=request.getParameterNames();
while(enumeratrion.hasMoreElements()){
String parname=(String)enumeratrion.nextElement();
for(Field field:fields){
//对所有属性进行一一赋值
if(parname.equals(field.getName())){
String fieldName=field.getName();
String value=request.getParameter(fieldName);
BeanUtils.setProperty(object,fieldName,value);
}
}
}
//把formbean对象保存进session对象中
request.getSession().setAttribute(formName, object);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.chusiyou.struts.core;
import java.lang.reflect.Field;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
public class FormFullUtil {
public static void full(HttpServletRequest request, String formClassName,
String formName) {
try {
//得到要填充的FormBean对象
Object object = Class.forName(formClassName).newInstance();
//得到要填充的所有属性
Field[] fields=object.getClass().getDeclaredFields();
//得到所有请求参数
Enumeration enumeratrion=request.getParameterNames();
while(enumeratrion.hasMoreElements()){
String parname=(String)enumeratrion.nextElement();
for(Field field:fields){
//对所有属性进行一一赋值
if(parname.equals(field.getName())){
String fieldName=field.getName();
String value=request.getParameter(fieldName);
BeanUtils.setProperty(object,fieldName,value);
}
}
}
//把formbean对象保存进session对象中
request.getSession().setAttribute(formName, object);
} catch (Exception e) {
e.printStackTrace();
}
}
}