当前位置:   article > 正文

struts2、hibernate 分页(自定义标签)_req.setattribute("cid", cid);

req.setattribute("cid", cid);

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

view plaincopy to clipboardprint?

  1. public class PagerTag extends SimpleTagSupport {
  2. private String uri;//分页要执行的action路径
  3. private Integer curpage;//当前页
  4. private Integer pagesize;//每页显示的记录数
  5. private Integer pagecount;//总页数
  6. private Integer rowcount;//总记录数
  7. public Integer getCurpage() {
  8. return curpage;
  9. }
  10. public void setCurpage(Integer curpage) {
  11. this.curpage = curpage;
  12. }
  13. public Integer getPagesize() {
  14. return pagesize;
  15. }
  16. public void setPagesize(Integer pagesize) {
  17. this.pagesize = pagesize;
  18. }
  19. public Integer getPagecount() {
  20. return pagecount;
  21. }
  22. public void setPagecount(Integer pagecount) {
  23. this.pagecount = pagecount;
  24. }
  25. public Integer getRowcount() {
  26. return rowcount;
  27. }
  28. public void setRowcount(Integer rowcount) {
  29. this.rowcount = rowcount;
  30. }
  31. public String getUri() {
  32. return uri;
  33. }
  34. public void setUri(String uri) {
  35. this.uri = uri;
  36. }
  37. //每次执行标签时会调用toTag
  38. public void doTag() throws JspException, IOException {
  39. //获得页面的输出流
  40. JspWriter out = this.getJspContext().getOut();
  41. //通过流往页面写数据
  42. out.println("<div class=/"pager/">");
  43. out.println("共" +rowcount+ "行记录,每页");
  44. out.println("<input value=/""+ pagesize + "/" size=/"2/" />条");
  45. out.println("当前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>页/共"+ pagecount + "页");
  46. out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一页</a>");
  47. if(curpage > 0) {
  48. out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一页</a>");
  49. }
  50. if(curpage < pagecount -1) {
  51. out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一页</a>");
  52. }
  53. out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一页</a>");
  54. out.println("</div>");
  55. }
  56. }
  57. public class PagerTag extends SimpleTagSupport {
  58. private String uri;//分页要执行的action路径
  59. private Integer curpage;//当前页
  60. private Integer pagesize;//每页显示的记录数
  61. private Integer pagecount;//总页数
  62. private Integer rowcount;//总记录数
  63. public Integer getCurpage() {
  64. return curpage;
  65. }
  66. public void setCurpage(Integer curpage) {
  67. this.curpage = curpage;
  68. }
  69. public Integer getPagesize() {
  70. return pagesize;
  71. }
  72. public void setPagesize(Integer pagesize) {
  73. this.pagesize = pagesize;
  74. }
  75. public Integer getPagecount() {
  76. return pagecount;
  77. }
  78. public void setPagecount(Integer pagecount) {
  79. this.pagecount = pagecount;
  80. }
  81. public Integer getRowcount() {
  82. return rowcount;
  83. }
  84. public void setRowcount(Integer rowcount) {
  85. this.rowcount = rowcount;
  86. }
  87. public String getUri() {
  88. return uri;
  89. }
  90. public void setUri(String uri) {
  91. this.uri = uri;
  92. }
  93. //每次执行标签时会调用toTag
  94. public void doTag() throws JspException, IOException {
  95. //获得页面的输出流
  96. JspWriter out = this.getJspContext().getOut();
  97. //通过流往页面写数据
  98. out.println("<div class=/"pager/">");
  99. out.println("" +rowcount+ "行记录,每页");
  100. out.println("<input value=/""+ pagesize + "/" size=/"2/" />条");
  101. out.println("当前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>页/共"+ pagecount + "");
  102. out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一页</a>");
  103. if(curpage > 0) {
  104. out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一页</a>");
  105. }
  106. if(curpage < pagecount -1) {
  107. out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一页</a>");
  108. }
  109. out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一页</a>");
  110. out.println("</div>");
  111. }
  112. }



标签的tld文件 :my.tld

