PageCountTag.javapackage webapp;import java.io.*;import javax.servlet.http.*;
import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class PageCountTag extends TagSupport {

private String value = null;
private String parameter = null;
private String attribute = null;
private String property = null;
private String scope = "session";

public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}

public String getParameter() {
return this.parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}

public String getAttribute() {
return this.attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}

public String getProperty() {
return this.property;
}
public void setProperty(String property) {
this.property = property;
}

public void setScope(String scope) {
if(null == scope||scope.length() == 0) this.scope = "session";
else this.scope = scope;
}
public String getScope() {
return this.scope;
}

/**
 * 
 */
public int doEndTag() throws JspException {
int pageCount = 0;
//get value of current page from a constant
if(null!= value&&value.length() > 0) {
try {
pageCount = Integer.parseInt(value);
} catch(Exception e) {
pageCount = 0;
}
} else if(null != parameter && parameter.length() > 0) { //get value of current page from request parameters
//get request information
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
//get the value
String pageCountInRequest = request.getParameter(parameter);
//convert String to a int
try {
pageCount = Integer.parseInt(pageCountInRequest);
} catch(Exception e) {
pageCount = 0;
}
} else if(null != attribute && attribute.length() > 0) { //get value of current page from a java bean
Object obj = null;
//data is stored in session 
if(scope.toLowerCase().equals("session")) {
obj = pageContext.findAttribute(attribute);
} else { //data is stored in request
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
obj = request.getAttribute(attribute);
}
//get method name via property
if(null == property && property.length() == 0) {
pageCount = 0;
} else {
String methodName = "get"+property.substring(0,1).toUpperCase()+property.substring(1);
try { //get value
java.lang.reflect.Method method = obj.getClass().getMethod(methodName,null);
Integer pageCountInBean = (Integer)method.invoke(obj,null);
pageCount = pageCountInBean.intValue();
} catch(Exception e) {
pageCount = 0;
}
}
} else {
pageCount = 0;
}
//set value to parent
SeparationTag parent = (SeparationTag)findAncestorWithClass(this,SeparationTag.class);
if(null != parent) {
parent.setPageCount(String.valueOf(pageCount));


return (EVAL_PAGE);
}

public void release() {
value = null;
parameter = null;
String attribute = null;
String property = null;
scope = "session";
}
}
CurrentPageTag.java
package webapp;import java.io.*;import javax.servlet.http.*;
import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class CurrentPageTag extends TagSupport {

private String value = null;
private String parameter = null;
private String attribute = null;
private String property = null;
private String scope = "session";

public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}

public String getParameter() {
return this.parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}

public String getAttribute() {
return this.attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}

public String getProperty() {
return this.property;
}
public void setProperty(String property) {
this.property = property;
}

public void setScope(String scope) {
if(null == scope||scope.length() == 0) this.scope = "session";
else this.scope = scope;
}
public String getScope() {
return this.scope;
}

/**
 * 
 */
public int doEndTag() throws JspException {
int currentPage = 0;
//get value of current page from a constant 
if(null!= value&&value.length() > 0) {
try {
currentPage = Integer.parseInt(value);
} catch(Exception e) {
currentPage = 0;
}
} else if(null != parameter && parameter.length() > 0) { //get value of current page from request parameters
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
String currentPageInRequest = request.getParameter(parameter);
if(null == currentPageInRequest) currentPageInRequest = "1";
try {
currentPage = Integer.parseInt(currentPageInRequest);
} catch(Exception e) {
currentPage = 0;
}
} else if(null != attribute && attribute.length() > 0) { //get value of current page from a java bean
Object obj = null;
if(scope.toLowerCase().equals("session")) {
obj = pageContext.findAttribute(attribute);
} else {
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
obj = request.getAttribute(attribute);
}
if(null == property && property.length() == 0) {
currentPage = 0;
} else {
String methodName = "get"+property.substring(0,1).toUpperCase()+property.substring(1);
try {
java.lang.reflect.Method method = obj.getClass().getMethod(methodName,null);
Integer currentPageInBean = (Integer)method.invoke(obj,null);
currentPage = currentPageInBean.intValue();
} catch(Exception e) {
currentPage = 0;
}
}
} else {
currentPage = 0;
}
SeparationTag parent = (SeparationTag)findAncestorWithClass(this,SeparationTag.class);
if(null != parent) parent.setCurrentPage(String.valueOf(currentPage));
return (EVAL_PAGE);
}

