java 我可以在 JSP 的 URL 中将 List 作为参数传递吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12601070/
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
Can I pass a List as parameter in a URL in JSP?
提问by Nihal Sharma
I have to pass a List from a .jsp into my servlet and I want to do it through a URL. Can I do something like -
我必须将 .jsp 中的 List 传递到我的 servlet 中,并且我想通过 URL 来完成它。我可以做类似的事情 -
<a href="SellSelectedStockServlet?value=content" target="_self">
where 'content' is a List. I want to get the elements of this list in my servlet.
其中“内容”是一个列表。我想在我的 servlet 中获取此列表的元素。
Or I can only pass the individual parameters with a separation of '&'?
或者我只能传递带有“&”分隔的单个参数?
回答by JB Nizet
You need one parameter per element of the list. And all these parameters should have the same name:
列表中的每个元素都需要一个参数。所有这些参数都应该具有相同的名称:
SellSelectedStockServlet?values=elem1&values=elem2&values=elem3
In the servlet, you'll get all your list elements like this:
在 servlet 中,您将获得所有列表元素,如下所示:
String[] values = request.getParameterValues("values");
// contains elem1, elem2 and elem3.
Beware: the length of a query string is limited. Don't pass a large number of values this way.
注意:查询字符串的长度是有限的。不要以这种方式传递大量值。
回答by Raj Adroit
To get the object to the other JSP is to add it to the HttpServletRequest objects attribute field using a scriplet:
要将对象获取到其他 JSP 是使用脚本将其添加到 HttpServletRequest 对象属性字段:
JSP with the List:
带有列表的 JSP:
<%
request.setAttribute("theList", ListObject);
%>
The other JSP:
另一个JSP:
<%
List myList = (List) request.getAttribute("theList");
%>