在网页的显示中,经常需要用到将查询到的信息显示在页面上,当查询到的信息较多时,需要用到分页查询,将信息分页显示,为了区分每一行,所以要设置一个隔行换色的特效。Hibernate的Query对象提供了分页查询的功能,是setFirstResult和setMaxResults用于设置查询记录的第一条记录和每一也最大记录数,然后使用query.list()方法获取该页相关信息。
下面看具体代码,首先使用Page类封装分页查询的相关信息:
package tool; import java.util.List; public class Page { private int currentPage;//当前页码 private int totalSize;//总记录数 private int totalPage;//总页数 private boolean hasPrevious;//是否有前一页 private boolean hasNext;//是否有后一页 private List list;//存放查询结果集合信息 //省略set和get方法 }
然后看dao类的具体处理方法
import java.util.List; import model.Student; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import tool.Page; public class BaseDao { private SessionFactory sessionfactory; public SessionFactory getSessionfactory() { return sessionfactory; } public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory; } @Transactional public Page query(int currentPage,int pageSize){ Page page = new Page(); Session session = sessionfactory.getCurrentSession(); String hql = "FROM Student s order by s.id"; Query query = session.createQuery(hql); List<Student> list = query.list(); int count = list.size(); //计算总页数 int totalpage = (count%pageSize==0)?(count/pageSize):(count/pageSize+1); //判断当前页码是否有上下页 if(currentPage>=totalpage){ currentPage=totalpage; page.setHasNext(false); }else{ page.setHasNext(true); } if(currentPage <=1){ currentPage = 1; page.setHasPrevious(false); }else{ page.setHasPrevious(true); } query.setFirstResult((currentPage-1)*pageSize); query.setMaxResults(pageSize); List<Student> result = query.list(); page.setCurrentPage(currentPage); page.setList(result); page.setTotalPage(totalpage); page.setTotalSize(count); return page; } }
然后就将分页查询的具体信息放到page对象中,然后再Action类中将page对象放到request中并返回到显示界面,然后再显示界面进行相应处理后实现分页显示,具体信息显示页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri = "/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>查询结果</title> </head> <body> <div align="center"> <s:if test="#request.show.list.size !=0"> <table border="1"> <tr> <td>id</td> <td>姓名</td> </tr> <s:iterator value="#request.test.list" var="tes"> <tr> <td> <s:property value="#tes.id"/> </td> <td> <s:property value="#tes.name"/> </td> </tr> </s:iterator> <tr> <td colspan="2" align="center"> <a href="test?page=1">首页</a>| <s:if test="#request.test.hasPrevious"> <a href="test?page=<s:property value='#request.test.currentPage-1'/>"> 上一页</a> </s:if> <s:if test="#request.test.hasNext"> |<a href="test?page=<s:property value='#request.test.currentPage+1'/>"> 下一页</a> </s:if> |<a href="test?page=<s:property value='#request.test.totalPage'/>"> 末页</a> 第<s:property value="#request.test.currentPage"/>页/ 共<s:property value="#request.test.totalPage"/>页 (<s:property value="#request.test.totalSize"/>条记录) </td> </tr> </table> </s:if> </div> </body> </html>
对于隔行变色可以设置<s:iterator>标签的status属性,判断状态时奇数行还是偶数行。例如:
<s:iterator value="#request.records.list" var="tr" status="status"> <s:if test="#status.even"> <tr bgcolor="yellow"> </s:if> <s:if test="#status.odd"> <tr bgcolor="blue"> </s:if> <td><s:property value="#tr.transaction_date"/></td> <td><s:property value="#tr.expense"/></td> <td><s:property value="#tr.income"/></td> <td><s:property value="#tr.balance"/></td> <td><s:property value="#tr.transaction_type"/></td> <td><s:property value="#tr.remark"/></td> </tr> </s:iterator>
时间: 2024-09-30 09:20:56