public void release() {
value = null;
parameter = null;
String attribute = null;
String property = null;
scope = "session";
}
}

解决方案 »

  1.   


    创建TLD文件separation.tld:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    <taglib><tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>separation</shortname>
    <uri></uri><tag> <name>link</name>
    <tagclass>webapp.SeparationTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
    <name>page</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>pageCount</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>currentPage</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag><tag>
    <name>pagecount</name>
    <tagclass>webapp.PageCountTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
    <name>value</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>parameter</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>property</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>scope</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>attribute</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag><tag>
    <name>currentpage</name>
    <tagclass>webapp.CurrentPageTag</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
    <name>value</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>parameter</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>property</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>scope</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    <name>attribute</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag>
    </taglib>建立资源文件list.properties,在你的资源文件中加入:
    pages.count=[Total Pages:{0}]
    pages.current=[Current Page:{0}]
    pages.format=[{0}]
    用于指定显示格式。JSP使用范例:JSP文件头引入tld:
    <%@ taglib uri="/WEB-INF/separation.tld" prefix="separation" %>
    使用代码:
    <table width="98%">
    <tr align="center">
    <td><bean:message key="list.id"/></td>
    <td><bean:message key="list.name"/></td>
    </tr>
    <logic:iterate id="product" name="products" type="webapp.ListForm">
    <tr align="center">
    <td><bean:write name="product" property="productId" /></td>
    <td><bean:write name="product" property="productName" /></td>
    </tr>
    </logic:iterate>
    <tr>
    <td colspan=2 align="center">
    <separation:link page="listData.do">
    <separation:pagecount attribute="displayparam" property="pagecount" scope="request"/>
    <separation:currentpage parameter="page"/>
    </separation:link>
    </td>
    </tr>
    </table>listData.do对应获取数据的Action类,你应该在这个类中取得要显示的数据,并保存到products这个javabean中,ListForm是一个ActionForm,包含productId和productName两个属性;displayparam是一个包含分页参数的javabean。
      

  2.   

    Parsing of JSP File '/test.jsp' failed:
    --------------------------------------------------------------------------------
     /test.jsp(2): Error in using tag library uri='/WEB-INF/separation.tld' prefix='separation': cannot find tag class: 'webapp.SeparationTag'
    probably occurred due to an error in /test.jsp line 2:
    <%@ taglib uri="/WEB-INF/separation.tld" prefix="separation" %> 
    --------------------------------------------------------------------------------
    Wed May 12 15:41:11 GMT+08:00 2004
      

  3.   

    ok,把webapp改成自己的路径就好了,呵呵:)
      

  4.   

    struts有分页的标签不用,干吗自己写啊
      

  5.   

    kingbug(上帝是个程序员)
    struts有分页的标签不用,干吗自己写啊?请指点
      

  6.   

    struts 好像没有~~
    哪里有好用的标记库,说说,哪里下载,先谢谢了。
      

  7.   

    kingbug(上帝是个程序员)&shiyonggang(最熟悉的陌生人) :
    我刚开始学struts,不过真没找到有,请指点,谢了先
      

  8.   

    你上ftp://61.185.238.236:88上找找,当然你要有账号
      

  9.   

    http://jsptags.com/tags/navigation/pager/index.jsp
      

  10.   

    xch28(J Eclipse):谢了,研究中
    shiyonggang(最熟悉的陌生人) :没有账号,这是个什么地方?不过我的例子也提供了一个简易的实现,呵呵。
      

  11.   

    pager-taglib.jar和pager-taglib.tld两个文件考到struts目录下