?? pageaction.java
字號:
package com.booksearch.action;
/************************************************************
FileName: PageAction.java
Author: fengguang
Date:11/16/08
Description: 對內存中的結果鏈表進行處理,獲得分頁工具欄,以便顯示
Class List: PageAction
***********************************************************/
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.booksearch.dao.BookLoadDao;
import com.booksearch.dao.BookSaveDao;
import com.booksearch.dao.DailyLoadDao;
import com.booksearch.dao.DailySaveDao;
import com.booksearch.orm.Book;
import com.booksearch.service.process.PageService;
import com.booksearch.service.process.ProcessService;
import com.booksearch.util.SessionBean;
/**
* Class:PageAction
* Description: 對內存中的結果鏈表進行處理,獲得分頁工具欄,以便顯示
* extens:Action
* @author feng guang
* @since 11/16/08
*/
public class PageAction extends Action {
/*總記錄數*/
private long totalRecords = 0;
/*當前頁數*/
private int currPage;
/*本頁的起始記錄索引*/
private long startPos = 0;
/*本頁的結束記錄索引*/
private long endPos = 0;
private ArrayList<Book> arrayDis = new ArrayList<Book>();
private ProcessService processService;
private PageService pageService;
private SessionBean sessionBean;
private BookLoadDao bookLoadDao;
public void setProcessService(ProcessService processService) {
this.processService = processService;
}
public void setPageService(PageService pageService) {
this.pageService = pageService;
}
public void setSessionBean(SessionBean sessionBean) {
this.sessionBean = sessionBean;
}
public void setBookLoadDao(BookLoadDao bookLoadDao) {
this.bookLoadDao = bookLoadDao;
}
/**
* Function: execute
* Description: 對內存中的結果鏈表進行處理,獲得分頁工具欄,以便顯示
* Calls: processService.startMultithread(),pageService:setCurrentPageNo(),
* setPageCount(),getCurrentPageEndRecord(),setLastPage(),getPageToolBar()
* Called By: no
* @param mapping as ActionMapping,form as ActionForm request as HttpServletRequest,response as HttpServletResponse
* @return ActionForward
* @throws no
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/*取得當前頁數參數*/
String currPageStr = request.getParameter("page");
/*取得排序方式參數*/
String rankKind = request.getParameter("rankkind");
if(null != rankKind&&!"".equals(rankKind))
this.sessionBean.setRankKind(rankKind);
/*本頁開始時間*/
long currpageStartTime = System.currentTimeMillis();
/*設置當前頁*/
if (currPageStr != null && !currPageStr.equals("")) {
currPage = Integer.parseInt(currPageStr);
}else {
// 如果不存在 傳入的參數 則置為第一頁
currPage = 1;
}
/*不是從數據庫中檢索出的數據的時候*/
if(this.sessionBean.isFromDatabase() == false){
/*如果不是第一頁,則要判斷結果鏈表中的數據是否夠下一頁顯示,如果不夠,則去源網站抽取下頁的內容*/
if(currPage!=1){
if((this.sessionBean.getBookListSize() - (currPage * 10)) < 10&&this.sessionBean.getBookListSize()>(currPage - 1)*10){
HashMap<String,String> tempMap = this.sessionBean.getNextUrl();
if(null != tempMap){
this.sessionBean.setHasupdate(true);
this.processService.startMultithread(tempMap, sessionBean);
}
}else if(this.sessionBean.getBookListSize()<=(currPage - 1)*10){
this.sessionBean.setHasupdate(true);
do{
if(this.sessionBean.getBookListSize() > (currPage-1) * 10
||System.currentTimeMillis() - currpageStartTime > 60000)
break;
HashMap<String,String> tempMap = this.sessionBean.getNextUrl();
if(null != tempMap){
this.processService.startMultithread(tempMap, sessionBean);
}else
Thread.currentThread().sleep(2000);
}while(true);
}
}
/*如果結果鏈表中的數據不夠當前頁顯示,則讓主線程sleep 100毫秒,
*直到夠顯示本頁,如果超過60秒中還不夠,則中止循環*/
while((this.sessionBean.getBookListSize() < currPage * 10)){
Thread.currentThread().sleep(500);
if (System.currentTimeMillis() - currpageStartTime > 30000) {
break;
}
}
// 取得總記錄數
//totalRecords = this.sessionBean.getBookListSize();
totalRecords = this.sessionBean.getRecordNum();
/*如果是從數據庫檢索的結果,則去取當前頁的記錄*/
}else{
if(!"advanced".equals(this.sessionBean.getSearchKind())){
String temKeyword = null;
if(this.sessionBean.getKeyword().indexOf("/")!= -1)
temKeyword = this.sessionBean.getKeyword().substring(0, this.sessionBean.getKeyword().indexOf("/"));
else
temKeyword = this.sessionBean.getKeyword();
/*從數據庫中取出當前頁的記錄*/
this.arrayDis = this.bookLoadDao.loadBook(temKeyword,this.sessionBean.getSearchKind(), currPage,this.sessionBean.getRankKind());
}else
this.arrayDis = this.bookLoadDao.loadAdvancedBook(this.sessionBean.getKeyword(), currPage,this.sessionBean.getRankKind());
// 取得總記錄數
totalRecords = this.sessionBean.getRecordNum();
}
System.out.println(">>"+totalRecords);
/*算出最大頁數*/
long pageCountIndex=0;
if(totalRecords==0){
pageCountIndex=1;
}else{
if(totalRecords%10!=0){
pageCountIndex =totalRecords/10+1;
}else{
pageCountIndex=totalRecords/10;
}
}
/*調用頁數處理類成員函數,設置當前頁數*/
this.pageService.setCurrentPageNo(currPage);
/*調用頁數處理類成員函數,設置總共頁數*/
this.pageService.setPageCount(pageCountIndex);
/*每一頁的起始記錄索引*/
startPos = this.pageService.getCurrentPageStartRecord();
/*每一頁結束記錄索引的設置*/
if(this.sessionBean.getBookListSize()>0){
if (this.pageService.getCurrentPageEndRecord() >= (totalRecords - 1)
||this.sessionBean.getBookListSize()<this.pageService.getCurrentPageEndRecord()) {
this.pageService.setLastPage(true);
endPos = this.sessionBean.getBookListSize() - 1;
}else {
endPos = this.pageService.getCurrentPageEndRecord();
}
}else{
if (this.pageService.getCurrentPageEndRecord() >= (totalRecords - 1)) {
this.pageService.setLastPage(true);
endPos = this.sessionBean.getBookListSize() - 1;
}else {
endPos = this.pageService.getCurrentPageEndRecord();
}
}
if(this.sessionBean.isFromDatabase() == false&&startPos<endPos){
/*從靜態結果鏈中取出當前頁要顯示的記錄*/
arrayDis = this.sessionBean.gettempList((int)startPos, (int)endPos);
}
// for(int k = 0;k<arrayDis.size();k++){
// Book temBook = arrayDis.get(k);
// System.out.println(temBook.getBookAuthor() + ">>" + temBook.getBookFixPrice() + ">>"
// + temBook.getBookImage() + ">>" + temBook.getBookISBN() + ">>"
// + temBook.getBookName() + ">>" + temBook.getBookProspectus() + ">>"
// + temBook.getBookPublisher() + ">>" + temBook.getBookPublishTime());
// }
/*取得分頁工具欄*/
String pagetool = this.pageService.getPageToolBar();
/*如果有爬取線程在執行則去啟動添加數據線程*/
// if(!this.sessionBean.isThreadShutDown()){
//
// new BookSaveThread(this.sessionBean, this.bookSaveDao, this.dailySaveDao).start();
// }
String temKeyword = "";
if("advanced".equals(this.sessionBean.getSearchKind())){
if(this.sessionBean.getKeyword().indexOf("null") != -1){
temKeyword = this.sessionBean.getKeyword();
temKeyword = temKeyword.replace("null+", "");
temKeyword = temKeyword.replace("+null", "");
temKeyword = temKeyword.replace("%20", "+");
}else temKeyword = this.sessionBean.getKeyword().replace("%20", "+");
}else{
StringTokenizer st = new StringTokenizer(this.sessionBean.getKeyword(), "/");
while(st.hasMoreElements()){
temKeyword = st.nextToken();
break;
}
}
if(this.currPage == 1){
DecimalFormat df=(DecimalFormat)DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
String endTime = df.format((System.currentTimeMillis() - (Long)request.getSession().getAttribute("beginTime"))/1000.0);
request.getSession().setAttribute("endTime", endTime);
}
request.setAttribute("currPage", currPage);
request.setAttribute("rankKind", this.sessionBean.getRankKind());
request.setAttribute("isFromDatabase", this.sessionBean.isFromDatabase());
request.setAttribute("keyword",temKeyword);
request.setAttribute("splitKeyword", this.sessionBean.getKeyword());
request.setAttribute("searchKind", this.sessionBean.getSearchKind());
request.setAttribute("recordNum", this.sessionBean.getRecordNum());
request.setAttribute("listinfo", arrayDis);
request.setAttribute("pagetool", pagetool);
/*轉向結果顯示頁面*/
return mapping.findForward("display");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -