java-用SSH框架實現將查詢的資訊分頁顯示

Alice茄子發表於2016-11-16

以查詢學生表的資訊為例

1.   分頁類

package com.tool;

public class pageShow {
//此類用於分頁
	private int pageNow;//當前頁
	private int totalSize;//總條數
	private int totalPage;//總頁數
	private int pageSize=10;//每頁顯示條數
	private boolean hasPre;//是否有上一頁
	private  boolean hasNext;//是否有下一頁
	private  boolean hasFirst;//是否有首頁
	private  boolean hasLast;//是否有尾頁
	public pageShow(int pageNow,int totalSize){
		//構造方法
		this.setPageNow(pageNow);
		this.setTotalSize(totalSize);	
	}
	public pageShow(int pageNow,int totalSize,int pageSize){//可動態改變每頁條數
		//構造方法
		this.setPageNow(pageNow);
		this.setTotalSize(totalSize);	
		this.pageSize=pageSize;
	}
	public void setPageNow(int pageNow) {//得到當前頁
		this.pageNow = pageNow;
	}
	public int getPageNow() {
		return pageNow;
	}
	public void setTotalSize(int totalSize) {//總條數 
		this.totalSize = totalSize;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setPageSize(int pageSize) {//每頁顯示條數
		this.pageSize = pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public int getTotalPage() {//總頁數 = 總條數/每頁顯示條數
		totalPage=this.getTotalSize()/this.getPageSize();
		if(this.getTotalSize()%this.getPageSize()!=0){
			totalPage++; //若餘數為不0 則要多加一頁
		}
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public boolean isHasPre() {//是否有上一頁   除第一頁以外都有上一頁  說明有首頁的就有上一頁
		if(this.isHasFirst()){
			return true;
		}
		else return false;
	}
	public void setHasPre(boolean hasPre) {
		this.hasPre = hasPre;
	}
	public boolean isHasNext() {//是否有下一頁   除最後一頁以外都有下一頁  說明有尾頁的就有下一頁
		if(this.isHasLast()){
			return true;
		}
		else return false;
	}
	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
	public boolean isHasFirst() {//是否有首頁 除第一頁以外都有首頁
		if(this.pageNow==1)//是第一頁就沒有首頁
			return false;
		else
			return true;
	}
	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}
	public boolean isHasLast() {//是否有尾頁  除最後一頁以外都有尾頁
		if(pageNow==this.getTotalPage()){//最後一頁
			return false;	
		}else return true;
	}
	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}
	
}
2.持久層DAO程式碼

a.BaseDao類(用於獲取運算元據庫的session物件)

 package com.Dao.Imp;
 
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 
 public class BaseDao
 {
   private SessionFactory sessionFactory;//要設定注入
 
   public Session getSession()
   {
    return this.sessionFactory.openSession();//得到session物件
   }
   public SessionFactory getSessionFactory() {
     return this.sessionFactory;
   }
 
   public void setSessionFactory(SessionFactory sessionFactory) {
     this.sessionFactory = sessionFactory;
   }
 }

b.XsDaoImp類(運算元據庫)
package com.Dao.Imp;

import com.Dao.XsDAO;
import com.vo.Xsb;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class XsDaoImp extends BaseDao implements XsDAO {
	public List getAllXs(int pageNow, int pageSize){// 查詢所有學生 分頁頁
		Session session = getSession();//父類方法
		String hql = "from Xsb";
		Query query = session.createQuery(hql);//執行查詢操作
		query.setFirstResult((pageNow - 1) * pageSize);
		query.setMaxResults(pageSize);
		List XsList = query.list();
		session.close();
		session=null;
		if (XsList.size() > 0) {
			return XsList;
		}

		return null;
	}
	public int findXsSize() {// 所有學生個數
		Session session = getSession();
		String hql = "from Xsb";
		int size = session.createQuery(hql).list().size();
		session.close();
		return size;
	}

	
	

}



3.業務層service(呼叫dao層的方法)

package com.service.Imp;

import com.Dao.XsDAO;
import com.service.XsService;
import com.vo.Xsb;
import java.util.List;

public class XsServiceImp implements XsService {
	private XsDAO xsdao;
	
	public List getAllXs(int page, int pageSize)// 查詢所有學生 分頁
	{
		List XsList = this.xsdao.getAllXs(page, pageSize);
		return XsList;
	}

	public int findXsSize() {// 所有學生個數
		// TODO Auto-generated method stub
		return this.xsdao.findXsSize();
	}

}

4.action類

package com.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.XsService;
import com.service.ZyService;
import com.tool.pageShow;
import com.vo.Xsb;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

public class XsAction extends ActionSupport {
	private XsService xsService;// 要設定注入
	private Xsb xs;
	private int pageNow = 1;// 動態改變 頁面取得
	private int pageSize = 5;// 固定不變

	public String getAllXs() {
		List XsList = this.xsService.getAllXs(pageNow, pageSize);
		if (XsList.size() > 0) {// 學生列表
			Map session = ActionContext.getContext().getSession();
			Map request = (Map) ActionContext.getContext().get("request");
			session.put("XsList", XsList);// 儲存在session
			pageShow page = new pageShow(pageNow, xsService.findXsSize(), pageSize);// 例項化分頁物件
			request.put("page", page);// 儲存到request
			return "success";
		}
		return "error";
	}

	

	public void setXsService(XsService xsService) {//設定注入service
		this.xsService = xsService;
	}


	public int getPageSize() {
		return pageSize;
	}

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

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}
}


5.表示層:jsp頁面程式碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'LookXsInfo.jsp' starting page</title>
  </head>
  
  <body bgcolor="#D9DFAA" >
  <table border="1">
  	<tr>
  		<td>學號</td>
  		<td>姓名</td>
  		<td>性別</td>
  		<td>專業</td>
  		<td>出生時間</td>
  		<td>總學分</td>
  	</tr>
  	<s:iterator value="#session.XsList" id="xs">
         <!-- 獲得儲存在session中的學生資訊列表XsList-->
  	<tr>
  		<td><s:property value="#xs.xh"></s:property></td>
  		<td><s:property value="#xs.xm"></s:property></td>
  		<td><s:if test="#xs.xb==1">男</s:if><s:else >女</s:else></td>
  		<td><s:property value="#xs.zy.zym"></s:property></td>
  		<td><s:date name="#xs.cssj" format="yyyy-MM-dd"></s:date></td>
  		<td><s:property value="#xs.zxf"></s:property></td>
        </tr>
  	</s:iterator>
  	<s:set name="page" value="#request.page"></s:set>
  	  	<tr>
  		<td colspan="9">
  		當前是第<s:property value="#page.pageNow"/>頁,共<s:property value="#page.totalPage"/>頁
  		<s:if test="#page.hasFirst">
  			<a href="allXs?pageNow=1"  target="main">首頁</a>
  		</s:if>
  		<s:if test="#page.hasPre">
  			<a href="allXs?pageNow=<s:property value="#page.pageNow-1"/> "  target="main">上一頁</a>
  		</s:if>
  		<s:if test="#page.hasNext">
			 <a  href="allXs?pageNow=<s:property value="#page.pageNow+1" />" target="main">下一頁</a>
		</s:if>
		 <s:if test="#page.hasLast">
  			<a href="allXs?pageNow=<s:property value="#page.totalPage"/>"  target="main">尾頁</a>
  		</s:if>
	 </td>
  </tr>
  </table>
  </body>
</html>

6.配置檔案

applicationContext.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
		</property>
		<property name="url"
			value="jdbc:sqlserver://localhost:1433;databaseName=xscj">
		</property>
		<property name="username" value="sa"></property>
		<property name="password" value="root"></property>
	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/vo/Cjb.hbm.xml</value>
				<value>com/vo/Dlb.hbm.xml</value>
				<value>com/vo/Kcb.hbm.xml</value>
				<value>com/vo/XsKcb.hbm.xml</value>
				<value>com/vo/Xsb.hbm.xml</value>
				<value>com/vo/Zyb.hbm.xml</value>
			</list>
		</property>
	</bean>

	<bean id="baseDao" class="com.Dao.Imp.BaseDao">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>


	<bean id="XsDao" class="com.Dao.Imp.XsDaoImp" parent="baseDao">
	</bean>

	<bean id="XsService" class="com.service.Imp.XsServiceImp">
		<property name="xsdao" ref="XsDao"></property>
	</bean>

	<bean id="allXsAction" class="com.action.XsAction">
		<property name="xsService" ref="XsService"></property>
	</bean>
	
</beans>




相關文章