java 模糊查询 分页(自定义标签)

来源:互联网 发布:无线端有美工作图 编辑:程序博客网 时间:2024/05/01 11:48

本次的实现是采用struts2+hibernate+分页标签 实现

class :PagerTag 是用来实现自定义分页标签,继承SimpleTagSupport实现自定义标签

[java] view plaincopy
  1. public class PagerTag extends SimpleTagSupport {  
  2.       
  3.     private String uri;//分页要执行的action路径  
  4.     private Integer curpage;//当前页  
  5.     private Integer pagesize;//每页显示的记录数  
  6.     private Integer pagecount;//总页数  
  7.     private Integer rowcount;//总记录数  
  8.       
  9.       
  10.       
  11.     public Integer getCurpage() {  
  12.         return curpage;  
  13.     }  
  14.     public void setCurpage(Integer curpage) {  
  15.         this.curpage = curpage;  
  16.     }  
  17.     public Integer getPagesize() {  
  18.         return pagesize;  
  19.     }  
  20.     public void setPagesize(Integer pagesize) {  
  21.         this.pagesize = pagesize;  
  22.     }  
  23.     public Integer getPagecount() {  
  24.         return pagecount;  
  25.     }  
  26.     public void setPagecount(Integer pagecount) {  
  27.         this.pagecount = pagecount;  
  28.     }  
  29.     public Integer getRowcount() {  
  30.         return rowcount;  
  31.     }  
  32.     public void setRowcount(Integer rowcount) {  
  33.         this.rowcount = rowcount;  
  34.     }  
  35.     public String getUri() {  
  36.         return uri;  
  37.     }  
  38.     public void setUri(String uri) {  
  39.         this.uri = uri;  
  40.     }  
  41.   
  42.     //每次执行标签时会调用toTag  
  43.     public void doTag() throws JspException, IOException {  
  44.       
  45.         //获得页面的输出流  
  46.         JspWriter out  = this.getJspContext().getOut();  
  47.           
  48.           
  49.         //通过流往页面写数据       
  50.         out.println("<div class=/"pager/">");  
  51.         out.println("共" +rowcount+ "行记录,每页");  
  52.         out.println("<input value=/""+ pagesize + "/" size=/"2/" />条");  
  53.         out.println("当前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>页/共"+ pagecount + "页");  
  54.         out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一页</a>");  
  55.       
  56.         if(curpage > 0) {  
  57.             out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一页</a>");  
  58.         }  
  59.         if(curpage < pagecount -1) {  
  60.             out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一页</a>");  
  61.         }  
  62.         out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一页</a>");  
  63.         out.println("</div>");  
  64.     }  
  65.       
  66.       
  67.       
  68.   
  69. }  
  

标签的tld文件 :my.tld

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <taglib xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"  
  6.     version="2.1">  
  7.       
  8.     
  9.   <tlib-version>1.1</tlib-version>  
  10.   <short-name>my</short-name>  
  11.     
  12.   <!-- uri用来定位标签库  -->  
  13.   <uri>http://java.pojo.com/tag</uri>  
  14.   
  15.     
  16.     <!-- tag用来声明一个标签  
  17.     name:界面中标签的名字  
  18.     tag-class:标签的类名  
  19.     bodyContent:表示标签体的内容的形式  
  20.     attribute: 用来声明标签类具有的属性  
  21.         name:属性名  
  22.         required:是否一定要赋值  
  23.         rtexprvalue:是否允许使用el表达式  
  24.      -->  
  25.   <tag>  
  26.     <name>pager</name>  
  27.     <tag-class>com.pojo.web.tag.PagerTag</tag-class>  
  28.     <body-content>empty</body-content>  
  29.     <attribute>  
  30.         <name>uri</name>  
  31.         <required>true</required>  
  32.         <rtexprvalue>true</rtexprvalue>  
  33.     </attribute>  
  34.     <attribute>  
  35.         <name>curpage</name>  
  36.         <required>true</required>  
  37.         <rtexprvalue>true</rtexprvalue>  
  38.     </attribute>  
  39.     <attribute>  
  40.         <name>pagesize</name>  
  41.         <required>true</required>  
  42.         <rtexprvalue>true</rtexprvalue>  
  43.     </attribute>  
  44.     <attribute>  
  45.         <name>pagecount</name>  
  46.         <required>true</required>  
  47.         <rtexprvalue>true</rtexprvalue>  
  48.     </attribute>  
  49.     <attribute>  
  50.         <name>rowcount</name>  
  51.         <required>true</required>  
  52.         <rtexprvalue>true</rtexprvalue>  
  53.     </attribute>  
  54.   </tag>  
  55.   
  56. </taglib>  

 

模糊查询加分页的展示jsp界面

