当前位置:   article > 正文

Hibernate分页+自定义标签

hibernate page getpageindex()

Page.java

package com.hr.tangxinutil; public class Page { /** * total_record 总记录数 */ private int total;

 

/** * everyPage_size 每页记录数 */public int pageSize = 2;/** * pageIndex 页码 */private int pageIndex = 1;/** * previous_pageIndex 上一页 */@SuppressWarnings("unused")private int prePageIndex;/** * next_pageIndex 下一页 */@SuppressWarnings("unused")private int nextpageIndex;/** * 设置总记录数和每页记录 * @param total * @param pageSize */public Page(int total, int pageSize) {this.total = total;this.pageSize = pageSize;}/** * get_previous_pageIndex * * @return 获得上一页 */public int getPrePageIndex() {return this.pageIndex - 1;}/** * get_next_pageIndex * * @return 获得下一页 */public int getNextpageIndex() {return this.pageIndex + 1;}public Page() {}/** * get_total_pageSize * * @return 获得总页数 */public int getTotalPageSize() {return (int) Math.ceil(this.total / (double) this.pageSize);}/** * if_previous_page 是否有上一页 * * @return */public boolean IsPrePage() {return this.pageIndex > 1 ? true : false;}/** * if_next_page 是否有下一页 * * @return */public boolean IsNextPage() {return this.pageIndex < this.getTotalPageSize() ? true : false;}/** * 得到当前页码 * * @return */public int getPageIndex() {return pageIndex;}/** * 得到总记录数 * * @return */public int getTotal() {return total;}}

Tag.java

