struts 2 javaページング

13099 ワード

2回目にページングコードを変更し、いくつかの小さなバグを解決しました.このコードはまだテスト中です.問題の指摘を歓迎します.の
AJAXページングを使用していない場合は、jspページのハイパーリンクのjsをactionリクエストアドレスに置き換えます.
これはajaxPageInfo.javaです

package com.dwh.common.sys;

import org.apache.struts2.ServletActionContext;
/*
 *      
 * 
 *   ,      ajax,              
 * 
 * 		//         
		String requestURI="queryPurchaseMuti";//      
		String queryString="";//  
		Iterator iter = m.entrySet().iterator();  
		while (iter.hasNext()) {  
		Map.Entry entry = (Map.Entry) iter.next(); 
		  if(!entry.getKey().equals("cur_arg")){
			Object key = entry.getKey();  
			Object val = entry.getValue(); 
			queryString+="&"+key+"="+val;
		  }
		}  
		purchase.setPageUrl(requestURI, queryString);
		purchase.setPageSize(2);//          2
		purchase.setSumRows(8);//       8 
 * 
 * 
 */
public class AjaxPageInfo {
	/**    */
	private int currentPage;	
	/**       */
	private int pageSize =10;
	/**      */
	private int sumPages;
	/**    */
	private int sumRows;	
	/**          */
	private String pageUrl;	
	/**       */
	private int startRow;	
	/**       */
	private int endRow;
	
