自定义标签分页源代码---第三章

五;一个简单的小demo演示分页效果,

1,新建PageControllerTag.java

   Copy上面第一步的代码:

2,编写标签文件luojspage.tld

Copy上面第二步的代码:

3,调用分页

(1) 查询数据的方法(这里用的hibernatehql语句进行查询)

 public List getPage(final String sql, int pageId, final int pageSize) {

 

              System.out.println("yo  wcomecome you 1");

              final int firstId = (pageId - 1) * pageSize;

 

              Query query = this.getHibernateTemplate().getSessionFactory()

                            .getCurrentSession().createQuery(sql);

              query.setFirstResult(firstId);

              query.setMaxResults(pageSize);

              List list = query.list();

              return list;

       }

 

(2) actionservlet中调用查询数据的方法

int pageSize = 3; //设置每页要显示的条数

              request.setAttribute("pageSize", new Integer(pageSize));

             

              String pageNo = request.getParameter("pageNo");// 获取当前页号

              if (pageNo == null)

                     pageNo = "1";            

              int pagenum = Integer.parseInt(pageNo); //

              // 利用这两参数进行数据查询

              List listUser = user.getPage("from User", pagenum, pageSize);

        //获取中的记录数

              int size = user.serverSearchById(0, 0).size();

              request.setAttribute("total", size + ""); //总条数 

              request.setAttribute("listUser", listUser);

              return mapping.findForward("succ");

 

4,页面代码

<%@ page language="java" pageEncoding="gbk"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib uri="http://www.luojs.cn" prefix="luojs"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>  

    <title>分页测试</title>

  </head>

  <body>

    <h2>This is my Struts page. </h2><br>

   <h4>Worcome ${name }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="longin.do?path=toadds">add User</a></h4> <hr/>

   <table width="650px" border="1" background="#fffff" align="center" cellpadding="0" cellspacing="0" bordercolor="#cccfff" bgcolor="#ffffff">

  <caption>

    User info

  </caption>

  <tr bgcolor="#99ffcc">

    <th scope="col">id</th>

    <th scope="col">UserName</th>

    <th scope="col">PassWord</th>

    <th scope="col">Update</th>

    <th scope="col">Del</th>

  </tr>

  <c:forEach items="${listUser}" var="user">

  <tr>

    <td>${user.id }</td>

    <td>${user.username}</td>

    <td>${user.password }</td>

    <td>&nbsp;<a href="longin.do?path=toupdate&id=${user.id }">update</a>&nbsp;</td>

    <td>&nbsp;<a href="longin.do?path=del&id=${user.id }" onClick="return(confirm(' delete this is user'));">delete</a>&nbsp;</td>

  </tr>

 </c:forEach>

</table>

<table border="0" align="center"><tr><td>

<luojs:pageTag pageSize="${requestScope.pageSize}" gotoURI="longin.do?path=index"/>

</td></tr></table>

  </body>

</html>

 

5,页面效果:

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