JSP的自定义TagSupport标签实现分页

1、在pom.xml中配置相关jar包


	commons-fileupload
	commons-fileupload
	1.3.1

 

2、需要在WEB-INF下面配置myTag.tlg文件,内容如下



		
	1.0
	mt

	
    
    	page
    	
    	com.bs.beststore.web.tags.PageTag
    	empty
    	
    		href	
    		true
    		true
    	
    	
    		rows	
    		true
    		true
    	
    	
    		total	
    		true
    		true
    	
    
    

3、写PageTag.java文件

package com.bs.beststore.web.tags;

import java.io.IOException;

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

public class PageTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;
	private long total;		//总数
	private int rows;		//每页行数
	private String href;	//请求地址
	
	public int doStartTag() throws JspException {
		int totalPage=0;
		if(total>0 && rows>0){
			totalPage=((int)total%rows)==0 ? (int)total/rows : (int)total/rows+1; //总页数
		}
		int pageNo=0;		//当前页号
		if(total<=0 || rows<=0 || totalPage<=0){
			return SKIP_BODY;
		}
		
		while(pageNo%s", href,wen,pageNo,rows,pageName);
			try {
				pageContext.getOut().println(s);
			} catch (IOException e) {
				new JspException();
			}
		}
		return EVAL_BODY_INCLUDE;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setHref(String href) {
		this.href = href;
	}
	
}

4、在JSP页面加入指令

<%@ taglib uri="/WEB-INF/myTag.tld" prefix="mt"%>

5、JSP页面使用

6、写控制层,记得要判断当前页号

@RequestMapping(value="userCollectionPage.do")
	public String userCollectionPage(HttpServletRequest request) {
		Human human = (Human) request.getSession().getAttribute("loginHuman");
		String page=request.getParameter("page");
		if(page==null) {
			page="1";
		}
		int rows=8;	//每页条数
		long total=favoriteBiz.findAllTotal(human.getHid());	//总条数
		int totalPage=(int) (total/rows==0 ? total/rows: total/rows+1);	//总页数
		request.setAttribute("totalPage", totalPage);
		if(Integer.parseInt(page)<=1) {
			page="1";
		}
		if(Integer.parseInt(page)>=totalPage && totalPage>0){
			page=totalPage+"";
		}
		request.setAttribute("page", page);	//当前页数
		List> list = favoriteBiz.findAllFavorite(human.getHid(),Integer.valueOf(page),rows);
		if(list.size()>0) {
			request.setAttribute("list", list);
			request.setAttribute("total", total);
			request.setAttribute("rows", rows);
		}
		return "userCollection";
	}

 

 

 

 

你可能感兴趣的:(java)