Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

jsp - How to pass a list from one action to another in Struts 2 without using session?

I am displaying a list in my JSP as shown below:

<%@page  contentType="text/html;charset=UTF-8"language="java"pageEncoding="UTF-8"%>
<%@taglib prefix="s"uri="/struts-tags"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>xxx</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>

<s:form name="tableForm"method="post">
<th>
<s:submit action="verify" key="Add"></s:submit>
</th>
<s:hidden name="propagateList" value="%{formList}"/>
<table border="1">

<tr>
<th >ID</th>
<th>Name</th>
<th>Status</th>
<th>Type</th>
<th>System</th>
</tr>


<s:iterator value="formList">
<tr>
<td><s:checkbox name="checked" fieldValue="%{#attr.ID}" theme="simple" ></s:checkbox>
</td>
<td><s:property value="NAME"/></td>
<td><s:property value="STATUS"/></td>
<td><s:property value="TYPE"/></td>
<td><s:property value="UNIT"/></td>
</tr>
</s:iterator>

</table>

</s:form>
</body>
</html>

Here I want to pass the list formList to another action when I click on Add button without having to hit the database to fetch the list formList once again.

I tried using <s:hidden name="propagateList" value="%{formList}"/> but it does not work.

This list contains more than 1000 records , so is there any way to pass this list from the jsp to another action in Struts 2 without using session?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To answer the question "how to pass a List from ActionA to ActionB without using the Session":

  • in case of a List<String> :

    <s:iterator value="formList" status="row">
        <s:hidden name="formList[%{#row.index}]" />
    </s:iterator>
    

This will iterate through your whole List, and will generate an <s:hidden/> element for every element of the List; this way, you can pass an un-altered List from one Action to another.

  • in case of a List<Object> , where the object is that you've posted in the page:

    <s:iterator value="formList" status="row">
        <s:hidden name="formList[%{#row.index}].id" />
        <s:hidden name="formList[%{#row.index}].name" />
        <s:hidden name="formList[%{#row.index}].status" />
        <s:hidden name="formList[%{#row.index}].type" />
        <s:hidden name="formList[%{#row.index}].unit" />
    </s:iterator>
    

Exactly as before, this will iterate through your whole List, generating five elements for every object of the List.

Using this concept, you can alter the List by using interactive tags (textfield, select, etc) instead of read-only tags (hidden, property etc):

<s:iterator value="formList" status="row">
    <s:hidden name="formList[%{#row.index}].id" />
    <s:textfield name="formList[%{#row.index}].name" value="name" />        
    <s:hidden   name="formList[%{#row.index}].status" />
    <s:property value="status" />
    <s:textfield name="formList[%{#row.index}].type" value="type" />
    <s:textfield name="formList[%{#row.index}].unit" value="unit" />
</s:iterator>

Of course your List will be vulnerable to client-side changes, every user able to press F12 will be able to modify your List, then you should be careful.

You could, for example, put only the ID**s in the **session, inject the List in the JSP, then when receiving the data back, match the *ID*s of List coming from the page with the *ID*s you have in Session, for checking the integrity of the data posted (no new IDs, no double IDs etc)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...