⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pageaction.java

📁 本系统实现了从五个网站上搜索的图书进行整合后
💻 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 + -