Struts2批量提交

来源:互联网 发布:域名跳转微博 编辑:程序博客网 时间:2024/05/16 04:54

前段时间在做一个项目,其中涉及到表单批量提交,查阅了一些资料后,个人用了一个比较笨的办法基本实现了想要的功能,下面分享给大家,供大家参考:

首先,在jsp中,我是先从数据库中提取数据,在jsp是通过<s:iterator></s:iterator>迭代出了N条数据。根据需求,用户可以对其中任意一条或多条进行修改,然后一次提交,form表单代码如下:

<form action="model!updateSelfInfo?modelNum=1" method="post" onsubmit="return confim();"><table border="0" width="100%" height="500"><tr><td style="text-align: left; border-right: none;"><font size="3"><strong>用电模式-->自定义模式</strong></font></td></tr><tr><td style="border-right: none; border-bottom: none;"><div style="margin-left: -5px;"><div style="height: 25px; width: 780px;"><table id="tab" border="0" width="100%" align="center"><tr style="background-color: #cccccc"><td align="center" width="150">位置</td><td align="center" width="150">电器</td><td align="center" width="150">开启时间</td><td align="center" width="150">关闭时间</td><td width="9"></td></tr></table></div><divstyle="height: 408px; overflow-y: scroll; margin-left: -0px; margin-top: -5px;"><table style="line-height: 1.5;" border="0" width="100%"><s:if test="!types.isEmpty()"><s:iterator value="types" status="type"><tr><td class="td_top" width="186"><input type="text" name="types[0].applianceLocation" value='<s:property value="applianceLocation" />'></td><td class="td_top" width="186"><input type="text" name="types[0].applianceName" value='<s:property value="applianceName" />'></td><td width="185"><input type="text" name="types[0].openTime" id="openTime" class="Wdate"onclick="WdatePicker({dateFmt:'HH:mm:ss'});"readonly="readonly" value='<s:property value="openTime" />'></td><td style="border-right: none;"><input type="text" name="types[0].closeTime" id="closeTime"class="Wdate" onclick="WdatePicker({dateFmt:'HH:mm:ss'});"readonly="readonly" value='<s:property value="closeTime" />'></td></tr></s:iterator></s:if><s:else><tr height="150"><td colspan="4" align="center"style="border-bottom: none; border-right: none;"><fontsize="3" color="gray">暂无插座状态信息,请添加</font></td></tr></s:else></table></div></div></td></tr><tr><td align="center"style="border-bottom: none; border-right: none; border-top: solid 1px #cccccc;"><input type="submit" value="启    用"style="background-image: url(../images/butn01.bmp); width: 122px; height: 37px; border: none;">        <input type="button" value="返    回"style="background-image: url(../images/butn01.bmp); width: 122px; height: 37px; border: none;"onclick="javascript:window.location.href='${pageContext.request.contextPath}/model/byself.jsp';"></td></tr></table></form>

这样的话,提交到action的是一个list集合,因此在action中要有这个List集合的get和set方法。

private List<UseElecTypeEntity> types = new ArrayList<UseElecTypeEntity>();public List<UseElecTypeEntity> getTypes() {return types;}public void setTypes(List<UseElecTypeEntity> types) {this.types = types;}

结果提交到action后,在action中作如下处理:

public String updateSelfInfo() {for (UseElecTypeEntity type : types) {String appLocStr = type.getApplianceLocation();String appNameStr = type.getApplianceName();String openTimeStr= type.getOpenTime();String closeTimeStr = type.getCloseTime();String [] appLocs = appLocStr.split(",");String [] appNames = appNameStr.split(",");String [] openTimes = openTimeStr.split(",");String [] closeTimes = closeTimeStr.split(",");for(int i=0; i<appLocs.length; i++){String appLoc = strutil.replaceBlank(appLocs[i]);String appName = strutil.replaceBlank(appNames[i]);String openTime = strutil.replaceBlank(openTimes[i]);String closeTime = strutil.replaceBlank(closeTimes[i]);String socketAddr = socketdao.getSocketAddr(homegateAddr, appLoc, appName);type.setModelNumber(modelNum);type.setApplianceLocation(appLoc);type.setApplianceName(appName);type.setOpenTime(openTime);type.setCloseTime(closeTime);type.setHomegateAddr(homegateAddr);type.setElectricOutletAddr(socketAddr);typedao.updateSelfInfo(type);}}return "upsuc";}

其基本思路就是字符串的一些算是,在为在jsp里提交数据的时候,是将多条数据拼接成一条,传到了action,所以在action中应该进行相应的逆操作。其中replaceBlank()方法是去除字符串中的空格,因为从jsp往action提交,在拼接字符串的时候会自动加上“, ”,即元素之间是用逗号和空格来分开的,所以在分隔字符串的时候,各子串会带上一个空格,要作一下处理。replaceBlank()方法的代码如下:

public class StringUtil {// java中去除字符串中的空格、回车、换行符、制表符public String replaceBlank(String str) {String dest = "";if (str != null) {Pattern p = Pattern.compile("\\s*|\t|\r|\n");Matcher m = p.matcher(str);dest = m.replaceAll("");}return dest;}}

好了,到这里,基本上就实现了批量提交的功能,与数据库交互的部分就不往文章里放了,希望对大家有所帮助!!!

原创粉丝点击