我有一个Struts 2页面,其中有2个DisplayTag
表,每个表都有一个List<T>
。这个List<T>
填充在代码隐藏中,并且可以长达几千个条目。在页面上,我可以选择其中一个表的行,然后按一个按钮将它们从一个表移动到另一个表。在最后按下另一个按钮以保存数据库中某个表的内容之前,可以多次执行此操作。我想通过AJAX在动作中完成这两件事。
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=">>" type="button" id="removeUsersButton" /> <br />
<br />
<%-- The 'Refresh' button. Used to reload the issueTrackingSystemEditDialog.jsp --%>
<input value="<<" 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:
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;
}
我遇到的问题是,当我将元素从一个List移动到另一个List时,我需要访问当前在表中使用的列表。因为Struts2操作是无状态的,所以我需要在两个操作之间保留这些列表。
<s: hidden />
块中,但无法轻易转换回来。List<HashMap>
而不是List<T>
。处理此问题的最佳方法是什么?
答案 0 :(得分:0)
行动链是正确的方法。我正在使用注释,所以代码看起来像这样。
@Action(
value = "/loginAction",
results = {
@Result(name = "car_user", type="chain", params = {"actionName", "getAllUserAction", "loginUser", "${loginUser}"}),
....................
其中loginUser是在loginAction类中具有setter getter的变量,而具有actionName getAllUserAction的chain attion类将在该类中具有setter getter的loginUser变量中接收它。