Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环遍历ArrayList并更新属性_Java_Jsp_Session_Arraylist - Fatal编程技术网

Java 循环遍历ArrayList并更新属性

Java 循环遍历ArrayList并更新属性,java,jsp,session,arraylist,Java,Jsp,Session,Arraylist,我试图在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 i

我试图在
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); %>
}
...
//Main AL的类型为ArrayList。我正在尝试通过mainAL循环并将其中的每个项目传递到destination.jsp。
...
//这段代码为mainAL中的每个元素创建一个按钮,并在单击时将元素提交到destination.jsp。
对于(ArrayList al:mainAL)
{
}
...
destination.jsp:

...
<% ArrayList<String> result = (ArrayList<String>) session.getAttribute("list"); %>
<%out.println(list);%>
...
。。。
...
如果
mainAL
包含多个项,则从for循环生成的每个
destination.jsp
实例将仅显示
mainAL
中的最后一个ArrayList。我应该如何解决这个问题?是否有方法将每个ArrayList传递到
destination.jsp
,而不覆盖其值?

session.setAttribute()函数全局设置一个值。这意味着,您将覆盖每个循环周期中的值。您必须进行隐藏输入,在其中放入数据,以便将其作为请求url中的GET参数传递到其他目标站点。在目的地站点,您必须从请求url检索该数据。当你想保持你的url干净时,你也可以使用POST。我还建议使用会话令牌执行类似操作,而不是通过输入传递列表


我希望这对您有所帮助:)

现在是21世纪,不要将Java代码放入JSP,请使用JSTL。另外,如果您是
提交
,那么您的表单方法应该是
POST