package com.zgq.tagsupport;
public class PageAction extends TagSupport {
private String items;
private String var;
private String url;
private PageModel pm;
Iterator it = null;
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public PageModel getPm() {
return pm;
}
public void setPm(PageModel pm) {
this.pm = pm;
}
@Override
public int doStartTag() throws JspException {
pm = (PageModel)pageContext.getAttribute(items, pageContext.REQUEST_SCOPE);
//解析 分页对象 中的数据
it = pm.getList().iterator();
if(it.hasNext()){
//将 获取的值存放到 page作用中
pageContext.setAttribute(var, it.next());
return super.EVAL_BODY_INCLUDE;
}else{
return super.SKIP_BODY;
}
}
@Override
public int doAfterBody() throws JspException {
//判断是否 存在下一条数据
if(it.hasNext()){
pageContext.setAttribute(var,it.next());
return super.EVAL_BODY_AGAIN;
}else{
return super.SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
//数据循环:
//.................
out.print("<tr>");
out.print("<td><a href='"+url+"?op=myPageInfo&size=3¤tPage=1'>首页</a></td>");
out.print("<td><a href='"+url+"?op=myPageInfo&size=3¤tPage="+((pm.getCurrentPage()==1)?pm.getCurrentPage():(pm.getCurrentPage()-1))+"'>上一页</a></td>");
out.print("<td><a href='"+url+"?op=myPageInfo&size=3¤tPage="+((pm.getCurrentPage()==pm.getSumPage())?pm.getCurrentPage():(pm.getCurrentPage()+1))+"'>下一页</a></td>");
out.print("<td><a href='"+url+"?op=myPageInfo&size=3¤tPage="+pm.getSumPage()+"'>尾页</a><td>");
out.print("</tr>");
} catch (Exception e) {
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
配置文件:导入包
新建一个.tld配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>xdz</short-name>
<uri>www.zgq</uri>
<tag>
<name>actionpage</name>
<tag-class>com.zgq.tagsupport.PageAction</tag-class>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
页面设置
<%@ taglib uri="www.zgq" prefix="xdz"%>
<xdz:actionpage items="pm" url="servlet/Yydbservlet" var="item">
<tr>
<td>${item.user_id }</td>
<td>${item.uname }</td>
</tr>
</xdz:actionpage>