简体   繁体   English

在Struts2中,如何在两个动作之间保留数据?

[英]In Struts2, how do I persist data between 2 actions?

I have a Struts 2 page with 2 DisplayTag tables on there, each having a List<T> . 我有一个Struts 2页面,上面有2个DisplayTag表,每个表都有一个List<T> This List<T> is populated in code-behind and can be a couple of thousand entries long. List<T>填充在代码后面,并且可以是几千个条目。 On the page, I can select rows of either of the tables and press a button to move them from one table to the other. 在页面上,我可以选择任何一个表的行,然后按一个按钮将它们从一个表移到另一个表。 This can be done multiple times before finally pressing another button to save the contents of one of the tables in the database. 在最终按下另一个按钮将其中一个表的内容保存到数据库之前,可以多次执行此操作。 I want to do both of these things in the action through AJAX. 我想通过AJAX在动作中同时做这两个事情。

userGroupEditmembers.jsp (apologies for the excess whitespace, remnant of using block selection in eclipse) userGroupEditmembers.jsp(对多余的空白表示歉意,在eclipse中残留使用块选择)

<table>
    <tr>
        <td>
            <p class="collectionAlt">
                <s:text name="overview.groupMembers" />
            </p>
            <s:set name="groupMembers" value="groupMembers" scope="request" />
            <display:table name="groupMembers" uid="row" excludedParams="*" cellspacing="1" cellpadding="1">
                <%-- include localized messages for the table's banner --%>                     
                <%@ include file="/jsp/includesstruts2/displayTagBanner.jsp"%> 

                <display:column sortable="false" headerClass="listHeader">
                    <s:checkbox name="groupMemberOids" fieldValue="%{#attr.row.oid}"  theme="simple" />                                                       
                </display:column>                                                               
                <%-- User Name --%>
                <display:column property="userId" titleKey="label.user.userId" headerClass="listHeader" />                                                 

                <%-- User Name --%>
                <display:column property="name" titleKey="label.user.name" headerClass="listHeader" />                                                 

                <%-- Whether the User is archived --%>                                          
                <display:column titleKey="label.user.archived" headerClass="listHeader"         
                    class="iconColumn">                                                         
                    <s:if test="%{#attr.row.archived}">                                         
                        <img src="<s:url value='/images/checkedWarning.gif'/>" border="0" />    
                    </s:if>                                                                     
                </display:column>                                                               
            </display:table>                                                                    
        </td>                                                                                   
        <td>                                                                                    
            <%-- 'Remove Users from Group' button --%>                                          
            <input value="&gt;&gt;" type="button" id="removeUsersButton" /> <br />              
            <br />                                                                              
            <%-- The 'Refresh' button. Used to reload the issueTrackingSystemEditDialog.jsp --%>
            <input value="&lt;&lt;" type="button" id="addUsersButton" />                        
        </td>                                                                                   
        <td>                                                                                    
            <p class="collectionAlt">                                                           
                <s:text name="overview.nonGroupMembers" />                                      
            </p>                                                                                
            <s:set name="nonGroupMembers" value="nonGroupMembers" scope="request" />            

            <display:table name="nonGroupMembers" uid="row" excludedParams="*"                  
                cellspacing="1" cellpadding="1">                                                
                <%-- include localized messages for the table's banner --%>                     
                <%@ include file="/jsp/includesstruts2/displayTagBanner.jsp"%>                  

                <display:column sortable="false" headerClass="listHeader">                      
                    <s:checkbox name="nonGroupMemberOids" fieldValue="%{#attr.row.oid}"         
                        theme="simple" />                                                       
                </display:column>                                                               
                <%-- User Name --%>                                                             
                <display:column property="userId" titleKey="label.user.userId"                  
                    headerClass="listHeader" />                                                 

                <%-- User Name --%>                                                             
                <display:column property="name" titleKey="label.user.name"                      
                    headerClass="listHeader" />                                                 

                <%-- Whether the User is archived --%>                                          
                <display:column titleKey="label.user.archived" headerClass="listHeader"         
                    class="iconColumn">                                                         
                    <s:if test="%{#attr.row.archived}">                                         
                        <img src="<s:url value='/images/checkedWarning.gif'/>" border="0" />    
                    </s:if>                                                                     
                </display:column>                                                               
            </display:table>                                                                    
        </td>                                                                                   
    </tr>                                                                                       
</table>                                                                                        
<s:token />                                                                                     


<s:hidden name="oid" />                                                                         
<s:hidden name="version" />                                                                     
<s:hidden name="groupMembersJson" />                                                            
<s:hidden name="nonGroupMembersJson" />

