Dao層代碼:
1、數據層接口代碼
package cn.cdi.util.page;
import java.util.List;
public interface MemberDao {
/**
* 分頁查詢
* @param hql 查詢的條件
* @param offset 開始記錄
* @param length 一次查詢幾條記錄
* @return
*/
public List queryForPage(final String hql,final int offset,final int length);
/**
* 查詢所有記錄數
* @param hql 查詢的條件
* @return 總記錄數
*/
public int getAllRowCount(String hql);
}
2、實現接口的代碼
package cn.cdi.util.page;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
/**
* 分頁查詢
* @param hql 查詢的條件
* @param offset 開始記錄
* @param length 一次查詢幾條記錄
* @return
*/
public List queryForPage(final String hql,final int offset,final int length){
List list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}
/**
* 查詢所有記錄數
* @return 總記錄數
*/
public int getAllRowCount(String hql){
return getHibernateTemplate().find(hql).size();
}
}
業務邏輯層代碼
1、業務邏輯層接口代碼
package cn.cdi.util.page;
public interface MemberService {
/**
* 分頁查詢
* @param pageSize 每頁顯示幾條
* @param currentPage 當前第幾頁
* @return 封閉了分頁信息(包括記錄集list)的Bean
*/
public PageBean queryForPage(int pageSize,int currentPage,String ModelBean);
}
2、業務邏輯層實現接口代碼
package cn.cdi.util.page;
import java.util.List;
public class MemberServiceImpl implements MemberService{
//通過applicationContext.xml配置文件注入MemberDao的值
private MemberDao memberDao;
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
/**
* 分頁查詢
* @param currentPage 當前第幾頁
* @param pageSize 每頁大小
* @return 封閉了分頁信息(包括記錄集list)的Bean
*/
public PageBean queryForPage(int pageSize,int page,String hql){
//final String hql = "from "+ModelBean; //查詢語句
int allRow = memberDao.getAllRowCount(hql); //總記錄數
int totalPage = PageBean.countTotalPage(pageSize, allRow); //總頁數
final int offset = PageBean.countOffset(pageSize, page); //當前頁開始記錄
final int length = pageSize; //每頁記錄數
final int currentPage = PageBean.countCurrentPage(page);
List list = memberDao.queryForPage(hql,offset, length); //"一頁"的記錄
//把分頁信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
return pageBean;
}
}
bean層代碼
package cn.cdi.util.page;
import java.util.ArrayList;
import java.util.List;
import cn.cdi.importData.entity.ImportTask;
public class PageBean {
@SuppressWarnings("rawtypes")
private List list = new ArrayList(); //要返回的某一頁的記錄列表
private int allRow; //總記錄數
private int totalPage; //總頁數
private int currentPage; //當前頁
private int pageSize; //每頁記錄數
private boolean isFirstPage; //是否為第一頁
private boolean isLastPage; //是否為最后一頁
private boolean hasPreviousPage; //是否有前一頁
private boolean hasNextPage; //是否有下一頁
@SuppressWarnings("rawtypes")
public List getList() {
return list;
}
@SuppressWarnings("rawtypes")
public void setList(List list) {
this.list = list;
}
public int getAllRow() {
return allRow;
}
public void setAllRow(int allRow) {
this.allRow = allRow;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/** *//**
* 初始化分頁信息
*/
public void init(){
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}
/** *//**
* 以下判斷頁的信息,只需getter方法(is方法)即可
* @return
*/
public boolean isFirstPage() {
return currentPage == 1; // 如是當前頁是第1頁
}
public boolean isLastPage() {
return currentPage == totalPage; //如果當前頁是最后一頁
}
public boolean isHasPreviousPage() {
return currentPage != 1; //只要當前頁不是第1頁
}
public boolean isHasNextPage() {
return currentPage != totalPage; //只要當前頁不是最后1頁
}
/** *//**
* 計算總頁數,靜態方法,供外部直接通過類名調用
* @param pageSize 每頁記錄數
* @param allRow 總記錄數
* @return 總頁數
*/
public static int countTotalPage(final int pageSize,final int allRow){
int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
return totalPage==0?1:totalPage;
}
/** *//**
* 計算當前頁開始記錄
* @param pageSize 每頁記錄數
* @param currentPage 當前第幾頁
* @return 當前頁開始記錄號
*/
public static int countOffset(final int pageSize,final int currentPage){
final int offset = pageSize*(currentPage-1);
return offset;
}
/** *//**
* 計算當前頁,若為0或者請求的URL中沒有"?page=",則用1代替
* @param page 傳入的參數(可能為空,即0,則返回1)
* @return 當前頁
*/
public static int countCurrentPage(int page){
final int curPage = (page==0?1:page);
return curPage;
}
}
分頁代碼的使用
1、業務邏輯層獲取封裝數據代碼實例(注這里需要利用spring的Ioc 將memberService注入到你業務邏輯層里面去)
/**
* 獲取日志信息
* @param displayedCount //從第幾條開始取
* @param pageDisplayCount //每頁顯示數
* @return 每頁的日志信息
*/
public PageBean getLogInfos(int displayedCount,int pageDisplayCount) {
String hql = "";
hql = "from LogInfo";
return this.packImportTaskList(memberService.queryForPage(pageDisplayCount, displayedCount, hql));
}
/**
* 封裝導入任務列表
*
* @param pageBean
* @return
*/
@SuppressWarnings("rawtypes")
public PageBean packImportTaskList(PageBean pageBean) {
List list = pageBean.getList();
List<HashMap<String, Object>> newlist = new ArrayList<HashMap<String, Object>>();
if (list != null)
for (int i = 0; i < list.size(); i++) {
LogInfo it = (LogInfo) list.get(i);
HashMap<String, Object> map = new HashMap<String, Object>();
String getTime = it.getUpdateDataTime();
getTime=getTime.substring(0, 4)+"-"+getTime.substring(4, 6)+"-"+getTime.substring(6, 8)+" "+getTime.substring(8, 10)+":"+getTime.substring(10, 12)+":"+
getTime.substring(12, 14);
map.put("updateDataTime",getTime);//更新數據日期
map.put("errorFilePath", it.getErrorFilePath());//錯誤數據文件的存放路徑
map.put("operatorNum", it.getOperatorNum());//操作員賬號
map.put("pckageFileSize", it.getPckageFileSize());//文件包大小(MB)
map.put("updateFileName", it.getUpdateFileName());//更新的文件名
map.put("updateQuantity", it.getUpdateQuantity());//更新數量(書目數據數量,館藏數據量)
map.put("updateState", it.getUpdateState());//更新狀態:成功,失敗
newlist.add(map);
}
pageBean.setList(newlist);
return pageBean;
}
2、在Action中的使用
//第幾頁
private int page = 1;
//包含分頁信息的bean
private PageBean pageBean;
/**
* 日志分頁顯示(每頁10條)
* @return
*/
public String showLogInfo() {
this.pageBean = importDataBusiness.getLogInfos(page, 10);
return "showLogInfo";
}
頁面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<#include "../meta.ftl">
<title>查看覆蓋日志</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="JavaScript" type="text/javascript" src="${request.contextPath}/js/shade.js"></script>
<script language="JavaScript" type="text/javascript">
function submitForm(maxpage){
var page = document.getElementById("page").value;
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(page)){
alert("請輸入正整數字!");
} else if(page > maxpage){
alert("跳轉頁碼不能超過總頁數!");
}else if(page < 1){
alert("跳轉頁碼不能小與1!");
}else{
document.getElementById("searchImportTask").submit();
}
}
function checkDel(){
var str = "/%E6%B1%A4%E5%A7%86%E7%8C%AB";alert(str);
var str2 = decodeURIComponent(str);alert(str2);
document.location.href("/downloads/湯姆貓.zip")
var url = document.location.href;
var name=""
if (url.indexOf("=")>0)
{
name = url.substring(url.indexOf("=")+1,url.length)
}
alert(url);
var qq = document.getelementbyid("jiema");
alert(qq);
}
</script>
</head>
<body>
<!--引入頭部 -->
<#include "../head.ftl">
<!-- 內容 -->
<div id="content">
<div id="allmain">
<form action="../importData/searchLogInfo.action" method="post" id="searchImportTask">
<h2>查看覆蓋日志</h2>
<TABLE cellpadding="0" cellspacing="1" class="t2" style=" margin-top:10px">
<tr class="a1">
<th width="15%">更新數據日期</th>
<th width="15%">操作員賬號</th>
<th width="15%">更新的文件名</th>
<th width="10%">文件包大小(MB)</th>
<th width="10%">更新數量</th>
<th width="10%">更新狀態</th>
<th width="25%">錯誤數據文件的存放路徑</th>
</tr>
<#list pageBean.list as logInfo>
<tr <#if (logInfo_index%2 != 0) >class="a1"</#if>>
<td align="center">${logInfo.updateDataTime!}</td>
<td align="center">${logInfo.operatorNum!}</td>
<td align="center">${logInfo.updateFileName!}</td>
<td align="center">${logInfo.pckageFileSize!}</td>
<td align="center">${logInfo.updateQuantity!}</td>
<td align="center"><#if logInfo.updateState=="0"><span class="red">失敗</span><#elseif logInfo.updateState == "1"><span class="green">成功</span></#if></td>
<td align="center">${logInfo.errorFilePath!}</td>
</tr>
</#list>
</table>
<style>
.result{ text-align:center; padding-top:10px}
.red{ color:#CE490F}
a{ color:#656565}
a:hover{ color:#CE490F}
.input_blur{ width:35px; height:14px}
</style>
<p class="result">
共 <b class="red">${pageBean.allRow!}</b> 條 共 <b class="red">${pageBean.totalPage!}</b> 頁 第 <b class="red">${pageBean.currentPage!}</b> 頁
<#if ("${pageBean.currentPage!}" == "1")>首頁 上一頁
<#else>
<a href="searchLogInfo.action?page=1">首頁</a>
<a href="searchLogInfo.action?page=${pageBean.currentPage-1!}">上一頁</a>
</#if>
<#if ("${pageBean.currentPage!}" == "${pageBean.totalPage!}")>下一頁 末頁
<#else>
<a href="searchLogInfo.action?page=${pageBean.currentPage+1!}">下一頁</a>
<a href="searchLogInfo.action?page=${pageBean.totalPage!}">末頁</a>
</#if>
<input name="page" id="page" type="text" class="input_blur" />
<a href="javascript:submitForm(${pageBean.totalPage!});" target="_self" class="go" >GO</a></p>
</form>
</div>
</div>
<!--頁尾 -->
<#include "../foot.ftl">
</body>
</html>
