SSHページング処理

6031 ワード

下地Dao
import java.util.List;
import org.cyxl.ssh.page.IPage;

public class PageImpl implements IPage{
	/**
	 * 
	 * @param list2	         
	 * @param totalCount    
	 * @param pageSize	      
	 * @param pageCount	   
	 * @param page	    
	 * @return
	 */
	public List page(List list2,Integer totalCount,Integer pageSize,Integer pageCount,Integer page){

		List list;
		if(page==1){
		//  ,      
			
			if(totalCount<pageSize){
				list = list2.subList(0,totalCount);
			}else{
				list = list2.subList(0,pageSize);
			}
			
			
    	}else if(page==pageCount){
    	//  ,      	
    		list = list2.subList(page*pageSize-pageSize,totalCount);
			
    	}else{
    	//    
    		list = list2.subList((page-1)*pageSize,page*pageSize);
    	
    	}
		return list;
	}
}
注意すべき点は、まずjspを見てください.
<center>
		<div style="font-size: 20px">
			<s:if test = "%{totalCount<=pageSize}">
				 1 , 1 ,  <s:property value = "totalCount"/>   
			</s:if>
			<s:else>
				<s:if test = "%{page==1}">

					<s:iterator value = "array" id = "num">	
						<s:if test="%{#num==page}">
							<span style="font-size: 30px; color: black">${num}</span>
							
						</s:if>
						<s:else>
							<a
								href=<s:url value = "index.action" >
								<s:param name = "page">${num}</s:param>
								</s:url>>
						
							${num}
							</a>
						</s:else>
						
						
	  					 
					</s:iterator>
					<a
							href=<s:url value = "index.action" >
								<s:param name = "page"><s:property value = "page+1"/></s:param>
						</s:url>>
							Next </a>
					 
					<a
							href=<s:url value = "index.action" >
								<s:param name = "page"><s:property value = "pageCount"/></s:param>
						</s:url>>
							Last </a>
				
				 1 , <s:property value="pageCount" /> ,  <s:property value = "totalCount"/>   
				
			</s:if>
			
			<s:elseif test="%{page==pageCount}">
				
					
					<a
						href=<s:url value = "index.action" >
							<s:param name = "page" value = "1"/>
					</s:url>>
						Home</a>
					 
					<a
							href=<s:url value = "index.action" >
								<s:param name = "page"><s:property value = "page-1"/></s:param>
						</s:url>>
							Prev </a>
				
					<s:iterator value = "array" id = "num">	
						<s:if test="%{#num==page}">
							<span style="font-size: 30px; color: black">${num}</span>
							
						</s:if>
						<s:else>
							<a
								href=<s:url value = "index.action" >
								<s:param name = "page">${num}</s:param>
								</s:url>>
						
							${num}
							</a>
						</s:else>
						
						
	  					 
					</s:iterator>
				
					
					 <s:property value="pageCount" /> , <s:property value="pageCount" /> ,  <s:property value = "totalCount"/>   
				</s:elseif>

				<s:else>
					<a
						href=<s:url value = "index.action" >
					
							<s:param name = "page" value = "1"/>
						
					</s:url>>
						Home </a>
				 
				
				<a
							href=<s:url value = "index.action" >
								<s:param name = "page"><s:property value = "page-1"/></s:param>
						</s:url>>
							Prev </a>
						
					 
					<s:iterator value = "array" id = "num">	
						<s:if test="%{#num==page}">
							<span style="font-size: 30px; color: black">${num}</span>
							
						</s:if>
						<s:else>
							<a
								href=<s:url value = "index.action" >
								<s:param name = "page">${num}</s:param>
								</s:url>>
						
							${num}
							</a>
						</s:else>
						
						
	  					 
					</s:iterator>
				<a
						href=<s:url value = "index.action" >
							<s:param name = "page"><s:property value = "page+1"/></s:param>
					</s:url>>
						Next </a>
				 
				<a
						href=<s:url value = "index.action" >
							<s:param name = "page"><s:property value = "pageCount"/></s:param>
					</s:url>>
						Last </a>
				 <s:property value="page" /> , <s:property value="pageCount" /> ,  <s:property value = "totalCount"/>   		
					
			
			</s:else>
		</s:else>
		

		</div>
	</center>

注意すべき点:総本数が1ページに規定された本数より小さいことに注意し、トップページの前のページに現れないように注意してください.
この中には2つのパラメータを入れる必要があります.pageとpageCountなので、ActionではGet、Setを必要とします.マイアクション:
public String selete(){  
          
        String field_name = "typ";
        String field_value = "1";
        String desc = "desc";
        String table = "Content";
    	List list2 = userService.selete(field_name,field_value,desc,table);  

    	totalCount = list2.size();
    	if(totalCount%pageSize==0){
    		pageCount = totalCount/pageSize;
    	}else{
    		pageCount = totalCount/pageSize+1;
    	}
    	array = new ArrayList();
    	for(int i=1;i<=pageCount;i++){
    		array.add(i);
    	}
    	
    	//  list,      ,           ,   ,     ,    ------
		list = pageService.page(list2, totalCount, pageSize, pageCount, page);

    	return "selete";  
    }

パラメータは必ず正しい!!!