view plaincopy to clipboardprint?

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <taglib xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  5. version="2.1">
  6. <tlib-version>1.1</tlib-version>
  7. <short-name>my</short-name>
  8. <!-- uri用来定位标签库 -->
  9. <uri>http://java.pojo.com/tag</uri>
  10. <!-- tag用来声明一个标签
  11. name:界面中标签的名字
  12. tag-class:标签的类名
  13. bodyContent:表示标签体的内容的形式
  14. attribute: 用来声明标签类具有的属性
  15. name:属性名
  16. required:是否一定要赋值
  17. rtexprvalue:是否允许使用el表达式
  18. -->
  19. <tag>
  20. <name>pager</name>
  21. <tag-class>com.pojo.web.tag.PagerTag</tag-class>
  22. <body-content>empty</body-content>
  23. <attribute>
  24. <name>uri</name>
  25. <required>true</required>
  26. <rtexprvalue>true</rtexprvalue>
  27. </attribute>
  28. <attribute>
  29. <name>curpage</name>
  30. <required>true</required>
  31. <rtexprvalue>true</rtexprvalue>
  32. </attribute>
  33. <attribute>
  34. <name>pagesize</name>
  35. <required>true</required>
  36. <rtexprvalue>true</rtexprvalue>
  37. </attribute>
  38. <attribute>
  39. <name>pagecount</name>
  40. <required>true</required>
  41. <rtexprvalue>true</rtexprvalue>
  42. </attribute>
  43. <attribute>
  44. <name>rowcount</name>
  45. <required>true</required>
  46. <rtexprvalue>true</rtexprvalue>
  47. </attribute>
  48. </tag>
  49. </taglib>
  50. <?xml version="1.0" encoding="UTF-8" ?>
  51. <taglib xmlns="http://java.sun.com/xml/ns/javaee"
  52. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  54. version="2.1">
  55. <tlib-version>1.1</tlib-version>
  56. <short-name>my</short-name>
  57. <!-- uri用来定位标签库 -->
  58. <uri>http://java.pojo.com/tag</uri>
  59. <!-- tag用来声明一个标签
  60. name:界面中标签的名字
  61. tag-class:标签的类名
  62. bodyContent:表示标签体的内容的形式
  63. attribute: 用来声明标签类具有的属性
  64. name:属性名
  65. required:是否一定要赋值
  66. rtexprvalue:是否允许使用el表达式
  67. -->
  68. <tag>
  69. <name>pager</name>
  70. <tag-class>com.pojo.web.tag.PagerTag</tag-class>
  71. <body-content>empty</body-content>
  72. <attribute>
  73. <name>uri</name>
  74. <required>true</required>
  75. <rtexprvalue>true</rtexprvalue>
  76. </attribute>
  77. <attribute>
  78. <name>curpage</name>
  79. <required>true</required>
  80. <rtexprvalue>true</rtexprvalue>
  81. </attribute>
  82. <attribute>
  83. <name>pagesize</name>
  84. <required>true</required>
  85. <rtexprvalue>true</rtexprvalue>
  86. </attribute>
  87. <attribute>
  88. <name>pagecount</name>
  89. <required>true</required>
  90. <rtexprvalue>true</rtexprvalue>
  91. </attribute>
  92. <attribute>
  93. <name>rowcount</name>
  94. <required>true</required>
  95. <rtexprvalue>true</rtexprvalue>
  96. </attribute>
  97. </tag>
  98. </taglib>



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

  1. view plaincopy to clipboardprint?
  2. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  3. <%@ taglib prefix="s" uri="/struts-tags" %>
  4. <%@ taglib uri="http://java.pojo.com/tag" prefix="my" %>
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  6. <html>
  7. <head>
  8. <title>管理电影档期</title>
  9. <!--
  10. <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
  11. -->
  12. <link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">
  13. <mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>
  14. </head>
  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. <table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">
  35. <tr>
  36. <td colspan="9" class="header">
  37. 电影档期管理
  38. </td>
  39. </tr>
  40. <tr>
  41. <td align="center" class="altbg1">
  42. <b>电影图片</b>
  43. </td>
  44. <td align="center" class="altbg1">
  45. <b>电影名称</b>
  46. </td>
  47. <td align="center" class="altbg1">
  48. <b>日期</b>
  49. </td>
  50. <td align="center" class="altbg1">
  51. <b>时间</b>
  52. </td>
  53. <td align="center" class="altbg1">
  54. <b>影厅</b>
  55. </td>
  56. <td align="center" class="altbg1">
  57. <b>票价</b>
  58. </td>
  59. <td align="center" class="altbg1">
  60. <b>编辑</b>
  61. </td>
  62. <td align="center" class="altbg1">
  63. <b>删除</b>
  64. </td>
  65. <td align="center" class="altbg1">
  66. <b>查看电影订票情况</b>
  67. </td>
  68. </tr>
  69. <s:iterator value="#request.list">
  70. <tr>
  71. <td align="center" class="altbg2">
  72. <img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />" width="100px" height="100px" />
  73. </td>
  74. <td class="altbg2" align="center">
  75. <s:property value="filmInfo.fname" />
  76. </td>
  77. <td class="altbg2" align="center">
  78. <s:date name="rdate" format="yyyy-MM-dd" />
  79. </td>
  80. <td class="altbg2" align="center">
  81. <s:date name="rtime" format="HH:mm" />
  82. </td>
  83. <td class="altbg2" align="center">
  84. <s:property value="cinemaInfo.cname" />
  85. </td>
  86. <td class="altbg2" align="center">
  87. <s:property value="filmInfo.price" />
  88. </td>
  89. <td class="altbg2" align="center">
  90. <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>
  91. </td>
  92. <td class="altbg2" align="center">
  93. <a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>
  94. </td>
  95. <td class="altbg2" align="center">
  96. <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>
  97. </td>
  98. </tr>
  99. </s:iterator>
  100. <tr style="font-size:12px" mce_style="font-size:12px" align="right">
  101. <th colspan="100" class="pager">
  102. <my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"
  103. curpage="${ curpage }"
  104. pagesize="${ pagesize}"
  105. pagecount="${ pagecount}"
  106. rowcount="${rowcount }"
  107. />
  108. </th>
  109. </tr>
  110. </table>
  111. </body>
  112. </html>
  113. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  114. <%@ taglib prefix="s" uri="/struts-tags" %>
  115. <%@ taglib uri="http://java.pojo.com/tag" prefix="my" %>
  116. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  117. <html>
  118. <head>
  119. <title>管理电影档期</title>
  120. <!--
  121. <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
  122. -->
  123. <link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">
  124. <mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>
  125. </head>
  126. <body>
  127. <form action="Release_findAllPaging.action" method="post" name="form1">
  128. <table cellspacing="0" cellpadding="4" width="100%" class="tableborder" id="table3">
  129. <tr>
  130. <td class="header" colspan="7">
  131. 信息查询
  132. </td>
  133. </tr>
  134. <tr align="left">
  135. <td width="52px">影片名称</td>
  136. <td width="60px"><input type="text" name="filmInfo.fname" /></td>
  137. <td width="52px">影厅名称</td>
  138. <td width="60px"><s:action name="Cinemainfo2_findAll" namespace="/" executeResult="true" ignoreContextParams="true" /></td>
  139. <td width="26px">日期</td>
  140. <td width="60px"><INPUT name="rdate" onFocus="this.select();" readonly="readonly" onClick="fPopCalendar(event,this,this);" size="20px" /></td>
  141. <td><input type="submit" value="查询"/></td>
  142. </tr>
  143. </table>
  144. </form>
  145. <table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">
  146. <tr>
  147. <td colspan="9" class="header">
  148. 电影档期管理
  149. </td>
  150. </tr>
  151. <tr>
  152. <td align="center" class="altbg1">
  153. <b>电影图片</b>
  154. </td>
  155. <td align="center" class="altbg1">
  156. <b>电影名称</b>
  157. </td>
  158. <td align="center" class="altbg1">
  159. <b>日期</b>
  160. </td>
  161. <td align="center" class="altbg1">
  162. <b>时间</b>
  163. </td>
  164. <td align="center" class="altbg1">
  165. <b>影厅</b>
  166. </td>
  167. <td align="center" class="altbg1">
  168. <b>票价</b>
  169. </td>
  170. <td align="center" class="altbg1">
  171. <b>编辑</b>
  172. </td>
  173. <td align="center" class="altbg1">
  174. <b>删除</b>
  175. </td>
  176. <td align="center" class="altbg1">
  177. <b>查看电影订票情况</b>
  178. </td>
  179. </tr>
  180. <s:iterator value="#request.list">
  181. <tr>
  182. <td align="center" class="altbg2">
  183. <img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />" width="100px" height="100px" />
  184. </td>
  185. <td class="altbg2" align="center">
  186. <s:property value="filmInfo.fname" />
  187. </td>
  188. <td class="altbg2" align="center">
  189. <s:date name="rdate" format="yyyy-MM-dd" />
  190. </td>
  191. <td class="altbg2" align="center">
  192. <s:date name="rtime" format="HH:mm" />
  193. </td>
  194. <td class="altbg2" align="center">
  195. <s:property value="cinemaInfo.cname" />
  196. </td>
  197. <td class="altbg2" align="center">
  198. <s:property value="filmInfo.price" />
  199. </td>
  200. <td class="altbg2" align="center">
  201. <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>
  202. </td>
  203. <td class="altbg2" align="center">
  204. <a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>
  205. </td>
  206. <td class="altbg2" align="center">
  207. <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>
  208. </td>
  209. </tr>
  210. </s:iterator>
  211. <tr style="font-size:12px" mce_style="font-size:12px" align="right">
  212. <th colspan="100" class="pager">
  213. <my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"
  214. curpage="${ curpage }"
  215. pagesize="${ pagesize}"
  216. pagecount="${ pagecount}"
  217. rowcount="${rowcount }"
  218. />
  219. </th>
  220. </tr>
  221. </table>
  222. </body>
  223. </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"

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jayqean/archive/2010/06/20/5681763.aspx

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/529006
推荐阅读
相关标签
  

闽ICP备14008679号