我正在尝试遍历 start.jsp
中的 ArrayList,并将每个项目传递到不同的 jsp destination.jsp
。我目前正在使用会话,并且我知道 session.setAttribute
函数将覆盖该属性以前的任何值。这是我的代码:
start.jsp:
// Main AL has the type ArrayList<ArrayList<String>>. I am trying to loop through the mainAL and pass each item in it to destination.jsp.
...
// This snipet of code creates a button for each element in mainAL and submits the element to destination.jsp when clicked.
for (ArrayList<String> al : mainAL)
{
<form action="destination.jsp" method="get">
<input type="submit"/>
</form>
<% session.setAttribute("list", al); %>
}
...
destination.jsp:
...
<% ArrayList<String> result = (ArrayList<String>) session.getAttribute("list"); %>
<%out.println(list);%>
...
如果 mainAL
有 1 个以上的项,则 for 循环生成的每个 destination.jsp
实例将仅显示 mainAL
中的最后一个 ArrayList。我应该如何解决这个问题?有没有办法将每个 ArrayList 传递给 destination.jsp
而不会覆盖其值?
答案 0 :(得分:1)
session.setAttribute() 函数设置全局值。这意味着,您会在每个循环周期中覆盖该值。您将不得不进行隐藏输入,在您输入数据的过程中,它会作为 GET 参数在请求 url 中传递到您的其他目标站点。在目标站点,您必须从请求 url 中检索该数据。当您想保持 url 干净时,您也可以使用 POST。我还建议使用会话令牌执行此类操作,而不是通过输入传递列表。
希望对你有帮助:)