JSP的自定义TagSupport标签实现分页
程序员文章站
2022-06-08 18:05:02
...
1、在pom.xml中配置相关jar包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2、需要在WEB-INF下面配置myTag.tlg文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>mt</short-name>
<!-- 标签库配置文件 -->
<tag>
<name>page</name>
<!-- PageTag的类路径 -->
<tag-class>com.bs.beststore.web.tags.PageTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>href</name> <!-- 链接 -->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>rows</name> <!-- 每页行数 -->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>total</name> <!-- 总行数 -->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
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<totalPage){
//String pageName= pageNo==0 ? "首页" : pageNo==totalPage-1 ? "尾页" : (pageNo+1+"");
String pageName=null;
if(pageNo==0){
pageName="首页";
}else if(pageNo==totalPage-1){
pageName="尾页";
}else{
pageName=pageNo+1+"";
}
pageNo++;
String wen=href.contains("?") ? "&" : "?";
String s=String.format("<a href='%s%spage=%s&rows=%s'>%s</a>", 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页面使用
<mt:page href="userCollectionPage.do" total="${total }" rows="${rows }"/>
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<Map<String, Object>> 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";
}