	/**          **/
	private int beginPage;
	/**          **/
	private int endPage;
	/**        Page  **/
	private int pageNumber=10;//      10     

	
	/**
	 *       
	 * @return
	 */
	public int getCurrentPage() {
		return currentPage;
	}
	/**
	 *       
	 * @param currentPage
	 */
	public void setCurrentPage(String curPage) {
		if(curPage==null||curPage.equals("")){//           ,   0,     
			this.currentPage=0;
		}else{
			currentPage=Integer.parseInt(curPage);
		}
		this.currentPage = currentPage < 1 ? 0 : currentPage;//             
		this.startRow=currentPage*pageSize;				//       ,0         
		this.endRow=startRow+pageSize;
		setPageUrl();										//      
	}
	/**
	 *       
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 *       
	 * @param rowsPage
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 *      
	 * @return
	 */
	public int getSumPages() {
		return sumPages;
	}
	/**
	 *      
	 * @param sumPages
	 */
	public void setSumPages(int sumPages) {
		this.sumPages = sumPages;
	}
	/**
	 *      
	 * @return
	 */
	public int getSumRows() {
		return sumRows;
	}
	/**
	 *      
	 * @param sumRows
	 */
	public void setSumRows(int sumRows) {
		this.sumRows = sumRows;
		if(sumRows%pageSize==0){//    
			this.sumPages=sumRows/pageSize;//     
		}
		if(sumRows%pageSize>0){//    
			this.sumPages=sumRows/pageSize+1;
		}
		if(sumRows%pageSize<0){//    
			this.sumPages=sumRows/pageSize;
		}
		
	}
	public void setSumRows(int sumRows,int begin,int end) {//          
		this.sumRows = sumRows;
		if(sumRows%pageSize==0){//    
			this.sumPages=sumRows/pageSize;//     
		}
		if(sumRows%pageSize>0){//    
			this.sumPages=sumRows/pageSize+1;//(sumRows/pageSize)+1 
		}
		if(sumRows%pageSize<0){//    
			this.sumPages=sumRows/pageSize;
		}
		this.beginPage=begin;
		this.endPage=end;
		
		
		if(sumPages<pageNumber){//             
			this.pageNumber=sumPages;
		}

		//              
		if(pageNumber<sumPages){
			if(currentPage>0){		
				this.beginPage=(currentPage/pageNumber)*pageNumber;//       *pageNumber
				this.endPage=beginPage+pageNumber;
			}else{
				this.beginPage=1;
				this.endPage=pageNumber;
			}
		}else{
			this.beginPage=1;
			this.endPage=pageNumber;
		}
		//          	
		if((currentPage+1)%pageNumber==0){
			if(sumPages>pageNumber){//                       
				this.beginPage=currentPage+1;
			}else{
				this.beginPage=begin;
			}
			if(sumPages-currentPage>pageNumber){//             
				this.endPage=currentPage+11;
			}else{
				this.endPage=sumPages;
			}
		}
		//           
		if(beginPage==currentPage+1&&currentPage!=0&&(currentPage+1)%pageNumber==0){
			if(currentPage+1==begin){
				if(currentPage+1!=pageNumber){//          
				this.beginPage=(currentPage+1)-pageNumber;
				}else{
				this.beginPage=(currentPage+2)-pageNumber;	//         
				}
				this.endPage=currentPage+1;
			}
		}
		
		if(currentPage == sumPages-1){//     ,            
			if(sumPages==1){
				this.beginPage = 1;
				this.endPage =1;
			}else{
				if(sumPages>pageNumber){
					this.beginPage = sumPages - (sumPages % pageSize);//  ,     	
				}
				this.endPage = sumPages;
			}
				
		}
		
		
	//	System.out.println("   :"+beginPage);
	//	System.out.println("   :"+endPage);
		
	}
	/**
	 *       
	 * @return           
	 */
	public String getPageUrl() {
		return pageUrl;
	}
	public void setPageUrl() {
		String pageURl =  ServletActionContext.getRequest().getRequestURI()+"?a=a";
		pageURl +="&"+ServletActionContext.getRequest().getQueryString();
		pageURl=pageURl.replaceAll("&curPage="+currentPage,"");//        
		if(pageURl.contains("a=a&")){						   //        
			pageURl=pageURl.replaceAll("a=a&", "");
		}else{
			pageURl=pageURl.replaceAll("a=a", "");
		}
		this.pageUrl = pageURl;
	}
	/**setPageUrl()   
	 * @param requestURI js   uri
	 * @param queryString  js     
	 * */
	public void setPageUrl(String requestURI,String queryString) {
		String pageURl =  requestURI+"?a=a";
		pageURl +="&"+queryString;
		pageURl=pageURl.replaceAll("&curPage="+currentPage,"");//        
		if(pageURl.contains("a=a&")){						   //        
			pageURl=pageURl.replaceAll("a=a&", "");
		}else{
			pageURl=pageURl.replaceAll("a=a", "");
		}
		this.pageUrl = pageURl;
	}

	
	public int getStartRow() {
		return startRow;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	public int getEndRow() {
		return endRow;
	}
	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
	public int getBeginPage() {
		return beginPage;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public void setBeginPage(int beginPage) {
		if(currentPage==0){//         
			this.beginPage=1;
		}
		this.beginPage = beginPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		if(currentPage==0){//         
			this.endPage=pageNumber;
		}
		this.endPage = endPage;
	}

}

これはpageinfo.jspです
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
	isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head>
<style type="text/css">
DIV.scott A.actives {
	BORDER-RIGHT: #85bd1e 1px solid; BORDER-TOP: #85bd1e 1px solid; BORDER-LEFT: #85bd1e 1px solid; COLOR: #638425; BORDER-BOTTOM: #85bd1e 1px solid; BACKGROUND-COLOR: #f1ffd6; text-decoration:none;
}
</style>
</head>

<link href="${pageContext.request.contextPath}/lib/common/css/pageInfo.css" type="text/css" rel="stylesheet">

<div class="scott" id="showPage" style="height: 28px;">
	<s:if test="currentPage>0">
		<a href="javascript:ajaxQuery('${pageUrl}&currentPage=0');">  </a>|
        <a href="javascript:ajaxQuery('${pageUrl}&currentPage=${currentPage-1}');">   </a>|
    </s:if>
	<s:else>  |    |</s:else>
	
	<!--          -->
			<s:bean name="org.apache.struts2.util.Counter" id="counter">
			<s:param name="first" value="beginPage" />
			<s:param name="last" value="endPage" />
			<s:iterator>
			<s:if test="currentPage+1==current-1">
			<a class="actives" href="javascript:ajaxQuery('${pageUrl}&currentPage=${current-2}');"><s:property /></a>
			</s:if>
			<s:else>
			<a href="javascript:ajaxQuery('${pageUrl}&currentPage=${current-2}');"><s:property /></a>
			</s:else>
			</s:iterator>
			</s:bean>

	<!--        -->
	<s:if test='currentPage+1 < sumPages'>
		<a href="javascript:ajaxQuery('${pageUrl}&currentPage=${currentPage+1}');">   </a>|
        <a href="javascript:ajaxQuery('${pageUrl}&currentPage=${sumPages-1}');">   
		</a>
	</s:if>
	<s:else>   |    </s:else>

	 ${sumPages} / ${sumRows}   
	<input id="load" type="text" size="3" value="" onkeyup="loadUrl(this.value)">
	 
	<a href="javascript:;" onclick="loadUrl2(document.getElementById('load').value);">  </a>
</div>

<script type="text/javascript">
//ajax       
	function ajaxQuery(url){
	url+="&beginPage="+${beginPage}+"&endPage="+${endPage};
		$.ajax({
			url:url,
			type:"get",
			cache:false,
			success:function(data){//  ajax  			
				var selectFetchType=$('#fetchType').find("option:selected").text();
				$('#content').html(data);//           jsp  
				$('#queryName').html(selectFetchType);	
				$('#queryName').width(100);
			}
			});
	}
	function loadUrl(str) {
		//         ,      ,        
		if (window.event) {//    
			var keyCode = window.event.keyCode;
			if (keyCode == 13 && str != false) {//    
				//         ,      ,        
				var str = isNumber(str);
				if (str == false) {
					alert("            ");
					return false;
				}
				var sumPages = '${sumPages}';
				str = parseInt(str);
				if (str > sumPages || str < 1) {
					alert("       ");
					return false;
				}//oracle   ,     str 1
				str = str - 1;//   ORACLE   ,     1,    1    
				ajaxQuery('${pageUrl}&currentPage='+str);
			}
		}
	}
	function loadUrl2(str) {
		//         ,      ,        
		var str = isNumber(str);
		if (str == false) {
			alert("            ");
			return false;
		}
		var sumPages = '${sumPages}';
		str = parseInt(str);
		if (str > sumPages || str < 1) {
			alert("       ");
			return false;
		}//oracle   ,     str 1
		str = str-1;
		ajaxQuery('${pageUrl}&currentPage='+str);
	}

	/*     */
	var currentPage = '${currentPage}';
	function showCurrent() {
		var page = document.getElementById("showPage");
		if (currentPage != "") {
			var cssPage = page.getElementsByTagName("span");
			if (currentPage < cssPage.length) {
				cssPage[currentPage].className = "current";
			}
		}
	}
	showCurrent();

	/*       */
	function isNumber(str) {
		var Letters = "1234567890.";
		for ( var i = 0; i < str.length; i = i + 1) {
			var CheckChar = str.charAt(i);
			if (Letters.indexOf(CheckChar) == -1) {
				return false;
			}
		}
		return str;
	}
</script>