UserGroupEditMembersAction.java: UserGroupEditMembersAction.java:

private Integer oid;
private Integer version;

private List<UserView> groupMembers;

private List<UserView> nonGroupMembers;

private String groupMembersJson;

private String nonGroupMembersJson;

private Integer[] groupMemberOids;

private Integer[] nonGroupMemberOids;

private Boolean showArchived;

/**
 * method for DisplayUserGroupEdit action.
 * 
 * @return
 * @throws Exception
 */
public String display() throws Exception {
    if (showArchived == null) {
        showArchived = false;
    }
    UserService userService = (UserService) ServiceFactory.getInstance().createService(UserService.class);
    List<List<UserView>> usersForUserGroup = userService.getUsers(oid, showArchived);
    groupMembers = usersForUserGroup.get(0);
    nonGroupMembers = usersForUserGroup.get(1);
    groupMembersJson = serializeToJsonString(groupMembers);
    nonGroupMembersJson = serializeToJsonString(nonGroupMembers);

    return SUCCESS;
}

public String addMembers() throws Exception {
    groupMembers = (List<UserView>) deserializeFromJsonString(groupMembersJson);
    nonGroupMembers = (List<UserView>) deserializeFromJsonString(nonGroupMembersJson);
    UserService userService = (UserService) ServiceFactory.getInstance().createService(UserService.class);
    for (Integer userOid : nonGroupMemberOids) {
        UserView userview = userService.getUser(userOid);
        groupMembers.add(userview);
        for(int i = 0; i< nonGroupMembers.size(); i++){
            UserView view = nonGroupMembers.get(i);
            if(view.getOid().equals(userOid)){
                nonGroupMembers.remove(i);
                break;
            }
        }
    }
    groupMembersJson = serializeToJsonString(groupMembers);
    nonGroupMembersJson = serializeToJsonString(nonGroupMembers);
    return SUCCESS;
}

public String removeMembers() throws Exception {
    groupMembers = (List<UserView>) deserializeFromJsonString(groupMembersJson);
    nonGroupMembers = (List<UserView>) deserializeFromJsonString(nonGroupMembersJson);
    UserService userService = (UserService) ServiceFactory.getInstance().createService(UserService.class);
    for (Integer userOid : groupMemberOids) {
        UserView userview = userService.getUser(userOid);
        nonGroupMembers.add(userview);
        for(int i = 0; i< groupMembers.size(); i++){
            UserView view = groupMembers.get(i);
            if(view.getOid().equals(userOid)){
                groupMembers.remove(i);
                break;
            }
        }
    }
    groupMembersJson = serializeToJsonString(groupMembers);
    nonGroupMembersJson = serializeToJsonString(nonGroupMembers);
    return SUCCESS;
}                                                     

The problem I have is that when I'm moving elements from one List to the other List, I need access to the lists that are currently used in the tables. 我遇到的问题是,当我将元素从一个列表移动到另一个列表时,我需要访问表中当前使用的列表。 Because Struts2 actions are stateless, I need to persist these lists between 2 actions. 由于Struts2操作是无状态的,因此我需要在2个操作之间保留这些列表。

  • I've tried just having the lists in a <s: hidden /> block, but that can't be converted back easily. 我试过将列表放在<s: hidden />块中,但这不能轻易转换回去。
  • My current attempt is by serializing them to JSON through the JSONUtil.serialize and JSONUtil.deserialize methods, but for some reason that deserializes them to List<HashMap> instead of List<T> . 我当前的尝试是通过JSONUtil.serialize和JSONUtil.deserialize方法将它们序列化为JSON,但是由于某种原因,将它们反序列化为List<HashMap>而不是List<T>
  • I've suggested my coworker we use the Session, but he's concerned about performance losses. 我已经建议我的同事使用“会议”,但他担心性能会下降。

What's the best way to handle this? 处理此问题的最佳方法是什么?

Action chaining is the correct way. 动作链接是正确的方法。 I am using annotation so code looks like this. 我正在使用注释,因此代码如下所示。

@Action(
            value = "/loginAction",
            results = {
                    @Result(name = "car_user", type="chain", params = {"actionName", "getAllUserAction", "loginUser", "${loginUser}"}),

.................... ....................

Where loginUser is the variable having setter getter in loginAction class and the chain attion class which has actionName getAllUserAction will receive it in loginUser Variable in that class which has setter getter for the same. 其中loginUser是loginAction类中具有setter getter的变量,而具有actionName getAllUserAction的链表类将在具有setter getter的该类的loginUser Variable中接收它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM