利用Pager-Taglib实现机构管理的分页处理
- 重点理解Pager-Taglib的分页原理
- 为什么需要PagerModel对象
引用
1、拷贝pager-taglib.jar包
2、在JSP页面中使用taglib指令引入pager-taglib标签库
3、使用pager-taglib标签库进行分页处理
pg:pager【这个标签用来设置分页的总体参数】重要参数说明:
url:分页的链接根地址,pager标签会在这个链接的基础上附加分页参数
items:总记录数,pager标签正是根据这个值来计算分页参数的
maxPageItems:每页显示的行数,默认为10
maxIndexPages:在循环输出页码的时候,最大输出多少个页码,默认是10
pg:first【第一页的标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 首页第一行的索引值
lastItem - 首页最后一行的索引值
pg:pre【上一页标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 前页第一行的索引值
lastItem - 前页最后一行的索引值
pg:next【下一页标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 下页第一行的索引值
lastItem - 下页最后一行的索引值
pg:last重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 尾页第一行的索引值
lastItem - 尾页最后一行的索引值
pg:pages【这个标签用来循环输出页码信息】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - pageNumber这个页码指定的那一页的第一行的索引值
lastItem - pageNumber这个页码指定的那一页的最后一行的索引值
package com.bjsxt.oa;
import java.util.List;
public class PagerModel {
private List datas;
private int total;
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
package com.bjsxt.oa.web.actions;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.manager.OrgManager;
import com.bjsxt.oa.model.Organization;
import com.bjsxt.oa.web.forms.OrgActionForm;
public class OrgAction extends DispatchAction {
private OrgManager orgManager;
/**
* 进入主界面
*/
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//
OrgActionForm oaf = (OrgActionForm)form;
int parentId = oaf.getParentId();
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (Exception ignore) {
}
int pagesize = 10;
PagerModel pm = orgManager.searchOrgs(parentId,offset,pagesize);
request.setAttribute("pm", pm);
int ppid = 0;
if(parentId != 0){
Organization parent = orgManager.findOrg(parentId);
if(parent.getParent() != null){
ppid = parent.getParent().getId();
}
}
request.setAttribute("ppid", ppid);
return mapping.findForward("index");
}
/**
* 打开添加界面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward addInput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("add_input");
}
//添加机构信息
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
OrgActionForm oaf = (OrgActionForm)form;
Organization org = new Organization();
BeanUtils.copyProperties(org, oaf);
orgManager.addOrg(org, oaf.getParentId());
return mapping.findForward("pub_add_success");
}
public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
OrgActionForm oaf = (OrgActionForm)form;
int id = oaf.getId();
// try{
orgManager.delOrg(id);
// }catch(Exception e){
// ActionMessages msgs = new ActionMessages();
//
// ActionMessage msg = new ActionMessage()
//
// msgs.add("m", msg);
//传输的是普通消息
// request.setAttribute(Globals.MESSAGES_KEY, msgs);
// saveMessages(request, msgs);
//传输的是错误消息
// request.setAttribute(Globals.ERROR_KEY, msgs);
// saveErrors(request, msgs);
//
// return mapping.findForward("org_exception");
// }
return mapping.findForward("pub_del_success");
}
public ActionForward updateInput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("update_input");
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("pub_update_success");
}
public void setOrgManager(OrgManager orgManager) {
this.orgManager = orgManager;
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Pager-taglib分页处理演示</title>
</head>
<body>
<pg:pager items="1001" maxPageItems="33" maxIndexPages="20" export="currentPageNumber=pageNumber">
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl}">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl}">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl}">下页</a>
</pg:next>
<pg:last>
<a href="${pageUrl}">尾页</a>
</pg:last>
</pg:pager>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@include file="/common/common.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<link href="style/oa.css" rel="stylesheet" type="text/css">
<script language="javascript" src="script/public.js"></script>
<title>人员管理</title>
</head>
<BODY bgColor=#dee7ff leftMargin=0 background="" topMargin=0 marginheight="0" marginwidth="0">
<center>
<TABLE width="778" border=0 cellPadding=0 cellSpacing=0 borderColor=#ffffff bgColor=#dee7ff style="FONT-SIZE: 10pt">
<TBODY>
<TR height=35>
<TD align=middle width=20 background=images/title_left.gif
bgColor=#dee7ff></TD>
<TD align=middle width=120 background=images/title_left.gif
bgColor=#dee7ff><FONT color=#f7f7f7> 机构管理<font color="#FFFFFF"> </font></FONT> </TD>
<TD align=middle width=11 background=images/title_middle.gif
bgColor=#dee7ff><FONT color=#f7f7f7> </FONT> </TD>
<TD align=middle background=images/title_right.gif
bgColor=#dee7ff><FONT color=#f7f7f7> </FONT> </TD>
</TR>
</TBODY>
</TABLE>
<TABLE width="778" border=0 align=center cellPadding=0 cellSpacing=0 borderColor=#ffffff style="FONT-SIZE: 10pt">
<TBODY>
<TR>
<TD width="82%" height=14 align=right vAlign=center noWrap>
</TD>
<TD width="18%" align=right vAlign=center noWrap> </TD>
</TR>
<TR>
<TD height=14 align=right vAlign=center noWrap>
<!-- 在这里插入查询表单 -->
</TD>
<TD height=14 align="left" vAlign=center noWrap>
<%
/**
* 在这里定义“添加”,“查询”等按钮
* <input type="image" name="find" value="find" src="images/cz.gif">
*
* <a href="#" onClick="BeginOut('document.do?method=addInput','470')">
* <img src="images/addpic.gif" border=0 align=absMiddle style="CURSOR: hand"></a>
*/
%>
<a href="#" onClick="openWin('org.do?method=addInput&parentId=${orgForm.parentId }','addperson',600,200);">添加机构信息</a>
<a href="org.do?parentId=${ppid }">返回</a>
</TD>
</TR>
<TR>
<TD height=28 colspan="2" align=right vAlign=center noWrap background=images/list_middle.jpg>
<!-- 可以在这里插入分页导航条 -->
</TD>
</TR>
</TBODY>
</TABLE>
<table width="778" border="0" cellPadding="0" cellSpacing="1" bgcolor="#6386d6">
<!-- 列表标题栏 -->
<tr bgcolor="#EFF3F7" class="TableBody1">
<td width="5%" height="37" align="center"><b>序号</b></td>
<td width="18%" height="37" align="center"><B>机构名称</B></td>
<td width="18%" height="37" align="center"><b>机构编号</b></td>
<td width="18%" height="37" align="center"><b>父机构名称</b></td>
<td width="5%" height="37" align="center"><b>操作</b></td>
</tr>
<!-- 列表数据栏 -->
<c:if test="${!empty pm.datas}">
<c:forEach items="${pm.datas }" var="org">
<tr bgcolor="#EFF3F7" class="TableBody1" onMouseOver="this.bgColor = '#DEE7FF';" onMouseOut="this.bgColor='#EFF3F7';">
<td align="center" vAlign="center">${org.id }</td>
<td align="center" vAlign="center"><a href="org.do?parentId=${org.id }">${org.name }</a></td>
<td align="center" vAlign="center">${org.sn }</td>
<td align="center" vAlign="center">${org.parent.name }</td>
<td align="center" vAlign="center">
<a href="#" onClick="del('org.do?method=del&id=${org.id }')">删除</a>
</td>
</tr>
</c:forEach>
</c:if>
<!-- 在列表数据为空的时候,要显示的提示信息 -->
<c:if test="${empty pm.datas}">
<tr>
<td colspan="7" align="center" bgcolor="#EFF3F7" class="TableBody1" onMouseOver="this.bgColor = '#DEE7FF';" onMouseOut="this.bgColor='#EFF3F7';">
没有找到相应的记录
</td>
</tr>
</c:if>
</table>
<TABLE width="778" border=0 align=center cellPadding=0 cellSpacing=0 borderColor=#ffffff style="FONT-SIZE: 10pt">
<TBODY>
<TR>
<TD height=28 align=right vAlign=center noWrap background=images/list_middle.jpg>
<!-- 可以在这里插入分页导航条 -->
<pg:pager url="org.do" items="${pm.total}" export="currentPageNumber=pageNumber">
<pg:param name="parentId"/>
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl}">前页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl}">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl}">下页</a>
</pg:next>
<pg:last>
<a href="${pageUrl}">尾页</a>
</pg:last>
</pg:pager>
</TD>
</TR>
</TBODY>
</TABLE>
</center>
</body>
</html>
package com.bjsxt.oa.manager.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.manager.OrgManager;
import com.bjsxt.oa.manager.SystemException;
import com.bjsxt.oa.model.Organization;
public class OrgManagerImpl extends HibernateDaoSupport implements OrgManager {
public void addOrg(Organization org, int parentId) {
if(parentId != 0){
org.setParent(findOrg(parentId));
}
getHibernateTemplate().save(org);
//自动生成机构编号
org.setSn(
(org.getParent() == null ? "" : org.getParent().getSn() + "_")
+ org.getId()
);
getHibernateTemplate().update(org);
}
public void delOrg(int orgId) {
Organization org = findOrg(orgId);
//判断子机构列表是否为空
if(org.getChildren().size() > 0){
//throw new RuntimeException("存在子机构信息,不允许删除");
// SystemException se = new SystemException("");
// se.setKey();
// se.setValues(..);
throw new SystemException("errors.org.hassuborg",new Object[]{org.getName(),org.getChildren().size()},"存在子机构信息,不允许删除");
}
//判断人员是否非空
String hql = "select count(*) from Person p where p.org.id = ?";
Long personSize = (Long)getSession().createQuery(hql).setParameter(0, orgId).uniqueResult();
if(personSize > 0){
throw new RuntimeException("机构下面有人员信息,不允许删除");
}
getHibernateTemplate().delete(org);
}
public Organization findOrg(int orgId) {
return (Organization)getHibernateTemplate().load(Organization.class, orgId);
}
public PagerModel searchOrgs(int parentId,int offset,int pagesize) {
//查询总记录数
String selectCountHql = "select count(*) from Organization o where o.parent is null";
if(parentId != 0){
selectCountHql = "select count(*) from Organization o where o.parent.id = "+parentId;
}
int total = ((Long)getSession().createQuery(selectCountHql).uniqueResult()).intValue();
//查询当前页的数据
String hql = "select o from Organization o where o.parent is null";
if(parentId != 0){
hql = "select o from Organization o where o.parent.id = "+parentId;
}
List datas = getSession().createQuery(hql)
.setFirstResult(offset)
.setMaxResults(pagesize)
.list();
PagerModel pm = new PagerModel();
pm.setDatas(datas);
pm.setTotal(total);
return pm;
}
public void updateOrg(Organization org, int parentId) {
if(parentId != 0){
org.setParent(findOrg(parentId));
}
getHibernateTemplate().update(org);
}
}