• Struts2.3,,遍历map中的list集合


    一:首先是<s:iterator></s:iterator>遍历map中的list集合,代码如下:

    1.首先是后台代码,包括定义map和给map中赋值,代码如下:

    [java] view plain copy
    1. package cn.gov.csrc.cms.action;  
    2.   
    3. import java.util.HashMap;  
    4. import java.util.List;  
    5. import java.util.Map;  
    6.   
    7. import javax.annotation.Resource;  
    8.   
    9. import org.apache.struts2.ServletActionContext;  
    10. import org.apache.struts2.convention.annotation.Action;  
    11. import org.apache.struts2.convention.annotation.ExceptionMapping;  
    12. import org.apache.struts2.convention.annotation.Result;  
    13. import org.apache.struts2.dispatcher.SessionMap;  
    14. import org.slf4j.Logger;  
    15. import org.slf4j.LoggerFactory;  
    16. import org.springframework.context.annotation.Scope;  
    17. import org.springframework.stereotype.Controller;  
    18.   
    19. import cn.gov.csrc.base.action.BaseAction;  
    20. import cn.gov.csrc.cms.model.UserInfo;  
    21. import cn.gov.csrc.cms.service.UserService;  
    22. import cn.gov.csrc.cms.util.Globals;  
    23. import cn.gov.csrc.cms.util.PageData;  
    24. import cn.gov.csrc.cms.util.PageNavUtil;  
    25.   
    26. import com.opensymphony.xwork2.ActionContext;  
    27.   
    28. @Controller  
    29. @Scope("prototype")  
    30. public class UserAction extends BaseAction<UserInfo>{  
    31.   
    32.     private static Logger logger = LoggerFactory.getLogger(UserAction.class);  
    33.       
    34.     private static final long serialVersionUID = 1L;  
    35.       
    36.     private Integer page;  
    37.       
    38.     private Map<String, Object> dataMap;  
    39.       
    40.     public UserAction(){  
    41.         dataMap = new HashMap<String, Object>();  
    42.     }  
    43.       
    44.     @Resource  
    45.     private UserService userService;  
    46.       
    47.     public Integer getId() {  
    48.         return id;  
    49.     }  
    50.   
    51.     public void setId(Integer id) {  
    52.         this.id = id;  
    53.     }  
    54.       
    55.     @Action(value = "UserAction_find", results = { @Result(name = "success", location = "/WEB-INF/admin/user/userList.jsp") })  
    56.     public String find() {  
    57.         page = page == null ? 1 : page < 1 ? 1:page;  
    58.         int pageSize = 10;  
    59.         PageData<UserInfo> pageData = this.userService.getUsers(page,pageSize,model.getUserName());  
    60.         dataMap.clear();  
    61.         dataMap.put("dataList", pageData.getPageData());  
    62.         dataMap.put("totalCount", pageData.getTotalCount());  
    63.         dataMap.put("totalPage", pageData.getTotalPage());  
    64.         dataMap.put("currentPage", page);  
    65.         dataMap.put("pageNav", PageNavUtil.getPageNavHtml(page.intValue(), pageSize, pageData.getTotalCount(), 15));  
    66.         ActionContext.getContext().getSession().put("dataMap", dataMap);  
    67.         return "success";  
    68.     }  
    69.   
    70.     public Integer getPage() {  
    71.         return page;  
    72.     }  
    73.   
    74.     public void setPage(Integer page) {  
    75.         this.page = page;  
    76.     }  
    77.       
    78.       
    79. }  

    2.其次是在前台页面接收map中list的值,这里我用的是jsp接收值,代码如下:

    [html] view plain copy
    1. <form action="./UserAction_delete.action" id="mainForm" method="post">  
    2.     <table class="table table-striped table-hover">  
    3.         <thead>  
    4.             <tr>  
    5.                 <th style=" 8px;">  
    6.                     <input type="checkbox" id="checkall" class="group-checkable" />  
    7.                 </th>  
    8.                 <th>编号</th>  
    9.                 <th>登录名</th>  
    10.                 <th>用户姓名</th>  
    11.                 <th>电话</th>  
    12.                 <th>部门</th>  
    13.                 <th>岗位</th>  
    14.                 <th>操作</th>  
    15.             </tr>  
    16.              <c:if test="${totalCount==0}">  
    17.                  <tr>  
    18.                     <td colspan="8">没有相关数据</td>  
    19.                  </tr>  
    20.              </c:if>  
    21.         </thead>  
    22.         <s:iterator value="#session.dataMap.dataList" status="user" >  
    23.         <tbody>  
    24.             <tr class="odd gradeX">  
    25.                 <td class="hidden-480"><input type="checkbox" class="checkboxes" name="ids" value="${id }" /></td>  
    26.                 <td class="hidden-480"><s:property value="#user.index+1" /></td>  
    27.                 <td class="hidden-480"><s:property value="userName" /></td>  
    28.                 <td class="hidden-480"><s:property value="realName" /></td>  
    29.                 <td class="hidden-480"><s:property value="phone" /></td>  
    30.                 <td class="hidden-480"><s:property value="dept" /></td>  
    31.                 <td class="hidden-480"><s:property value="post" /></td>  
    32.                 <td class="hidden-480">  
    33.                     <s:url id="url" action="UserAction_toUpdate" namespace="/">  
    34.                     <s:param name="id" value="id" />  
    35.                     </s:url>  
    36.                     <a class="btn mini purple thickbox" title='编辑和用户权限' href="<s:property value="#url"/>">  
    37.                         <i class="icon-edit"></i>编辑和用户权限  
    38.                     </a>  
    39.                 </td>  
    40.             </tr>  
    41.         </tbody>  
    42.         </s:iterator>  
    43.     </table>  
    44. </form>  


    二:其次是<c:forEach></c:forEach>遍历map中的list集合,代码如下:

    1.后台代码和上面的一样;

    2.前台代码如下:

    [html] view plain copy
    1. <c:forEach items="${dataMap.dataList}" var="dataItem" varStatus="rowStatus" >  
    2. <tbody>  
    3.     <tr class="odd gradeX">  
    4.         <td class="hidden-480"><input type="checkbox" class="checkboxes" name="ids" value="${dataItem.id }" /></td>  
    5.         <td class="hidden-480"><s:property value="#dataItem.index+1" /></td>  
    6.         <td class="hidden-480">${dataItem.userName}</td>  
    7.         <td class="hidden-480"><s:property value="realName" /></td>  
    8.         <td class="hidden-480"><s:property value="phone" /></td>  
    9.         <td class="hidden-480"><s:property value="dept" /></td>  
    10.         <td class="hidden-480"><s:property value="post" /></td>  
    11.         <td class="hidden-480">  
    12.             <s:url id="url" action="UserAction_toUpdate" namespace="/">  
    13.             <s:param name="id" value="id" />  
    14.             </s:url>  
    15.             <a class="btn mini purple thickbox" title='编辑和用户权限' href="<s:property value="#url"/>">  
    16.                 <i class="icon-edit"></i>编辑和用户权限  
    17.             </a>  
    18.         </td>  
    19.     </tr>  
    20. </tbody>  
    21. </c:forEach>  

  • 相关阅读:
    Python的包管理工具Pip
    [Reactive Programming] RxJS dynamic behavior
    [Reactive Programming] Using an event stream of double clicks -- buffer()
    [RxJS + AngularJS] Sync Requests with RxJS and Angular
    [Javascript] An Introduction to JSPM (JavaScript Package Manager)
    [Angular 2] 9. Replace ng-modle with #ref & events
    [React] Intro to inline styles in React components
    [AngualrJS + Webpack] Production Source Maps
    [AngularJS + Webpack] Uglifying your JavaScript
    [AngularJS + Webpack] Production Setup
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317864.html
Copyright © 2020-2023  润新知