1. 程式人生 > >實現查詢分頁以及隔行變色

實現查詢分頁以及隔行變色

在網頁的顯示中,經常需要用到將查詢到的資訊顯示在頁面上,當查詢到的資訊較多時,需要用到分頁查詢,將資訊分頁顯示,為了區分每一行,所以要設定一個隔行換色的特效。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>