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

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

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

package cn.gov.csrc.cms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.SessionMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.gov.csrc.base.action.BaseAction;
import cn.gov.csrc.cms.model.UserInfo;
import cn.gov.csrc.cms.service.UserService;
import cn.gov.csrc.cms.util.Globals;
import cn.gov.csrc.cms.util.PageData;
import cn.gov.csrc.cms.util.PageNavUtil;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<UserInfo>{

	private static Logger logger = LoggerFactory.getLogger(UserAction.class);
	
	private static final long serialVersionUID = 1L;
	
	private Integer page;
	
	private Map<String, Object> dataMap;
	
	public UserAction(){
		dataMap = new HashMap<String, Object>();
	}
	
	@Resource
	private UserService userService;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	@Action(value = "UserAction_find", results = { @Result(name = "success", location = "/WEB-INF/admin/user/userList.jsp") })
	public String find() {
		page = page == null ? 1 : page < 1 ? 1:page;
		int pageSize = 10;
		PageData<UserInfo> pageData = this.userService.getUsers(page,pageSize,model.getUserName());
		dataMap.clear();
		dataMap.put("dataList", pageData.getPageData());
		dataMap.put("totalCount", pageData.getTotalCount());
		dataMap.put("totalPage", pageData.getTotalPage());
		dataMap.put("currentPage", page);
		dataMap.put("pageNav", PageNavUtil.getPageNavHtml(page.intValue(), pageSize, pageData.getTotalCount(), 15));
		ActionContext.getContext().getSession().put("dataMap", dataMap);
		return "success";
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}
	
	
}

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

<form action="./UserAction_delete.action" id="mainForm" method="post">
	<table class="table table-striped table-hover">
		<thead>
			<tr>
				<th style="width: 8px;">
					<input type="checkbox" id="checkall" class="group-checkable" />
				</th>
				<th>编号</th>
				<th>登录名</th>
				<th>用户姓名</th>
				<th>电话</th>
				<th>部门</th>
				<th>岗位</th>
				<th>操作</th>
			</tr>
			 <c:if test="${totalCount==0}">
				 <tr>
					<td colspan="8">没有相关数据</td>
				 </tr>
			 </c:if>
		</thead>
		<s:iterator value="#session.dataMap.dataList" status="user" >
		<tbody>
			<tr class="odd gradeX">
				<td class="hidden-480"><input type="checkbox" class="checkboxes" name="ids" value="${id }" /></td>
				<td class="hidden-480"><s:property value="#user.index+1" /></td>
				<td class="hidden-480"><s:property value="userName" /></td>
				<td class="hidden-480"><s:property value="realName" /></td>
				<td class="hidden-480"><s:property value="phone" /></td>
				<td class="hidden-480"><s:property value="dept" /></td>
				<td class="hidden-480"><s:property value="post" /></td>
				<td class="hidden-480">
					<s:url id="url" action="UserAction_toUpdate" namespace="/">
					<s:param name="id" value="id" />
					</s:url>
					<a class="btn mini purple thickbox" title='编辑和用户权限' href="<s:property value="#url"/>">
						<i class="icon-edit"></i>编辑和用户权限
					</a>
				</td>
			</tr>
		</tbody>
		</s:iterator>
	</table>
</form>


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

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

2.前台代码如下:

<c:forEach items="${dataMap.dataList}" var="dataItem" varStatus="rowStatus" >
<tbody>
	<tr class="odd gradeX">
		<td class="hidden-480"><input type="checkbox" class="checkboxes" name="ids" value="${dataItem.id }" /></td>
		<td class="hidden-480"><s:property value="#dataItem.index+1" /></td>
		<td class="hidden-480">${dataItem.userName}</td>
		<td class="hidden-480"><s:property value="realName" /></td>
		<td class="hidden-480"><s:property value="phone" /></td>
		<td class="hidden-480"><s:property value="dept" /></td>
		<td class="hidden-480"><s:property value="post" /></td>
		<td class="hidden-480">
			<s:url id="url" action="UserAction_toUpdate" namespace="/">
			<s:param name="id" value="id" />
			</s:url>
			<a class="btn mini purple thickbox" title='编辑和用户权限' href="<s:property value="#url"/>">
				<i class="icon-edit"></i>编辑和用户权限
			</a>
		</td>
	</tr>
</tbody>
</c:forEach>


你可能感兴趣的:(iterator)