日期:2014-05-18  浏览次数:20787 次

STRUTS中如何提交一个table中的多行数据并保存到数据库?
STRUTS中如何提交一个table中的多行数据并保存到数据库?
如:
第一行   1   2   3   4   5   6
第二行   7   8   9   2   5   6
....


------解决方案--------------------
public static List populateReqBeanList(Class clazz, HttpServletRequest request) {
List list = new ArrayList();
Map map = request.getParameterMap(); //获得字段名称 和值
Enumeration <String> e = request.getParameterNames(); //获得字段名称
int beancount = 0;
while (e.hasMoreElements()) {
String p = e.nextElement();
String[] pv = (String[]) map.get(p);
if (pv.length > beancount) {
beancount = pv.length;
}
}

for (int i = 0; i < beancount; i++) {
try {
Object o = clazz.newInstance();
list.add(o);
} catch (Exception e1) {
e1.printStackTrace();
}
}
Enumeration <String> e2 = request.getParameterNames();
while (e2.hasMoreElements()) {
String p = e2.nextElement();
String[] pv = (String[]) map.get(p);
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
try {
if (pv.length > i) {
BeanUtils.setProperty(o, p, pv[i]);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
return list;
}
------解决方案--------------------
这个是将表单提交的多行封装成一个list

这在循环list调用你的save(Object o);