Java ServletRequest.getParameterMap() 返回 Map<String, String[]> 和 ServletRequest.getParameter() 返回 String?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1928675/
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
ServletRequest.getParameterMap() returns Map<String, String[]> and ServletRequest.getParameter() returns String?
提问by BillMan
Can someone explain to me why ServletRequest.getParameterMap()
returns type
有人可以向我解释为什么ServletRequest.getParameterMap()
返回类型
Map<String, String[]>
ServletRequest.getParameter()
just returns type String
ServletRequest.getParameter()
只返回类型 String
I'm don't understand why the map would ever map to more then one value. TIA.
我不明白为什么地图会映射到一个以上的值。TIA。
采纳答案by BalusC
It returns all parameter values for controls with the samename.
它返回具有相同名称的控件的所有参数值。
For example:
例如:
<input type="checkbox" name="cars" value="audi" /> Audi
<input type="checkbox" name="cars" value="ford" /> Ford
<input type="checkbox" name="cars" value="opel" /> Opel
or
或者
<select name="cars" multiple>
<option value="audi">Audi</option>
<option value="ford">Ford</option>
<option value="opel">Opel</option>
</select>
Any checked/selected values will come in as:
任何选中/选择的值都将作为:
String[] cars = request.getParameterValues("cars");
It's also useful for multiple selections in tables:
它对于表格中的多项选择也很有用:
<table>
<tr>
<th>Delete?</th>
<th>Foo</th>
</tr>
<c:forEach items="${list}" var="item">
<tr>
<td><input type="checkbox" name="delete" value="${item.id}"></td>
<td>${item.foo}</td>
</tr>
</c:forEach>
</table>
in combination with
结合
itemDAO.delete(request.getParameterValues("delete"));
回答by Steve B.
If you have a multi-value control like a multi-selectable list or a set of buttons mapped to the same name multiple selections will map to an array.
如果您有一个多值控件,如多选列表或一组映射到相同名称的按钮,则多个选择将映射到一个数组。
回答by Jonathan Feinberg
http://foo.com/bar?biff=banana&biff=pear&biff=grape
"biff" now maps to {"banana","pear","grape"}
“biff”现在映射到 {"banana","pear","grape"}
回答by Bozho
In the case with multi-value controls (checkbox, multi-select, etc), the request.getParameterValues(..)
is used to fetch the values.
对于多值控件(复选框、多选等),request.getParameterValues(..)
用于获取值。
回答by ZZ Coder
The real function to get all parameter values is
获取所有参数值的真正函数是
request.getParameterValues();
getParameter()
is just a shortcut to get first one.
getParameter()
只是获得第一个的捷径。