jsp自定义标签实现分页

1. maven导入标签架包


 
          
			javax.servlet.jsp  
			jsp-api  
			2.2  
         
        
        
			javax.servlet
			javax.servlet-api
			3.1.0
		


2. 新建分页标签wld.tld文件

 



    1.0
    1.1
    wld
    http://www.xxx.tld

	 
   
     page
      welab.finance.web.common.tags.GosPage
      JSP
 	  
            page
            true
            true
      
      
            action
            true
            true
      
   
  
  
  
     param
      welab.finance.web.common.tags.GosParam
      JSP
 	  
            name
            true
            true
      
      
            value
            true
            true
      
  
  
  


3. web.xml中配置


 
    
		
		http://www.xxx.tld
		/WEB-INF/tld/wld.tld
		
    


4. 分页bean

 

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * 分页对象类
 * 
 * @author rhj
 */
public class Page  {

	/** 当前页数* */
	private int pageNum;

	/** 每页显示的记录数* */
	private int numPerPage;

	/** 记录行数* */
	private int rowsCount;

	/** 总页数* */
	private int pageCount;
	
	/** 分页列表数据*/
	private E list;
	
	/**分页开始**/
	private int startIndex;
	
	/**分页结束**/
	private int endIndex;


	/*** action中可以自己定义每页大小,然后获取页面的下一页的页数 **/
	public static  Page getPage(HttpServletRequest request, int numPerPage) {
		Page page = new Page();
		page.setNumPerPage(numPerPage);
		toPage(page, request);
		return page;
	}

	/** 获取页面的下一页的页数 **/
	public static  Page getPage(HttpServletRequest request) {
		Page page = new Page();
		toPage(page, request);
		return page;
	}

	/*** 获取页面的下一页的页数,并赋值当前页面页数 **/
	private static void toPage(Page page, HttpServletRequest request) {
		String pageNumber = request.getParameter("pageNum");
		if (!StringUtils.isEmpty(pageNumber)) {
			page.setPageNum(Integer.parseInt(pageNumber));
			int endIndex = page.getPageNum()*page.getNumPerPage();
			page.setEndIndex(endIndex);
			page.setStartIndex(endIndex-page.getNumPerPage());
		}
	}

	/*** 新建page时默认页面值 ****/
	private Page() {
		this.numPerPage = 10;
		this.pageNum = 1;
		this.startIndex = 0;
		this.endIndex =10;
	}


	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getRowsCount() {
		return rowsCount;
	}

	public void setRowsCount(int rowsCount) {
		int pageSize = (int) Math.ceil((double) rowsCount / numPerPage);
		if(this.pageNum>pageSize && pageSize>=1){
			this.pageNum = pageSize;
		}
		this.rowsCount = rowsCount;
	}

	public int getPageCount() {
		return this.pageCount = (int) Math.ceil((double) rowsCount / numPerPage);
	}

	public E getList() {
		return list;
	}

	public void setList(E list) {
		this.list = list;
	}

	public int getStartIndex() {
		return (this.pageNum - 1) * this.numPerPage;
	}


	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}


}



5. 创建分页标签处理类GosPage


package welab.finance.web.common.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import welab.finance.web.common.Page;



/**
 * 分页标签
 * @author rhj
 *
 */

public class GosPage extends BodyTagSupport {
	private String action;
	private Page page;
	
	private Logger logger = Logger.getLogger(getClass());

	@Override
	public int doStartTag() throws JspException {
		return EVAL_BODY_BUFFERED;
	}

	@Override
	public int doEndTag() throws JspException {
		//标签体之间的内容
		String bodyContentVal = null;
		if (bodyContent != null) {
			bodyContentVal = bodyContent.getString();
		}
		//如果页数小于0
		if (page.getPageCount() <= 0) {
			page.setPageNum(0);
		}
		StringBuilder builder = new StringBuilder();
		builder.append("
"); builder.append("
"); if (Integer.valueOf(page.getPageNum()) > 1) { builder.append("").append(" 首页 ").append(""); builder.append("").append(" 上一页 ").append(""); } else { builder.append("").append(" 首页 ").append(""); builder.append("").append(" 上一页 ").append(""); } if (Integer.valueOf(page.getPageNum()) < Integer.valueOf(page.getPageCount())) { builder.append("").append(" 下一页 ").append(""); builder.append("").append(" 尾页 ").append(""); } else { builder.append("").append(" 下一页 ").append(""); builder.append("").append(" 尾页 ").append(""); } builder.append("共").append(page.getPageCount()).append("页/").append(page.getRowsCount()).append("条 "); builder.append(" 页"); builder.append(""); builder.append(" GO "); builder.append("
"); if (null != bodyContentVal && !"".equals(bodyContentVal)) { builder.append(bodyContentVal); } builder.append("
"); //构造js函数 builder.append(""); try { pageContext.getOut().print(builder.toString()); } catch (IOException e) { e.printStackTrace(); logger.error("分页标签错误",e); } return EVAL_PAGE; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } }
 
  

6. 创建标签分页参数类GosParam

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
/**
 * 分页标签
 * @author rhj
 *
 * 2015年4月8日
 */
public class GosParam extends TagSupport{
	
	private Logger log=Logger.getLogger(getClass());
	
	private String name;
	private String value;
	
	@Override
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}
	/**
	 * form表单的隐形框 
	 */
	@Override
	public int doEndTag() throws JspException {
		StringBuilder builder = new StringBuilder();
		builder.append("");
		try {
			pageContext.getOut().print(builder.toString());
		} catch (IOException e) {
			e.printStackTrace();
			log.error("分页标签error",e);
		}
		return EVAL_PAGE;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}

}



7. 分页样式类page.css

  

.pageDiv .pageClassDisable{text-decoration: none;cursor: pointer; margin-left: 10px;color: #B0B0B0}
.pageDiv .pageClass{text-decoration: none;cursor: pointer; margin-left: 10px;}
.pageDiv .pageGO{text-decoration: none;cursor: pointer; margin-left: 10px;}
.pagespan input{width: 35px;height: 20px;border: 1px solid #B0B0B0}
.pageDiv .pageSpan{margin-left:10px;}



8. jsp中引用分页


  1). 引入自定义标签    <%@ taglib uri="http://www.wolaidai.tld" prefix="wld"%>

  2). 引入css文件         

  3). 显示分页



	 

9.  java调用方法


         /**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toApprovalWriteoff")
	public String checkWriteoff(HttpServletRequest request,Model model,WriteoffTransactionVo writeoff,HttpSession session) {
		 
		 Integer userId = (Integer) session.getAttribute(FrsConstant.LOGIN_USER_ID);
		 writeoff.setApprovalId(userId);
		 Page> page = Page.getPage(request);
		 int  rowsCount = writeoffTransferRetriever.getWriteoffTransactionsVosForApprovalCount(writeoff);	
		 page.setRowsCount(rowsCount);
		 List writeoffList =
				 writeoffTransferRetriever.getWriteoffTransactionsVosForApproval(writeoff,page.getStartIndex(),page.getEndIndex());
		 model.addAttribute("writeoffList", writeoffList);
		 model.addAttribute("page", page);
		return "/writeoff/approval_writeoff";
	}


 
  

10. 分页效果展现




下班喽~~~~

你可能感兴趣的:(java)