Java 如何从 JSP 中的选择框中获取多个选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get multiple selected values from select box in JSP?
提问by Param-Ganak
I have a html form which have a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is get method. The html code for form is as follows.
我有一个 html 表单,它有一个选择列表框,您可以从中选择多个值,因为它的 multiple 属性设置为multiple。考虑表单方法是获取方法。表单的html代码如下。
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.jsp">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.jsp page. So how the selected values are accessed on display.jsp page.
我想在 display.jsp 页面上的选择列表框中显示选定的值。那么如何在 display.jsp 页面上访问选定的值。
采纳答案by Bozho
request.getParameterValues("select2")
returns an array of all submitted values.
request.getParameterValues("select2")
返回所有提交值的数组。
回答by Hyman Leow
Something along the lines of (using JSTL):
类似于(使用 JSTL):
<p>Selected Values:
<ul>
<c:forEach items="${paramValues['select2']}" var="selectedValue">
<li><c:out value="${selectedValue}" /></li>
</c:forEach>
</ul>
</p>
回答by averymogen
It would seem overkill but Spring Forms handles this elegantly. That is of course if you are already using Spring MVC and you want to take advantage of the Spring Forms feature.
这似乎有点矫枉过正,但 Spring Forms 优雅地处理了这个问题。如果您已经在使用 Spring MVC 并且想要利用 Spring Forms 功能,那当然是正确的。
// jsp form
<form:select path="friendlyNumber" items="${friendlyNumberItems}" />
// the command class
public class NumberCmd {
private String[] friendlyNumber;
}
// in your Spring MVC controller submit method
@RequestMapping(method=RequestMethod.POST)
public String manageOrders(@ModelAttribute("nbrCmd") NumberCmd nbrCmd){
String[] selectedNumbers = nbrCmd.getFriendlyNumber();
}
回答by Shiva
Since I don't find a simple answer just adding more this will be JSP page. save this content to a jsp file once you run you can see the values of the selected displayed.
因为我没有找到一个简单的答案只是添加更多这将是 JSP 页面。运行后将此内容保存到 jsp 文件中,您可以看到所选显示的值。
Update: save the file as test.jsp and run it on any web/app server
更新:将文件保存为 test.jsp 并在任何 web/app 服务器上运行它
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<% String[] a = request.getParameterValues("multiple");
if(a!=null)
{
for(int i=0;i<a.length;i++){
//out.println(Integer.parseInt(a[i])); //If integer
out.println(a[i]);
}}
%>
<html>
<body>
<form action="test.jsp" method="get">
<select name="multiple" multiple="multiple"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>
<input type="submit">
</form>
</body>
</html>