<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
总听别人说分页分页的,自己也来试试,网上查了查资料,集百家所长后又自己发挥了一下.
直接上代码:
操作数据: PageDao.java
- packagecom.enorth.lichen.youth.dao;
- importjava.util.ArrayList;
- importjava.util.List;
- importorg.hibernate.Session;
- importcom.enorth.lichen.youth.hi.HibernateUtils;
- publicclassPageDao{
- publicListpageQuery(intpageSize,intpageNow){
- Listlist=newArrayList();
- Sessionsession=null;
- try{
- session=HibernateUtils.getSession();
- session.beginTransaction();
- //得到每页显示的LevTwoInFuUser的对象的集合list
- list=session.createQuery("fromLevTwoInFuUserl2ifwherel2if.permission=201orderbyl2if.l2infid")
- .setFirstResult(pageNow*pageSize-pageSize)
- .setMaxResults(pageSize)
- .list();
- session.getTransaction().commit();
- }catch(Exceptione){
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally{
- HibernateUtils.closeSession(session);
- }
- returnlist;
- }
- publicintpageTotle(intpageSize,intpageNow){
- inti=0;
- Sessionsession=null;
- try{
- session=HibernateUtils.getSession();
- session.beginTransaction();
- //i是查询结果条数
- i=session.createQuery("fromLevTwoInFuUserl2ifwherel2if.permission=201orderbyl2if.l2infid")
- .list()
- .size();
- //用总的结果条数对每页显示的条数取余,得到总页数
- i=i%pageSize==0?i/pageSize:i/pageSize+1;
- session.getTransaction().commit();
- }catch(Exceptione){
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally{
- HibernateUtils.closeSession(session);
- }
- returni;
- }
- }
Action处理: PageQueryAction.java
- packagecom.enorth.lichen.youth.act;
- importjava.util.List;
- importjava.util.Map;
- importjavax.servlet.http.HttpServletRequest;
- importorg.apache.struts2.interceptor.ServletRequestAware;
- importorg.apache.struts2.interceptor.SessionAware;
- importcom.enorth.lichen.youth.dao.PageDao;
- importcom.opensymphony.xwork2.ActionSupport;
- publicclassPageQueryActionextendsActionSupportimplementsServletRequestAware,SessionAware{
- privateHttpServletRequestrequest;
- privateListlist;
- privateintpageNow=1;//初始化为1,默认从第一页开始显示
- privateintpageSize=3;//每页显示3条记录
- privateintpageTotle=1;//总页数
- privatePageDaopageDao=newPageDao();
- publicListgetList(){
- returnlist;
- }
- publicvoidsetList(Listlist){
- this.list=list;
- }
- publicintgetPageNow(){
- returnpageNow;
- }
- publicvoidsetPageNow(intpageNow){
- this.pageNow=pageNow;
- }
- publicintgetPageSize(){
- returnpageSize;
- }
- publicvoidsetPageSize(intpageSize){
- this.pageSize=pageSize;
- }
- publicintgetPageTotle(){
- returnpageTotle;
- }
- publicvoidsetPageTotle(intpageTotle){
- this.pageTotle=pageTotle;
- }
- @Override
- publicStringexecute()throwsException{
- //得到每页显示的对象的集合list
- list=pageDao.pageQuery(pageSize,pageNow);
- //得到总页数pageTotle
- pageTotle=pageDao.pageTotle(pageSize,pageNow);
- returnSUCCESS;
- }
- @Override
- publicvoidsetServletRequest(HttpServletRequestrequest){
- //TODOAuto-generatedmethodstub
- this.request=request;
- }
- @Override
- publicvoidsetSession(Maparg0){
- //TODOAuto-generatedmethodstub
- }
- }
查询分页页面: page.jsp
- <%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"
- +request.getServerName()+":"+request.getServerPort()
- +path+"/";
- %>
- <%@taglibprefix="s"uri="/struts-tags"%>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <SCRIPTtype="text/javascript"src="js/calendar.js"></SCRIPT>
- <basehref="<%=basePath%>">
- <title>分页</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- </head>
- <body>
- <tableborder="1">
- <tr>
- <th>
- 用户编号
- </th>
- <th>
- 真实姓名
- </th>
- <th>
- 注册时间
- </th>
- </tr>
- <!--迭代查询-->
- <s:iteratorvalue="list">
- <tr>
- <td>
- <s:propertyvalue="l2infid"/>
- </td>
- <td>
- <s:propertyvalue="realname"/>
- </td>
- <td>
- <s:propertyvalue="registime"/>
- </td>
- </tr>
- </s:iterator>
- </table>
- <!--首页-->
- <s:urlid="url_first"value="page.action">
- <s:paramname="pageNow"value="1"></s:param>
- </s:url>
- <!--上一页-->
- <s:urlid="url_pre"value="page.action">
- <s:paramname="pageNow"value="pageNow-1"></s:param>
- </s:url>
- <!--下一页-->
- <s:urlid="url_next"value="page.action">
- <s:paramname="pageNow"value="pageNow+1"></s:param>
- </s:url>
- <!--末页-->
- <s:urlid="url_last"value="page.action">
- <s:paramname="pageNow"value="pageTotle"></s:param>
- </s:url>
- <!--如果不是首页则提供首页的链接,如果是首页则不提供链接,以下类似-->
- <s:iftest="pageNow!=1">
- [<s:ahref="%{url_first}">首页</s:a>]
- </s:if>
- <s:else>
- [首页]
- </s:else>
- <s:iftest="pageNow>1">
- [<s:ahref="%{url_pre}">上一页</s:a>]
- </s:if>
- <s:else>
- [上一页]
- </s:else>
- <!--for循环,current是循环变量(org.apache.struts2.util.Counter里定义的)-->
- <s:beanname="org.apache.struts2.util.Counter"id="i">
- <s:paramname="first"value="1"/>
- <s:paramname="last"value="%{pageTotle}"/>
- <s:iterator>
- <s:iftest="pageNow+1==current">
- <fontcolor="#3f84ff"><b><s:property/></b></font>
- </s:if>
- <s:else>
- <s:ahref="page.action?pageNow=%{current-1}"><fontcolor="#2fa2ff"><s:property/></font></s:a>
- </s:else>
- </s:iterator>
- </s:bean>
- <s:iteratorvalue="list"status="status">
- <s:urlid="url"value="page.action">
- <s:paramname="pageNow"value="pageNow"/>
- <s:paramname="pageTotle"value="pageTotle"/>
- </s:url>
- </s:iterator>
- <s:iftest="pageTotle>pageNow">
- [<s:ahref="%{url_next}">下一页</s:a>]
- </s:if>
- <s:else>
- [下一页]
- </s:else>
- <s:iftest="pageTotle!=pageNow">
- [<s:ahref="%{url_last}">末页</s:a>]
- </s:if>
- <s:else>
- [末页]
- </s:else>
- </body>
- </html>