package com.hr.tangxinutil; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; @SuppressWarnings("serial") public class Tag extends TagSupport { private String action; public String getAction() { return action; } public void setAction(String action) { this.action = action; } @Override public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); HttpServletRequest request = (HttpServletRequest) pageContext .getRequest(); Page page = (Page) request.getAttribute("page"); try { out.println("第" + page.getPageIndex() + "页"); out.println("共" + page.getTotalPageSize() + "页" + "共" + page.getTotal() + "记录"); out.println("<a href="/" mce_href="/""" + request.getContextPath() + action + "1/">首页</a>"); out.println("<a href="/" mce_href="/""" + request.getContextPath() + action + page.getTotalPageSize() + "/">尾页</a>"); if (page.IsPrePage()) { out.println("<a href="/" mce_href="/""" + request.getContextPath() + action + page.getPrePageIndex() + "/">上页</a>"); } else { out.println("上页"); } if (page.IsNextPage()) { out.println("<a href="/" mce_href="/""" + request.getContextPath() + action + page.getNextpageIndex() + "/">下页</a>"); } else { out.println("下页"); } } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } }

 mytag.tld

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>my</short-name> <tag> <name>cutPage</name> <tag-class>com.hr.tangxinutil.PageTag</tag-class> <attribute> <name>action</name> <required>true</required> </attribute> </tag> </taglib>

 myaction.java

 package com.hr.web.struts.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.hr.biz.SysUserBiz; import com.hr.pojo.SysUser; import com.hr.tangxinutil.Page; public class UserAction extends DispatchAction { @SuppressWarnings("unused") private SysUserBiz sysUserBiz; public void setSysUserBiz(SysUserBiz sysUserBiz) { this.sysUserBiz = sysUserBiz; } /** * 查询出所有的用户 * * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward toList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int currentPage = 0; Page page = null; if (request.getAttribute("page") == null) { page = new Page(7, 2); request.setAttribute("page", page); } else { page = (Page) request.getSession().getAttribute("page"); } if (request.getParameter("index") == null) { currentPage = 1; } else { currentPage = Integer.valueOf(request.getParameter("index")); page.setPageIndex(currentPage); } List<SysUser> pageUserList = sysUserBiz.pageUser(2, currentPage); request.setAttribute("pageUserList", pageUserList); return new ActionForward( "/sys_man/jsp/operatermanager/sysoperaterman.jsp"); } }

hibernate.java

package com.hr.dao.impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.hr.pojo.SysUser; public class Hibernate extends HibernateDaoSupport { @SuppressWarnings("unchecked") public List<SysUser> findByIndex(int pageSize, int pageIndex) { Session session = super.getSession(); Query query = session.createQuery("FROM SysUser s order by s.id asc"); query.setFirstResult(pageSize * (pageIndex - 1)); query.setMaxResults(pageSize); List<SysUser> list = (List<SysUser>) query.list(); return list; } }

myJsp.jsp

<%@ taglib prefix="mytag" uri="/tld/mytag.tld"%> <%@ page language="java" pageEncoding="utf-8"%> <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%> <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%> <!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>操作员管理</title> <mce:script type='text/javascript' src="/hr_1/dwr/interface/sysUserBiz.js" mce_src="hr_1/dwr/interface/sysUserBiz.js"></mce:script> <mce:script type='text/javascript' src="/hr_1/dwr/engine.js" mce_src="hr_1/dwr/engine.js"></mce:script> <mce:script type='text/javascript' src="/hr_1/dwr/util.js" mce_src="hr_1/dwr/util.js"></mce:script> <mce:script type="text/javascript"><!-- function hidd(){ document.getElementById("addSysUser").style.display="none"; } // --></mce:script> <link rel="stylesheet" href="../css/fontform.css" mce_href="css/fontform.css" type="text/css"></link> </head> <body> <div id="container"> <div id="head" style="color: #FFF; font-weight: bold; background-color: #ccc; height: 50px;"> 你正在做的业务是:人力资源-系统设置-操作员设置 <br /> <div id="addinput" align="right"> <input type="button" value="新增操作" οnclick="toInputAddUser();" /> </div> </div> <div id="main"> <div id="userList"> <table style="border-collapse: collapse; border-color: yellow" mce_style="border-collapse: collapse; border-color: yellow" width="100%" border="1"> <thead> <tr> <th> 编号 </th> <th> 用户 </th> <th> 状态 </th> <th> 操作 </th> </tr> </thead> <tbody> <logic:present name="pageUserList"> <logic:iterate id="user" collection="${pageUserList}"> <tr align="center"> <td> ${user.id } </td> <td> ${user.name } </td> <td> <logic:equal value="0" name="user" property="userFlag"> 禁用 </logic:equal> <logic:equal value="1" name="user" property="userFlag"> 正常 </logic:equal> </td> <td> <a href="${pageContext.request.contextPath}/sysUser.do?op=lookSingleUser&uid=${user.id }">查看</a> <a href="${pageContext.request.contextPath}/sysUser.do?op=editUser&uid=${user.id }">编辑</a> </td> </tr> </logic:iterate> </logic:present> <tr align="center"> <td colspan="4" style="font-size: 11px;" mce_style="font-size: 11px;"> <mytag:cutPage action="/sysUser.do?op=toList&index="></mytag:cutPage> </td> </tr> </tbody> </table> </div> </div> <mce:script type="text/javascript"><!-- function displ(){ sysUserBiz.allRole( function getRole(data){ DWRUtil.removeAllOptions('sysuser.sysRole.roleId'); DWRUtil.addOptions('sysuser.sysRole.roleId',["<<--未分配-->>"]); DWRUtil.addOptions('sysuser.sysRole.roleId',data,'roleId','roleName'); } ); document.getElementById("addSysUser").style.display="block"; } function adduser(){ var sFeatures="dialogHeight: " + 300 + "px;dialogWidth:"+400+"px;"; window.showModalDialog("${pageContext.request.contextPath}/sys_man/jsp/main/sysman_add.jsp", "",sFeatures); } /** 转到添加用户页面 */ function toInputAddUser(){ this.window.location='${pageContext.request.contextPath}/sys_man/jsp/operatermanager/sysman_add.jsp'; } // --></mce:script> </div> </body> </html>

jsp关键代码

<%@ taglib prefix="mytag" uri="/tld/mytag.tld"%>
<mytag:cutPage action="/sysUser.do?op=toList&index="></mytag:cutPage>

 

效果图

 

 

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号