Java 如何使用 request.getParameterValues?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32307855/
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 use request.getParameterValues?
提问by Dan S.
I'm trying to pass an array from one jsp page to another using a hidden form.
我正在尝试使用隐藏表单将数组从一个 jsp 页面传递到另一个页面。
Here is the relevant code of my jsp files.
这是我的jsp文件的相关代码。
<td style="vertical-align: top;"><button onclick="getPlayerNames()"id="generate">Generate</button><br></td>
<form id="playerNames" method="post" action="lineups.jsp">
<input type="hidden" id="players" />
</form>
<script>
function getPlayerNames(){
var selected = document.querySelectorAll("#selected-players > tr > td");
var playernames = [];
for(var i=0; i<selected.length; ++i){
//alert(selected[i].textContent);
var num = (i-1)%6;
if(num==0){
playernames.push(selected[i].textContent);
}
}
document.getElementById("players").values=playernames;
alert(document.getElementById("players").values);
document.getElementById("playerNames").submit();
}</script>
The alert message does show that the correct values are being placed in "players"
警报消息确实显示正确的值被放置在“玩家”中
Then on my lineup.jsp I have:
然后在我的 lineup.jsp 上我有:
<%String[] s = request.getParameterValues("players");
System.out.println(s[0]);%>
and I get a null pointer exception on the line with 'System.out.println(s[0]);'
我在“System.out.println(s[0]);”行上得到一个空指针异常
采纳答案by Srinu
name attribute is not specified to input tag, if name attribute not specified then no value will be sent.
In your case
request.getParameter
andrequest.getParameterValues
return same value becauseplayers
element is specified only one. When you userequest.getParameter
it'll return directstring
,request.getParameterValues
will returnstring[]
with length1
.
未为输入标签指定名称属性,如果未指定名称属性,则不会发送任何值。
在您的情况下
request.getParameter
并request.getParameterValues
返回相同的值,因为players
element 仅指定了一个。当您使用request.getParameter
它时string
,request.getParameterValues
它将直接返回,将返回string[]
length1
。
If you want to send multiple players and you don't want to repeat element in your jsp, then concat the players
with some special character like the following:
如果您想发送多个玩家并且不想在您的 jsp 中重复元素,则将其players
与一些特殊字符连接起来,如下所示:
document.getElementById("players").value=playernames.join("::");
You can get is as string in lineup.jsp
and you can split it with the same special characters like the following:
您可以获取 is as string in,lineup.jsp
并且可以使用相同的特殊字符将其拆分,如下所示:
<%
String players = request.getParameter("players");
String[] s = players.split("::");
%>
回答by RishikeshD
String[] players = request.getParametervalues("nameOfTheHiddenField");
String[] 玩家 = request.getParametervalues("nameOfTheHiddenField");
Please try specifying a name for the hidden field and it will work.
请尝试为隐藏字段指定一个名称,它会起作用。
回答by sbur
OK, s is null here hence s[0]
throws NullPointerException
好的,这里 s 为空,因此s[0]
抛出NullPointerException
The method getParameterValues()
will generally come into picture if there is a chance of getting multiple values for any input parameter,
this method will retrieve all of it values and return it as string array.
该方法getParameterValues()
通常将进入画面,如果有越来越多值任何输入参数的机会,这种方法会检索所有的IT价值,并返回作为字符串数组。
but in your case I think you have only one value to fetch, use request.getAttribute
and try to print the result i.e. s and not s[0]
但就您而言,我认为您只有一个值可以获取、使用request.getAttribute
并尝试打印结果,即 s 而不是 s[0]
once s is not null , you can go for s[0]
一旦 s 不为 null ,你可以去 s[0]