序言:今天在项目中使用了map存储list和普通对象,但是在jsp中显示的时候出来问题,后经查阅,终于解决,现在记录一下,以便以后查阅:
一:后台代码如下:
package cn.gov.csrc.cms.action;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
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.RoleInfo;
import cn.gov.csrc.cms.service.RoleService;
import cn.gov.csrc.cms.util.PageData;
import cn.gov.csrc.cms.util.PageNavUtil;
import com.opensymphony.xwork2.ActionContext;
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<RoleInfo>{
private static Logger logger = LoggerFactory.getLogger(RoleAction.class);
private static final long serialVersionUID = 1L;
private Integer page;
private Map<String, Object> dataMap;
private RoleService roleService;
public RoleAction() {
dataMap = new HashMap<String, Object>();
}
@Resource
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@Action(value = "RoleAction_find", results={@Result(name = "success", location = "/ucenter/sys/roleList.jsp")})
public String find(){
page = page == null ? 1 : page < 1 ? 1:page;
int pageSize = 5;
PageData<RoleInfo> pageData = this.roleService.getRoles(page,pageSize,model.getRoleName());
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;
}
}
二:前台jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../../common/include_tag.jsp"%>
<%@ include file="../../common/page_var.jsp"%>
<!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><%=pageTitle%> - 角色管理</title>
<%@ include file="../../common/page_head.jsp"%>
<script type="text/javascript">
function add(){
$.jBox("iframe:#.action", {
title: "添加角色",
width: 700,
height: 320,
buttons: {}
});
}
function edit(id){
$.jBox("iframe:#.action?id="+id, {
title: "修改角色",
width: 700,
height: 320,
buttons: {}
});
}
function delRole(id,msg){
$.jBox.confirm("确定删除 ["+msg+"] 角色吗?", "确认操作", function (v, h, f) {
if (v == 'ok'){
$.post("#.action",{id:id},function(responseText){
if(responseText==true){
$.jBox.info("操作成功,请刷新查看结果", "成功信息",{top: '20%'});
}else{
$.jBox.error("操作失败", "失败信息");
}
});
}
return true;
},{top: '40%'});
}
function changeRoleStatus(id,status){
$.jBox.confirm("确定 "+(status==0?"禁用":"启用")+" 用户吗?", "确认操作", function (v, h, f) {
if (v == 'ok'){
$.post("#.action",{id:id,status:status},function(responseText){
if(responseText==true){
$.jBox.info("操作成功,请刷新查看结果", "成功信息",{top: '20%'});
}else{
$.jBox.error("操作失败", "失败信息");
}
});
}
return true;
},{top: '40%'});
}
function roleResourceSet(roleId){
$.jBox("iframe:#.action?roleId="+roleId, {
title: "角色资源授权",
width: 700,
height: 550,
buttons: {}
});
}
$().ready(function(){
pilicat.alternately('list');
});
</script>
</head>
<body>
<div style="height: 60px; overflow: hidden">
<%@ include file="../u_top.jsp"%>
</div>
<div id="frame_side">
<%@ include file="../u_login_info.jsp"%>
<jsp:include page="../u_left_menu.jsp" flush="true">
<jsp:param name="current" value="roles" />
</jsp:include>
</div>
<div id="body_box">
<table cellpadding="0" cellspacing="0" border="0" class="icon">
<tr>
<td></td>
</tr>
</table>
<span style="text-align: left;padding-left: 10px;">系统管理 -> 角色管理 -> 角色列表</span>
<table cellpadding="0" cellspacing="0" border="0" class="table">
<tbody>
<tr>
<td valign="top" align="right">
<form action="" method="get" name="search" id="search">
<table cellpadding="0" cellspacing="5" border="0">
<tbody>
<tr>
<td><a class="button_4" href="javascript:;" οnclick="add();">添加新角色</a></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
<div class="rounded table">
<form action="" method="get" name="search_form" id="search_form">
<table width="100%" cellpadding="0" cellspacing="0" border="0"
class="box_top">
<tr>
<td class="title">角色列表</td>
<td></td>
</tr>
</table>
</form>
<form action="" method="post" name="manage" id="manage">
<table class="list td_align" cellpadding="0" cellspacing="1"
border="0">
<tr>
<td class="field_head" width="30">选择</td>
<td class="field_head">角色ID</td>
<td class="field_head">角色名称</td>
<td class="field_head">角色描述</td>
<td class="field_head">操作</td>
</tr>
<c:forEach items="${dataMap.dataList}" var="dataItem" varStatus="rowStatus">
<tr>
<td><input type="checkbox" name="cbitem" value="${dataItem.id }" /></td>
<td>${dataItem.id}</td>
<td>${dataItem.roleName}</td>
<td>${dataItem.roleDesc}</td>
<td class="operation"><a href="javascript:;"
οnclick="edit(${dataItem.id});"> <img
src="./css/images/operation/pencil.png"
title="修改角色信息" />
</a> <a href="javascript:;"
οnclick="roleResourceSet(${dataItem.id});"> <img
src="./css/images/operation/folder.png"
title="角色权限" />
</a> <a href="javascript:;"
οnclick="delRole(${dataItem.id},'${dataItem.roleName}');"> <img
src="./css/images/operation/trashcan_delete.png"
title="删除角色" />
</a></td>
</tr>
</c:forEach>
</table>
<table class="table top_line">
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="all_action"><input name="allChkbox"
id="allChkbox" type="checkbox"
οnclick="pilicat.select_all(this.form);" /></td>
<td> 全选/取消 </td>
<td class="operation"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<table class="page" cellpadding="0" cellspacing="5">
<tr>
<td>
<div id="pageNav" class="scott">
<font color="#88af3f">共${dataMap["totalCount"]} 条数据, ${dataMap["totalPage"]} 页</font>
${dataMap["pageNav"]}
</div>
</td>
</tr>
</table>
</div>
<%@ include file="../u_bottom.jsp"%>
</div>
</body>
</html>
注:主要是下面的分页显示:
<div id="pageNav" class="scott">标签:function,EL,map,jBox,value,dataMap,import,id,page From: https://blog.51cto.com/u_4679008/5883755
<font color="#88af3f">共${dataMap["totalCount"]} 条数据, ${dataMap["totalPage"]} 页</font>
${dataMap["pageNav"]}
</div>