java 自定义标签

方式就是继承或实现jsp提供的标签支持类

下面是继承struts的TagSupport

1.实现标签处理

package com.qdys.b2b.web.tag; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import com.qdys.b2b.web.util.PagerHelper; /** * <p> * <b>类名称:</b>Pager * </p> * <p> * <b>类描述:</b> * </p> * <p> * <b>日期:</b>2009-12-15 下午03:42:47 * </p> * * @author PanJinfeng([email protected]) */ public class Pager extends TagSupport { private static final long serialVersionUID = 6421075734182665276L; /** * 链接地址 */ private String url; // 当前页变量 private String curPageVar; /* * // 需要从resquest里面得到的参数,多个,间隔 private String needAttr; */ /** * 页面可选页数,默认10页可选 */ private String tageSize = "10"; @SuppressWarnings("unchecked") @Override public int doStartTag() throws JspException { try { // request ServletRequest req = pageContext.getRequest(); // 获取并组装所有参数 Enumeration<String> en = req.getParameterNames(); StringBuffer params = new StringBuffer(); while (en.hasMoreElements()) { String parmName = (String) en.nextElement(); if (!parmName.equals(curPageVar)) { String value = req.getParameter(parmName); params.append("&").append(parmName).append("=").append( value); } } // 获取分页类 PagerHelper helper = (PagerHelper) req.getAttribute("pagerHelper"); if (helper != null) { JspWriter out = pageContext.getOut(); StringBuffer sb = new StringBuffer(); sb.append("总记录数:" + helper.getTotalRecords() + ",当前页记录数:" + helper.getCurrentPageSize() + "  "); // 总页数,总记录数,当前第几页,上一页 sb.append("当前第" + helper.getCurrentPage() + "/" + helper.getTotalPages() + "页    "); // 当前页不是第一页 if (helper.getCurrentPage() != 1) { sb.append("<a href="/" mce_href="/""" + url + "?" + curPageVar + "=1" + params + "/">首页</a> "); } // 当前页大于1显示上一页 if (helper.getCurrentPage() > 1) { sb.append("<a href="/" mce_href="/""" + url + "?" + curPageVar + "=" + (helper.getCurrentPage() - 1) + params + "/">上一页</a> "); } // 如果总页数小于可选择页数,直接显示1....n(n为总页数) if (helper.getTotalPages() < Integer.valueOf(tageSize)) { for (int i = 1; i <= helper.getTotalPages(); i++) { if (i == helper.getCurrentPage()) { sb.append("<b>").append(i).append("</b>"); } else { sb.append("<a href="/" mce_href="/""").append(url).append( "?" + curPageVar + "=").append(i).append( params).append("/">").append(i).append( "</a> "); } } } else { /** * 如果总页数大于可选择页数,则将当前页显示在中间位置 */ if (helper.getCurrentPage() <= Integer.valueOf(tageSize) / 2) { for (int i = 1; i <= Integer.valueOf(tageSize) && i <= helper.getTotalPages(); i++) { if (i == helper.getCurrentPage()) { sb.append("<b>").append(i).append("</b> "); } else { sb.append("<a href="/" mce_href="/""").append(url).append( "?" + curPageVar + "=").append(i) .append(params).append("/">").append(i) .append("</a> "); } } } else if (helper.getCurrentPage() <= helper .getTotalPages()) { int first = helper.getCurrentPage(); if (helper.getCurrentPage() <= (helper.getTotalPages() - Integer .valueOf(tageSize) / 2)) { first = helper.getCurrentPage() - Integer.valueOf(tageSize) / 2; } else { first = helper.getTotalPages() - Integer.valueOf(tageSize) + 1; } for (int i = first; i < (first + Integer .valueOf(tageSize)); i++) { if (i == helper.getCurrentPage()) { sb.append("<b>").append(i).append("</b> "); } else { sb.append("<a href="/" mce_href="/""").append(url).append( "?" + curPageVar + "=").append(i) .append(params).append("/">").append(i) .append("</a> "); } } } } // 当前页小于总页数,显示下一页和尾页 if (helper.getCurrentPage() < helper.getTotalPages()) { sb.append( "<a href="/" mce_href="/""" + url + "?" + curPageVar + "=" + (helper.getCurrentPage() + 1) + params + "/">下一页</a>  ").append( "<a href="/" mce_href="/""" + url + "?" + curPageVar + "=" + helper.getTotalPages() + params + "/">尾页" + "</a>"); } out.print(sb.toString()); } } catch (IOException e) { e.printStackTrace(); } return TagSupport.EVAL_BODY_INCLUDE; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getTageSize() { return tageSize; } public void setTageSize(String tageSize) { this.tageSize = tageSize; } /** * @return the curPageVar */ public String getCurPageVar() { return curPageVar; } /** * @param curPageVar * the curPageVar to set */ public void setCurPageVar(String curPageVar) { this.curPageVar = curPageVar; } /* * public String getNeedAttr() { return needAttr; } * * public void setNeedAttr(String needAttr) { this.needAttr = needAttr; } */ }

2.定义标签,书写ltd文档

<?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>mytags</short-name> <tag> <name>pager</name> <tag-class>com.qdys.b2b.web.tag.Pager</tag-class> <body-content>empty</body-content> <attribute> <name>url</name> <required>true</required> <type>string</type> </attribute> <attribute> <name>curPageVar</name> <required>true</required> <type>string</type> </attribute> <!-- <attribute> <name>needAttr</name> <required>false</required> <type>string</type> </attribute> --> <attribute> <name>tageSize</name> <required>false</required> <type>string</type> </attribute> </tag> </taglib>

3.web.xml中加入申明

<jsp-config> - <taglib> <taglib-uri>/mytags</taglib-uri> <taglib-location>/WEB-INF/mytags.tld</taglib-location> </taglib> </jsp-config>

4.jsp页面中调用

先申明标签定义

<%@ taglib uri="/mytags" prefix="p"%>

 

使用

   <p:pager url="" curPageVar="curPage" />

你可能感兴趣的:(java 自定义标签)