webwork+hibernate+spring進行分頁

package bu3.tools;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.opensymphony.xwork.ActionContext;

public class Pager extends HibernateDaoSupport {
	int currentPage = 1;
	//首頁
	int firstPage;
	//上一頁
	int PriviousPage;
	//下一頁
	int nextPage;
	//最后一頁
	int LastPage;
	//頁面大小
	int pageSize = 2;
	//數據的總數量
	int totalSize;
	//總頁面數
	int totalPage;
	//是否有第一頁
	boolean hasFirst;
	//是否有下一頁
	boolean hasNext;
	//是否有最后一頁
	boolean hasLast;
	//是否有上一頁
	boolean hasPrivious;
	//查詢數據的sql
	public String hql = "";

	public String getHql() {
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}


	public int getCurrentPage() {
		//request中的currentPage為翻頁鏈接的參數。判斷是翻頁,還是提交頁面。
		Object[] o = (Object[]) ActionContext.getContext().getParameters().get(
				"currentPage");
		if (o != null){
			currentPage = Integer.parseInt(o[0].toString());
		}else{
			this.getTotalSize();//提交頁面時計算總數
currentPage=1;
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {

		this.currentPage = currentPage;
	}

	public boolean isHasFirst() {
		//只有一頁時不顯示首頁
		if (this.getCurrentPage() < 2) {
			return false;
		} else {
			return true;
		}
	}

	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}

	public boolean isHasLast() {
		//最后一頁時不顯示尾頁
		if (currentPage == this.getTotalPage())
			return false;
		return true;
	}

	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}

	public boolean isHasNext() {
		
		if (this.getCurrentPage() >= this.getTotalPage())
			return false;
		return true;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrivious() {
		if (isHasFirst())
			return true;
		return false;
	}

	public void setHasPrivious(boolean hasPrivious) {
		this.hasPrivious = hasPrivious;
	}

	public int getPageSize() {

		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		//計算總頁數
		totalPage = totalSize/ pageSize;
		if (totalSize % pageSize != 0)
			totalPage++;
		this.setTotalPage(totalPage);
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalSize() {
		//總數量
		Session session = this.getSession();
		totalSize = session.createQuery(hql).list().size();
		session.close();
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getFirstPage() {
		int i = 0;
		if (this.isHasFirst())
			i = 1;
		return i;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getLastPage() {

		return totalPage;
	}

	public void setLastPage(int lastPage) {
		LastPage = lastPage;
	}

	public int getNextPage() {

		int i = 0;
		if (this.getCurrentPage() < this.totalPage) {
			i = getCurrentPage() + 1;
		} else {
			i = totalPage;
		}
		return i;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getPriviousPage() {

		int i = 0;
		if (this.getCurrentPage() > 1) {
			i = this.getCurrentPage() - 1;
		} else {
			i = 1;
		}
		return i;
	}

	public List getDataList(){
		//獲取本頁數據
		Session sess = getSession();
		Query query = sess.createQuery(hql);
		query.setFirstResult((getCurrentPage() - 1) * pageSize);
		query.setMaxResults(pageSize);
		List list = query.list();
		sess.close();
		return list;
	}
	public void setPriviousPage(int priviousPage) {
		PriviousPage = priviousPage;
	}
}

Pager類記錄頁面信息,用于和jsp頁面進行交互!
Action類如下。主要是給出sql,把page信息放到request中,提供給頁面使用。
package bu3.actionImpl;

import java.util.List;
import java.util.Map;

import bu3.action.OqcBadMessageAction;
import bu3.service.OqcBadService;
import bu3.tools.Pager;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

public class OqcBadMessageActionImpl extends ActionSupport implements
		OqcBadMessageAction {

	public Pager page;

	public String qcNo = "";
//qcMessage頁面列表數據
	public List qcMessage;

	public String qcDetial() {

		String hql = "from bu3.vo.VQcFromRepairInfo vf where vf.id.qcNo='"
				+ qcNo + "'";
		page.setHql(hql);
		page.setPageSize(3);
		Map map = (Map) ActionContext.getContext().get("request");
		map.put("page", page);
		qcMessage=page.getDataList();
		return SUCCESS;
	}

	public String getQcNo() {
		return qcNo;
	}

	public void setQcNo(String qcNo) {
		this.qcNo = qcNo;
	}

	public List getQcMessage() {
		return qcMessage;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

}

applicationContext中設定page和action的依賴關系
<bean id="OqcBadMessageAction"
		class="bu3.actionImpl.OqcBadMessageActionImpl" abstract="false"
		lazy-init="default" autowire="autodetect"
		dependency-check="default">
		<property name="page">
			<ref bean="page" />
		</property>
	</bean>

xwork.xml
<action name="OqcBadDetialMessage" class="OqcBadMessageAction"
			method="qcDetial">
			<result name="success">oqcBadDetial.jsp</result>

		</action>

最后jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="BIG5"%>
<%@ taglib prefix="ww" uri="/webwork"%>

<html>
	<head>
		<link rel=stylesheet href="../styles/main.css" type="text/css">
	</head>

	<body>
		<br>
		<table border>
			<ww:iterator value="qcMessage" status="away">
				<tr>
					<td>
						<ww:property value="#away.count" />
					</td>
					<td>
						<ww:property value="id.qcNo" />
					</td>
					<td>
						<ww:property value="id.partNo" />
					</td>
					<td>
						<ww:property value="id.workOrder" />
					</td>
					<td>
						<ww:property value="id.serialNumber" />
					</td>
					<td>
						<ww:property value="id.defectDesc" />
					</td>
					<td>
						<ww:property value="id.reasonDesc" />
					</td>
					<td>
						<ww:property value="id.processName" />
					</td>
			</ww:iterator>
		</table>
		<center>
			<ww:if test="qcMessage.size()<1">
				<br>
				<br>
				<br>
				<br>

				<h6>
					<ww:property value="#request.qcNo" />
					have no bad message!
				</h6>
			</ww:if>
		</center>
		<br>
//根據request中Pager類的信息生成鏈接
		<ww:if test="#request.page.hasFirst">
			<a href="OqcBadDetialMessage.action?currentPage=1">首頁</a>
		</ww:if>
		<ww:if test="#request.page.hasPrivious">
			<a
				href="OqcBadDetialMessage.action?currentPage=<ww:property value="#request.page.PriviousPage"/>">上一頁</a>
		</ww:if>
		<ww:if test="#request.page.hasNext">
			<a
				href="OqcBadDetialMessage.action?currentPage=<ww:property value="#request.page.nextPage"/>">下一頁</a>
		</ww:if>
		<ww:if test="#request.page.hasLast">
			<a
				href="OqcBadDetialMessage.action?currentPage=<ww:property value="#request.page.totalPage"/>">尾頁</a>
		</ww:if>
		<br>
		<br>
		<ww:if test="qcMessage.size>0">
			共<ww:property value="#request.page.totalPage"/>頁,當前<ww:property value="#request.page.currentPage"/>頁
		</ww:if>
	</body>
</html>

你可能感兴趣的:(spring,sql,Hibernate,jsp,Webwork)