Hibernate分頁+自定義標籤

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>

 

效果圖

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章