[xhtml] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%@ taglib uri="http://java.pojo.com/tag"  prefix="my" %>  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.     <title>管理电影档期</title>  
  8.     <!-- 
  9.     <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> 
  10.     -->  
  11.     <link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">  
  12.     <mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>  
  13.   </head>  
  14.     
  15. <body>  
  16. <form action="Release_findAllPaging.action" method="post" name="form1">  
  17.     <table cellspacing="0" cellpadding="4" width="100%" class="tableborder" id="table3">  
  18.         <tr>  
  19.             <td class="header" colspan="7">  
  20.                 信息查询  
  21.             </td>  
  22.         </tr>  
  23.         <tr align="left">  
  24.             <td width="52px">影片名称</td>  
  25.             <td width="60px"><input type="text" name="filmInfo.fname" /></td>  
  26.             <td width="52px">影厅名称</td>  
  27.             <td width="60px"><s:action name="Cinemainfo2_findAll" namespace="/" executeResult="true" ignoreContextParams="true" /></td>  
  28.             <td width="26px">日期</td>  
  29.             <td width="60px"><INPUT name="rdate" onFocus="this.select();" readonly="readonly" onClick="fPopCalendar(event,this,this);" size="20px"  /></td>  
  30.             <td><input type="submit" value="查询"/></td>  
  31.         </tr>  
  32.     </table>  
  33. </form>  
  34.   
  35. <table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">  
  36.     <tr>  
  37.         <td colspan="9" class="header">  
  38.             电影档期管理  
  39.         </td>  
  40.     </tr>  
  41.     <tr>                      
  42.         <td align="center" class="altbg1">  
  43.             <b>电影图片</b>  
  44.     </td>  
  45.         <td align="center" class="altbg1">  
  46.             <b>电影名称</b>  
  47.         </td>  
  48.         <td align="center" class="altbg1">  
  49.             <b>日期</b>  
  50.         </td>  
  51.         <td align="center" class="altbg1">  
  52.             <b>时间</b>  
  53.         </td>  
  54.         <td align="center" class="altbg1">  
  55.             <b>影厅</b>  
  56.         </td>               
  57.         <td align="center" class="altbg1">  
  58.             <b>票价</b>  
  59.         </td>  
  60.         <td align="center" class="altbg1">  
  61.             <b>编辑</b>  
  62.         </td>  
  63.         <td align="center" class="altbg1">  
  64.             <b>删除</b>  
  65.         </td>  
  66.         <td align="center" class="altbg1">  
  67.             <b>查看电影订票情况</b>  
  68.         </td>  
  69.     </tr>  
  70. <s:iterator value="#request.list">  
  71.     <tr>  
  72.         <td align="center" class="altbg2">  
  73.             <img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />width="100px" height="100px" />     
  74.         </td>  
  75.         <td class="altbg2" align="center">  
  76.             <s:property value="filmInfo.fname" />  
  77.         </td>  
  78.         <td class="altbg2" align="center">  
  79.             <s:date name="rdate" format="yyyy-MM-dd" />  
  80.         </td>  
  81.         <td class="altbg2" align="center">  
  82.             <s:date name="rtime" format="HH:mm" />  
  83.         </td>  
  84.         <td class="altbg2" align="center">  
  85.             <s:property value="cinemaInfo.cname" />  
  86.         </td>  
  87.         <td class="altbg2" align="center">  
  88.             <s:property value="filmInfo.price" />元  
  89.         </td>  
  90.         <td class="altbg2" align="center">  
  91.             <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>        
  92.         </td>               
  93.         <td class="altbg2" align="center">  
  94.             <a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>   
  95.         </td>  
  96.         <td class="altbg2" align="center">  
  97.             <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>   
  98.         </td>  
  99.     </tr>  
  100. </s:iterator>  
  101.     <tr style="font-size:12px" mce_style="font-size:12px" align="right">  
  102.         <th colspan="100" class="pager">  
  103.             <my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"  
  104.                 curpage="${ curpage }"  
  105.                 pagesize="${ pagesize}"  
  106.                 pagecount="${ pagecount}"  
  107.                 rowcount="${rowcount }"  
  108.             />     
  109.         </th>  
  110.     </tr>               
  111. </table>    
  112. </body>  
  113. </html>  

这里注意到的一点就是 需要导入自定义标签

还有一点 注意到自定义标签<my> uri的写法  由于我的想法是 将查询条件利用参数传递过去(参数存在request作用域下)

最重要的就是我在路径后面加上了一个temp参数 方便没有带条件的查询[条件的参数为空] (需要对比前面的分页标签类里的uri写法)

 

上面 Release_findAllPaging.action  在struts.xml里配置 不打算贴出来了 对应的类是ReleaseAction 调用的dao类为ReleaseDao

ReleaseAction类重要方法和属性(get set 方法省略...)

