- public class PagerTag extends SimpleTagSupport {
- private String uri;//分頁要執行的action路徑
- private Integer curpage;//當前頁
- private Integer pagesize;//每頁顯示的記錄數
- private Integer pagecount;//總頁數
- private Integer rowcount;//總記錄數
- public Integer getCurpage() {
- return curpage;
- }
- public void setCurpage(Integer curpage) {
- this.curpage = curpage;
- }
- public Integer getPagesize() {
- return pagesize;
- }
- public void setPagesize(Integer pagesize) {
- this.pagesize = pagesize;
- }
- public Integer getPagecount() {
- return pagecount;
- }
- public void setPagecount(Integer pagecount) {
- this.pagecount = pagecount;
- }
- public Integer getRowcount() {
- return rowcount;
- }
- public void setRowcount(Integer rowcount) {
- this.rowcount = rowcount;
- }
- public String getUri() {
- return uri;
- }
- public void setUri(String uri) {
- this.uri = uri;
- }
- //每次執行標簽時會調用toTag
- public void doTag() throws JspException, IOException {
- //獲得頁面的輸出流
- JspWriter out = this.getJspContext().getOut();
- //通過流往頁面寫數據
- out.println("<div class=/"pager/">");
- out.println("共" +rowcount+ "行記錄,每頁");
- out.println("<input value=/""+ pagesize + "/" size=/"2/" />條");
- out.println("當前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>頁/共"+ pagecount + "頁");
- out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一頁</a>");
- if(curpage > 0) {
- out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一頁</a>");
- }
- if(curpage < pagecount -1) {
- out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一頁</a>");
- }
- out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一頁</a>");
- out.println("</div>");
- }
- }
標簽的tld文件 :my.tld
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglib xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
- version="2.1">
- <tlib-version>1.1</tlib-version>
- <short-name>my</short-name>
- <!-- uri用來定位標簽庫 -->
- <uri>http://java.pojo.com/tag</uri>
- <!-- tag用來聲明一個標簽
- name:界面中標簽的名字
- tag-class:標簽的類名
- bodyContent:表示標簽體的內容的形式
- attribute: 用來聲明標簽類具有的屬性
- name:屬性名
- required:是否一定要賦值
- rtexprvalue:是否允許使用el表達式
- -->
- <tag>
- <name>pager</name>
- <tag-class>com.pojo.web.tag.PagerTag</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>uri</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>curpage</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>pagesize</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>pagecount</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>rowcount</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
模糊查詢加分頁的展示jsp界面
- <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%@ taglib uri="http://java.pojo.com/tag" prefix="my" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>管理電影檔期</title>
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- <link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">
- <mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>
- </head>
- <body>
- <form action="Release_findAllPaging.action" method="post" name="form1">
- <table cellspacing="0" cellpadding="4" width="100%" class="tableborder" id="table3">
- <tr>
- <td class="header" colspan="7">
- 信息查詢
- </td>
- </tr>
- <tr align="left">
- <td width="52px">影片名稱</td>
- <td width="60px"><input type="text" name="filmInfo.fname" /></td>
- <td width="52px">影廳名稱</td>
- <td width="60px"><s:action name="Cinemainfo2_findAll" namespace="/" executeResult="true" ignoreContextParams="true" /></td>
- <td width="26px">日期</td>
- <td width="60px"><INPUT name="rdate" onFocus="this.select();" readonly="readonly" onClick="fPopCalendar(event,this,this);" size="20px" /></td>
- <td><input type="submit" value="查詢"/></td>
- </tr>
- </table>
- </form>
- <table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">
- <tr>
- <td colspan="9" class="header">
- 電影檔期管理
- </td>
- </tr>
- <tr>
- <td align="center" class="altbg1">
- <b>電影圖片</b>
- </td>
- <td align="center" class="altbg1">
- <b>電影名稱</b>
- </td>
- <td align="center" class="altbg1">
- <b>日期</b>
- </td>
- <td align="center" class="altbg1">
- <b>時間</b>
- </td>
- <td align="center" class="altbg1">
- <b>影廳</b>
- </td>
- <td align="center" class="altbg1">
- <b>票價</b>
- </td>
- <td align="center" class="altbg1">
- <b>編輯</b>
- </td>
- <td align="center" class="altbg1">
- <b>刪除</b>
- </td>
- <td align="center" class="altbg1">
- <b>查看電影訂票情況</b>
- </td>
- </tr>
- <s:iterator value="#request.list">
- <tr>
- <td align="center" class="altbg2">
- <img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />" width="100px" height="100px" />
- </td>
- <td class="altbg2" align="center">
- <s:property value="filmInfo.fname" />
- </td>
- <td class="altbg2" align="center">
- <s:date name="rdate" format="yyyy-MM-dd" />
- </td>
- <td class="altbg2" align="center">
- <s:date name="rtime" format="HH:mm" />
- </td>
- <td class="altbg2" align="center">
- <s:property value="cinemaInfo.cname" />
- </td>
- <td class="altbg2" align="center">
- <s:property value="filmInfo.price" />元
- </td>
- <td class="altbg2" align="center">
- <a href="javascript:if(confirm('確實要刪除嗎?'))window.location='Release_delete.action?rid=<s:property value=" mce_href="javascript:if(confirm('確實要刪除嗎?'))window.location='Release_delete.action?rid=<s:property value="rid" ></a>'">刪除</a>
- </td>
- <td class="altbg2" align="center">
- <a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>
- </td>
- <td class="altbg2" align="center">
- <a href="SaleAction_getSaleByCondition.action?cid=<s:property value=" mce_href="SaleAction_getSaleByCondition.action?cid=<s:property value="cinemaInfo.cid"></a>&fid=<s:property value="filmInfo.fid"/>&rdate=<s:date name="rdate" format="yyyy-MM-dd"/>&rtime=<s:property value="rtime"/>">查看</a>
- </td>
- </tr>
- </s:iterator>
- <tr style="font-size:12px" mce_style="font-size:12px" align="right">
- <th colspan="100" class="pager">
- <my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"
- curpage="${ curpage }"
- pagesize="${ pagesize}"
- pagecount="${ pagecount}"
- rowcount="${rowcount }"
- />
- </th>
- </tr>
- </table>
- </body>
- </html>
這里注意到的一點就是 需要導入自定義標簽
還有一點 注意到自定義標簽<my> uri的寫法 由於我的想法是 將查詢條件利用參數傳遞過去(參數存在request作用域下)
最重要的就是我在路徑后面加上了一個temp參數 方便沒有帶條件的查詢[條件的參數為空] (需要對比前面的分頁標簽類里的uri寫法)
上面 Release_findAllPaging.action 在struts.xml里配置 不打算貼出來了 對應的類是ReleaseAction 調用的dao類為ReleaseDao
ReleaseAction類重要方法和屬性(get set 方法省略...)
- //保存分頁的屬性
- protected Integer curpage = 0;//當前第幾頁
- protected Integer pagesize = 4;//每頁條數
- protected Integer pagecount;//總頁數
- protected Integer rowcount; //總行數
- //分頁查詢
- public String findAllPaging(){
- //地址欄提交中文 tomcat 配置 URIEncoding="GB2312"
- HttpServletRequest req = ServletActionContext.getRequest();//拿到請求對象
- //表單里的值
- String fname = req.getParameter("filmInfo.fname");
- String cid = req.getParameter("cinemaInfo.cid");
- String rdate = req.getParameter("rdate");
- if(fname != null && !fname.equals("")){
- req.setAttribute("fname", fname);//不為空的話,放到request作用域下
- }
- if(cid != null && !cid.equals("")){
- req.setAttribute("cid", cid);
- }
- if(rdate != null && !rdate.equals("")){
- req.setAttribute("rdate", rdate);
- }
- if(fname == null){
- fname = (String)req.getAttribute("fname");//等於空 從request作用域下取
- }
- if(cid == null){
- cid = (String)req.getAttribute("cid");
- }
- if(rdate == null || rdate.equals("")){
- rdate = (String)req.getAttribute("rdate");
- }
- request.put("list",dao.findAllPaging(curpage, pagesize, fname, cid, rdate));//查詢當前頁的記錄 放到list里
- //存入當前頁
- request.put("curpage", curpage);
- //存入每頁條數
- request.put("pagesize", pagesize);
- //調用dao獲得總行數
- Integer rowcount = dao.getRowCount(fname,cid,rdate);
- //算出總頁數
- //101行,每頁10條 10.1 使用11頁
- int pagecount =(int)Math.ceil( (rowcount /( pagesize + 0.0)) );
- //總頁數
- request.put("pagecount", pagecount);
- //總條數
- request.put("rowcount", rowcount);
- return "listAction";
- }
dao類重要方法
- //根據條件 模糊查詢 總記錄數
- public Integer getRowCount(String fname,String cid,String rdate){
- String strSQL = "select count(r) from Release as r where 1 = 1";
- if(fname != null && !fname.equals("")){//如果equals在前面的話,容易報 nullpoint 異常
- strSQL += " and r.filmInfo.fname like :fname";
- }
- if(cid != null && !cid.equals("")){
- strSQL += " and r.cinemaInfo.cid = :cid";
- }
- if(rdate != null && !rdate.equals("")){
- strSQL += " and r.rdate = :rdate";
- }
- Query query = HibernateSessionFactory.getSession().createQuery(strSQL);
- if(fname != null && !fname.equals("")){//如果equals在前面的話,容易報 nullpoint 異常
- query.setString("fname", "%" + fname + "%");
- }
- if(cid != null && !cid.equals("")){
- query.setInteger("cid", new Integer(cid));
- }
- if(rdate != null && !rdate.equals("")){
- query.setString("rdate", rdate);
- }
- List list = query.list();
- return (Integer)list.get(0);
- }
- //分頁模糊查詢 檔期
- public List findAllPaging(int curpage,int pagesize,String fname,String cid,String rdate) {
- String strSQL = "select r from Release as r where 1 = 1";
- if(fname != null && !fname.equals("")){//如果equals在前面的話,容易報 nullpoint 異常
- strSQL += " and r.filmInfo.fname like :fname";
- }
- if(cid != null && !cid.equals("")){
- strSQL += " and r.cinemaInfo.cid = :cid";
- }
- if(rdate != null && !rdate.equals("")){
- strSQL += " and r.rdate = :rdate";
- }
- Query query = HibernateSessionFactory.getSession().createQuery(strSQL);
- if(fname != null && !fname.equals("")){//如果equals在前面的話,容易報 nullpoint 異常
- query.setString("fname", "%" + fname + "%");
- }
- if(cid != null && !cid.equals("")){
- query.setInteger("cid", new Integer(cid));
- }
- if(rdate != null && !rdate.equals("")){
- query.setString("rdate", rdate);
- }
- query.setFirstResult(curpage*pagesize)
- .setMaxResults(pagesize);
- List list = query.list();
- return list;
- }

還有要注意點的是 條件是采用get方式傳遞 中文的問題解決 需在tomcat下配置文件 加上URIEncoding="GB2312"