我的 jsp 中有一个表单,它没有固定数量的字段。用户可以根据需要添加任意数量的行。这是我的代码:
<form class="metrics_values" action="refreshMetrics.jsp" method="POST">
<div id="userId">User test id: <input type="text" name="userId" /> <input class="none" type="submit" value="Submit"></div>
<div class="rowWrapper">
<div class="row mName">
<input list="metrics" placeholder="Metric Name" name="metric_name" />
<datalist id="metrics">
<option value="userAgent">
<option value="header">
<option value="sni">
<option value="isversion_1_1">
<option value="sslVersion">
<option value="contenttype">
<option value="days">
<option value="server">
<option value="expip">
<option value="expauth">
<option value="expectmatch">
<option value="username">
<option value="password">
<option value="dbname">
<option value="redirectionFlag">
<option value="noDnsCache">
<option value="redirectionFlag">
</datalist>
</div>
<div class="row mvalue">
<input type="text" placeholder="Metric Value" name="metric_value" />
</div>
</div>
</form>
<a href="#" title="add row" class="add-row">+</a>
以及用于添加行的 jQuery:
jQuery(function(){
var counter = 1;
jQuery('a.add-row').click(function(event){
event.preventDefault();
counter++;
var newRow = jQuery('<div class="rowWrapper"><div class="row mName"> <input list="metrics" placeholder="Metric Name" name="metric_name"/> </div><div class="row mvalue"><input type="text" placeholder="Metric Value" name="metric_val"/></div></div>');
jQuery('form.metrics_values').append(newRow);
});
});
我尝试用以下方法获取数据:
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
但它为 paramValue
返回 null
。
有什么想法吗?
最佳答案
试试这个:
String[] names = request.getParameterValues("metric_name");
String[] values = request.getParameterValues("metric_value");
for (int i = 0; i < names.length; i++) {
System.out.print("<tr><td>" + names[i] + "</td>\n");
System.out.println("<td> " + valuse[i] + "</td></tr>\n");
}
关于java - jsp中获取表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35547889/