81,092
社区成员
发帖
与我相关
我的任务
分享
public class Pagination extends BodyTagSupport {
private int currentPage ,interval, size, count;
private List items;
private String type , item;
public int doAfterBody() throws JspException {
int currentIndex = this.currentPage * this.interval + this.count;
if(this.count >= this.interval || currentIndex >= this.size)
return Pagination.SKIP_BODY;
if (bodyContent != null) {
writePrevious(this.pageContext, bodyContent.getString());
bodyContent.clearBody();
}
this.pageContext.setAttribute("item", this.items.get(currentIndex));
this.count ++;
return Pagination.EVAL_BODY_BUFFERED;
}
public int doEndTag() throws JspException {
StringBuffer url = ((HttpServletRequest)this.pageContext.getRequest()).getRequestURL();
String quesMark = "";
if(url.indexOf("?") != -1)
quesMark = "?";
StringBuffer output = new StringBuffer();
try {
output.append("<style type=\"text/css\">" +
"ra {text-align:right}" +
"</style>");
output.append("<div class=\"ra\">");
if(this.currentPage == 0){
output.append("首页 上一页 ");
}
else{
output.append("<a href=\"url" + quesMark + "¤tPage=0\">首页</a> " +
"<a href=\"url" + quesMark + "¤tPage=" + (this.currentPage - 1) + "\">上一页</a> ");
}
if( ( (this.currentPage + 1) * this.interval ) >= this.size )
output.append("下一页 末页 ");
else
output.append("<a href=\"url" + quesMark + "¤tPage=" + (this.currentPage + 1) + "\">下一页</a> " +
"<a href=\"url" + quesMark + "¤tPage=" + (((this.size % this.currentPage) == 0 ) ? (this.size / this.currentPage - 1) : (this.size / this.currentPage)) + "\">末页</a> ");
output.append("共有" + this.size + "条记录,现在是第" + this.currentPage + 1 + "页,每页" + this.interval + "行</div>");
this.writePrevious(this.pageContext, output.toString());
} catch (JspException e) {
e.printStackTrace();
}
return Pagination.EVAL_PAGE;
}
public int doStartTag() throws JspException {
this.count = 0;
this.size = this.items.size();
String currentPageStr = this.pageContext.getRequest().getParameter("currentPage");
if(currentPageStr == null || currentPageStr.equals(""))
this.currentPage = 0;
else{
try{
this.currentPage = Integer.parseInt(currentPageStr);
}catch(NumberFormatException e){
System.out.println("Can't format the string \"" + currentPageStr + "\"!");
this.currentPage = 0;
}
}
return Pagination.EVAL_BODY_BUFFERED;
}
public void writePrevious(PageContext pageContext, String text) throws JspException {
JspWriter writer = pageContext.getOut();
if (writer instanceof BodyContent) {
writer = ((BodyContent) writer).getEnclosingWriter();
}
try {
writer.print(text);
} catch (IOException e) {
e.printStackTrace();
}
}
...Setters/Getters
}
public class PaginationTEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
String type = data.getAttributeString("type");
if(type == null)
type = "java.lang.String";
return new VariableInfo[]{
new VariableInfo(data.getAttributeString("item"), type, true, VariableInfo.NESTED)
};
}
}
<?xml version="1.0" ?>
<!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>july</short-name>
<tag>
<name>page</name>
<tag-class>nju.software.tags.Pagination</tag-class>
<tei-class>nju.software.tags.PaginationTEI</tei-class>
<body-content>JSP</body-content>
<attribute>
<name>interval</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.List</type>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<taglib>
<taglib-uri>/july</taglib-uri>
<taglib-location>/WEB-INF/pagination.tld</taglib-location>
</taglib>
</web-app>
<july:page items=" <%=session.getAttribute("cps") %>" interval="2" item="cp" type="nju.software.entity.ConflictPractice" >
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.List</type>
</attribute>
JSP页面:
[code=HTML]
<%@page contentType="text/html; charset=GBK" import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="july" uri="/july" %>
.....省略部分代码,太长了.....
<july:page items=" <%=session.getAttribute("cps") %>" interval="2" item="cp" type="nju.software.entity.ConflictPractice" >
<c:set var="id" value="${cp.id}"> </c:set>
<tr>
<td align="center"> <c:out value="${id}" > </c:out> </td>
<td align="center"> <c:out value="${cp.time}"> </c:out> </td>
<td align="center"> <c:out value="${cp.conductor}"> </c:out> </td>
<td align="center"> <c:out value="${cp.participant}"> </c:out> </td>
<td align="center"> <c:out value="${cp.emergency}"> </c:out> </td>
<td align="center"> <c:out value="${cp.assist_Department}"> </c:out> </td>
<td align=center>
<c:if test="${rp.insert == 1}"> <a href="ConflictPractice_Edit.jsp?id= <%=id %>&uid= <%=pageContext.getAttribute("id") %>">编辑 </a>
</c:if>
<c:if test="${rp.del == 1}">
<a href="ConflictPractice_Console.jsp?id= <%=id %>&fg=3&uid= <%=pageContext.getAttribute("id") %>" onclick="return Del();">删除 </a>
</c:if>
</td>
</tr>
</july:page>