entity中这样写:
private String xxx; //setXxx、getXxx方法
...
form中可以这样写:
private String[] xxx; //setXxx、getXxx方法
...
提交到后台之后这样解析:
public static synchronized Collection getCollection(HttpServletRequest parameters,
Class entity) throws
InvocationTargetException,
InstantiationException,
IllegalAccessException {
Object dto = entity.newInstance();
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(dto);
String propName = null;
int length = 0;
String[] tmp = null;
for(int i = 0; i < origDescriptors.length; i++){
propName = origDescriptors[i].getName();
//属性不能是class
if(!"class".equals(propName)){
tmp = parameters.getParameterValues(propName);
if(null != tmp){
length = tmp.length;
break;
}
}
}
Collection result = new ArrayList();
for (int j = 0; j < length; j++) {
Object item = entity.newInstance();
for (int i = 0; i < origDescriptors.length; i++) {
if (origDescriptors[i].getReadMethod() == null) {
if (log.isTraceEnabled()) {
log.trace("-->No getter on JavaBean for " + origDescriptors[i].getName() + ", skipping");
}
continue;
}
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
Object value = null;
tmp = parameters.getParameterValues(name);
if(null != tmp)
value = tmp[j];
BeanUtils.copyProperty(item, name, value);
}
result.add(item);
}
return result;
}然后根据生成的集合进行后台数据处理即可。
本文详细解释了如何在Java实体类中定义属性,并通过HttpServletRequest接收参数,使用Reflection解析并设置属性值,最后返回一个包含所有参数对应属性值的集合。此过程涉及到属性名称、getter和setter方法的使用,以及如何处理参数值的长度限制。
7122

被折叠的 条评论
为什么被折叠?