[java] view plaincopy
  1. //保存分页的属性  
  2.     protected Integer curpage = 0;//当前第几页  
  3.     protected Integer pagesize = 4;//每页条数  
  4.     protected Integer pagecount;//总页数  
  5.     protected Integer rowcount; //总行数  
  6.   
  7. //分页查询  
  8.     public String findAllPaging(){  
  9.         //地址栏提交中文  tomcat 配置 URIEncoding="GB2312"  
  10.         HttpServletRequest req = ServletActionContext.getRequest();//拿到请求对象  
  11.         //表单里的值  
  12.         String fname = req.getParameter("filmInfo.fname");  
  13.           
  14.         String cid = req.getParameter("cinemaInfo.cid");  
  15.         String rdate = req.getParameter("rdate");  
  16.         if(fname != null && !fname.equals("")){  
  17.             req.setAttribute("fname", fname);//不为空的话,放到request作用域下  
  18.         }  
  19.         if(cid != null && !cid.equals("")){  
  20.             req.setAttribute("cid", cid);  
  21.         }  
  22.         if(rdate != null && !rdate.equals("")){  
  23.             req.setAttribute("rdate", rdate);  
  24.         }  
  25.           
  26.         if(fname == null){  
  27.             fname = (String)req.getAttribute("fname");//等于空 从request作用域下取  
  28.         }  
  29.         if(cid == null){  
  30.             cid = (String)req.getAttribute("cid");  
  31.         }  
  32.         if(rdate == null || rdate.equals("")){  
  33.             rdate = (String)req.getAttribute("rdate");  
  34.         }  
  35.           
  36.         request.put("list",dao.findAllPaging(curpage, pagesize, fname, cid, rdate));//查询当前页的记录 放到list里  
  37.         //存入当前页  
  38.         request.put("curpage", curpage);  
  39.         //存入每页条数  
  40.         request.put("pagesize", pagesize);  
  41.           
  42.         //调用dao获得总行数  
  43.         Integer rowcount = dao.getRowCount(fname,cid,rdate);  
  44.         //算出总页数  
  45.         //101行,每页10条 10.1 使用11页  
  46.         int pagecount =(int)Math.ceil( (rowcount /( pagesize + 0.0)) );  
  47.         //总页数  
  48.         request.put("pagecount", pagecount);  
  49.         //总条数  
  50.         request.put("rowcount", rowcount);  
  51.           
  52.         return "listAction";  
  53.     }  

dao类重要方法

[java] view plaincopy
  1. //根据条件 模糊查询 总记录数  
  2.     public Integer getRowCount(String fname,String cid,String rdate){  
  3.         String strSQL = "select count(r) from Release as r where 1 = 1";  
  4.         if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常  
  5.             strSQL += " and r.filmInfo.fname like :fname";  
  6.         }  
  7.         if(cid != null && !cid.equals("")){  
  8.             strSQL += " and r.cinemaInfo.cid = :cid";  
  9.         }  
  10.           
  11.         if(rdate != null && !rdate.equals("")){  
  12.             strSQL += " and r.rdate = :rdate";  
  13.         }  
  14.         Query query = HibernateSessionFactory.getSession().createQuery(strSQL);  
  15.           
  16.         if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常  
  17.             query.setString("fname""%" + fname + "%");  
  18.         }  
  19.         if(cid != null && !cid.equals("")){  
  20.             query.setInteger("cid"new Integer(cid));  
  21.         }  
  22.           
  23.         if(rdate != null && !rdate.equals("")){  
  24.             query.setString("rdate", rdate);  
  25.         }  
  26.           
  27.         List list = query.list();  
  28.         return (Integer)list.get(0);  
  29.     }  
  30.       
  31.       
  32.     //分页模糊查询 档期  
  33.     public List findAllPaging(int curpage,int pagesize,String fname,String cid,String rdate) {  
  34.         String strSQL = "select r from Release as r where 1 = 1";  
  35.           
  36.         if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常  
  37.             strSQL += " and r.filmInfo.fname like :fname";  
  38.         }  
  39.         if(cid != null && !cid.equals("")){  
  40.             strSQL += " and r.cinemaInfo.cid = :cid";  
  41.         }  
  42.           
  43.         if(rdate != null && !rdate.equals("")){  
  44.             strSQL += " and r.rdate = :rdate";  
  45.         }  
  46.         Query query = HibernateSessionFactory.getSession().createQuery(strSQL);  
  47.         if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常  
  48.             query.setString("fname""%" + fname + "%");  
  49.         }  
  50.         if(cid != null && !cid.equals("")){  
  51.             query.setInteger("cid"new Integer(cid));  
  52.         }  
  53.           
  54.         if(rdate != null && !rdate.equals("")){  
  55.             query.setString("rdate", rdate);  
  56.         }  
  57.         query.setFirstResult(curpage*pagesize)  
  58.                 .setMaxResults(pagesize);  
  59.         List list = query.list();  
  60.         return list;  
  61.     }  

还有要注意点的是 条件是采用get方式传递 中文的问题解决 需在tomcat下配置文件 加上URIEncoding="GB2312"

0 0
原创粉丝点击