struts2中Action怎样获取动态表单中的多行数据

 我来答
huanglenzhi
推荐于2017-10-14 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
 一: struts-config.xml 配置
<struts-config>
  <form-beans>
    <form-bean name="trafficForm" type="com.ccit.safetm.controller.traffic.TrafficForm"></form-bean>
  </form-beans>
  <action-mappings>
    <action path="/traffic/addTraPolicy" type="com.ccit.safetm.controller.traffic.AddTraPolicyAction" name="trafficForm" scope="request">
<forward name="success" path="/jsp/success.jsp"></forward>
<forward name="error" path="/jsp/error.jsp"></forward>
</action>
  </action-mappings>
</struts-config>

二: 输入页面�0�2add_traffic_policy.jsp

�0�2
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8" isELIgnored="false"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加流量监控策略</title>
<link href="<%=request.getContextPath()%>/css/css_blue.css"
rel="stylesheet" type="text/css" id="cssStyle" />

<script type="text/javascript"
src="<%=request.getContextPath()%>/js/judge.js"></script>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.2.6.js"></script>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery.cookie.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added

// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

// manipulate the name/id values of the input inside the new element
// newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);

newElem.children(':first').attr('id', 'user['+ newNum + ']day' ).attr('name', 'user['+ newNum + ']day' );
newElem.children(':next').attr('id', 'user['+ newNum + ']per' ).attr('name', 'user['+ newNum + ']per' );
newElem.children(':last').attr('id', 'user['+ newNum + ']content' ).attr('name', 'user['+ newNum + ']content' );

// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);

// enable the "remove" button
$('#btnDel').attr('disabled','');

// business rule: you can only add 5 names
if (newNum == 5)
$('#btnAdd').attr('disabled','disabled');
});

$('#btnDel').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
$('#input' + num).remove(); // remove the last element

// enable the "add" button
$('#btnAdd').attr('disabled','');

// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});

$('#btnDel').attr('disabled','disabled');
});
</script>

</head>
<body class="main_bg">
<html:form method="post" action="/traffic/addTraPolicy.do">
<table width="90%" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td width="1%" height="33"
background="<%=request.getContextPath()%>/images/main_02.gif">
<img src="<%=request.getContextPath()%>/images/main_01.gif"
width="9" height="33" />
</td>
<td width="3%" height="33"
background="<%=request.getContextPath()%>/images/main_02.gif">
<img src="<%=request.getContextPath()%>/images/main_03.gif"
width="18" height="17" />
</td>
<td width="96%"
background="<%=request.getContextPath()%>/images/main_02.gif"
class="text10">
添加流量监控策略
</td>
</tr>
</table>

<br />
<table class="admintable" width="90%" border="0" align="center"
cellpadding="4" cellspacing="0" bgcolor="#cde6f6">

<tr>
<td>
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
提醒日:
<input type="text" name="traffics[0].checkDay" id="traffics[0].checkDay" />
提醒百分比:
<input type="text" name="traffics[0].warnPercent" id="traffics[0].warnPercent" />
推送消息内容:
<input type="text" name="traffics[0].warnContent" id="traffics[0].warnContent" />
</div>
</td>
<td>
<div>
<input type="button" id="btnAdd" value="添加策略" />
<input type="button" id="btnDel" value="移除策略" />
</div>
</td>
</tr>

<tr>
<td height="25" colspan="4" align="center" bgcolor="#FFFFFF">
<input type="submit" value="提交" />
</td>
</tr>
</table>

</html:form>
</body>

</html>

�0�2

�0�2
三: TrafficForm的关键代码

public class TrafficForm extends BaseActionForm implements Serializable {
private static final long serialVersionUID = 454397279205151336L;

private List traffics = new AutoArrayList(Traffic.class);

public List getTraffics() {
return traffics;
}

public void setTraffics(List traffics) {
this.traffics.clear();
this.traffics.addAll(traffics);
}

}

�0�2
四.bean,提问页面省略了get set方法

public class Traffic extends BaseActionForm implements Serializable {
private static final long serialVersionUID = 454397279205151336L;

/**
* Fields
*/
private Integer policyId;
private Integer checkDay;
private Integer warnPercent;
private String warnContent;

/** default constructor */
public Traffic() {
}

/** full constructor */
public Traffic(Integer policyId, Integer checkDay, Integer warnPercent, String warnContent) {
super();
this.policyId = policyId;
this.checkDay = checkDay;
this.warnPercent = warnPercent;
this.warnContent = warnContent;
}

�0�2 �0�2 �0�2 �0�2
五.�0�2自定义的 AutoArrayList

public class AutoArrayList extends ArrayList implements Serializable {

private static final long serialVersionUID = 1L;
private Class itemClass;

public AutoArrayList(Class itemClass){
this.itemClass = itemClass;
}

public Object get(int index){
try {
while(index >= size()){
add(itemClass.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
return super.get(index);
}
}

�0�2
六.AddTraPolicyAction

TrafficForm trafficForm = (TrafficForm)form;
List traffics = trafficForm.getTraffics();
System.out.println("traffics.size():"+traffics.size());
for (int i = 0; i < traffics.size(); i++) {
Traffic traffic = (Traffic) traffics.get(i);
System.out.println("Traffic["+i+"]CheckDay:"+traffic.getcheckDay()+" WarnPercent:"+traffic.getWarnPercent()+" WarnContent:"+traffic.getWarnContent());
}

// Integer checkDay = trafficForm.getcheckDay();
// Integer warnPercent = trafficForm.getWarnPercent();
// String warnContent = trafficForm.getWarnContent();
// System.out.println("checkDay:"+checkDay);
// System.out.println("warnPercent:"+warnPercent);
// System.out.println("warnContent:"+warnContent);
//

// String[] days = request.getParameterValues("day");
// String[] pers = request.getParameterValues("per");
// String[] contents = request.getParameterValues("content");
// System.out.println("测试day数组元素个数"+days.length);
// System.out.println("测试pers数组元素个数"+pers.length);
// System.out.println("测试contents数组元素个数"+contents.length);

// while(request.getParameterNames().hasMoreElements()){
// String day=(String)request.getParameterNames().nextElement();
// String value=request.getParameter(day);
// System.out.println("测试name:"+day+",它的值是:"+value);
// }

在action中测试,试过traffics的size总是1,总是只能获取表格中的第一行数据。也试过request.getParameterValues依然取不到其他行的数据,求解哪里有问题呢?附上我参考的页面
http://tech.ddvip.com/2008-12/122881555798671_2.html
小曹大人_
2014-11-27 · TA获得超过134个赞
知道小有建树答主
回答量:81
采纳率:0%
帮助的人:85.1万
展开全部
var num = $('.clonedInput').length;
这里的取值永远都会是1.因为你克隆的只是input,并没有克隆div。所以就算你有多个input,也不会取到多行的,你可以试试克隆tr好一点。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yuboalt
2014-11-27 · TA获得超过578个赞
知道小有建树答主
回答量:509
采纳率:0%
帮助的人:208万
展开全部
你的表单代码 能看看么
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式