JSP 自定义标签
JSP 自定义标签
首先新建一个项目,项目目录如下:
声明一个TLD文件,代码及说明如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- 标记库描述 --> <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0.0</tlib-version><!-- 自定义标签版本 --> <jsp-version>2.2</jsp-version><!-- jsp-api 版本 --> <short-name>self</short-name><!-- 指定Tag Library默认的前缀名(prefix) --> <uri>/WEB-INF/self.tld</uri><!-- 设定Tag Library的惟一访问表示符 --> <display-name>"Self Tags"</display-name><!-- --> <tag><!-- 具体标签说明 --> <name>date</name><!-- 标签名 --> <tag-class>com.self.framework.ui.tag.DateTag</tag-class><!-- 标签处理类 --> <!-- 1)empty:表示标签中没有body; 2)JSP:表示标签的body中可以加入JSP程序代码; 3)tagdependent:表示标签中的内容由标签自己去处理。 --> <body-content>empty</body-content> <!-- 定义属性 --> <attribute> <name>pattern</name> <!-- 属性名字 --> <type>String</type> <!-- 属性类型 --> <requried>false</requried> <!-- 属性是否必需的,默认为false --> <rtexprvale>false</rtexprvale><!-- 属性值是否可以为request-time表达式,也就是类似于< %=…% >的表达式 --> </attribute> </tag> <tag> <name>iterator</name> <tag-class>com.self.framework.ui.tag.BodyTag</tag-class> <body-content>jsp</body-content> <attribute> <name>count</name> <type>int</type> <requried>false</requried> <rtexprvale>false</rtexprvale> </attribute> </tag> <tag> <name>list</name> <tag-class>com.self.framework.ui.tag.ListTag</tag-class> <body-content>jsp</body-content> <attribute> <name>name</name> </attribute> <attribute> <name>value</name> </attribute> </tag> </taglib>
web.xml 文件中:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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-app_2_5.xsd"> <display-name>SelfTag</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置标签库及JSP页面编码 --> <jsp-config> <taglib> <taglib-uri>self</taglib-uri> <taglib-location>/WEB-INF/self.tld</taglib-location> </taglib> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>true</el-ignored> <page-encoding>UTF-8</page-encoding> <scripting-invalid>false</scripting-invalid> </jsp-property-group> <jsp-property-group> <url-pattern>*.html</url-pattern> <el-ignored>true</el-ignored> <page-encoding>UTF-8</page-encoding> <scripting-invalid>false</scripting-invalid> </jsp-property-group> </jsp-config> </web-app>
ListTag处理类代码如下:
package com.self.framework.ui.tag; import java.util.Iterator; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class ListTag extends BodyTagSupport { private static final long serialVersionUID = -8112121551855679060L; private Iterator it; private String name; private String value; @Override public int doStartTag() throws JspException { System.out.println("doStartTag..."); List list = (List)pageContext.findAttribute(value); if(list==null && list.size() > 0) return SKIP_BODY; it = list.iterator(); if (it.hasNext()) { pageContext.setAttribute(name, it.next()); } return EVAL_BODY_INCLUDE; } @Override public int doAfterBody() throws JspException { System.out.println("doAfterBody..."); if (it.hasNext()) { pageContext.setAttribute(name, it.next()); return EVAL_BODY_AGAIN; } return SKIP_BODY; } @Override public int doEndTag() throws JspException { System.out.println("doEndTag..."); return EVAL_PAGE; } @Override public void doInitBody() throws JspException { System.out.println("doInitBody..."); super.doInitBody(); } @Override public BodyContent getBodyContent() { System.out.println("getBodyContent..."); return super.getBodyContent(); } @Override public void release() { System.out.println("list release..."); super.release(); } @Override public void setBodyContent(BodyContent b) { System.out.println("setBodyContent..."); super.setBodyContent(b); } public void setName(String name) { this.name = name; } public void setValue(String value) { this.value = value; } }
index.jsp 使用代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="self" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Self Tag</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% List<String> list = new ArrayList<String>(); list.add("jsp tag..."); list.add("android..."); list.add("j2ee..."); request.setAttribute("list", list); %> <!-- 1:<s:date pattern="yyyy-MM-dd"/><br> 2:<s:date pattern="yyyy.MM.dd HH.mm.ss" /><br> 3:<s:iterator count="10">HelloWorld!</s:iterator><br> --> list:<s:list name="it" value="list"> <% try { pageContext.getOut().write(pageContext.getAttribute("it").toString()); } catch (Exception e) { e.printStackTrace(); } %> </s:list> </body> </html>
最终效果:
备注说明:
EVAL_BODY_INCLUDE:把Body读入存在的输出流中,doStartTag()函数可用
EVAL_PAGE:继续处理页面,doEndTag()函数可用
SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
EVAL_BODY_TAG:已经废止,由EVAL_BODY_BUFFERED取代
EVAL_BODY_BUFFERED:申请缓冲区,由setBodyContent()函数得到的BodyContent对象来处理tag的body,如果类实现了BodyTag,那么doStartTag()可用,否则非法
EVAL_BODY_BUFFERED 要将BodyContent的内容输出
EVAL_PAGE:继续处理页面,doEndTag()函数可用
SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
EVAL_BODY_TAG:已经废止,由EVAL_BODY_BUFFERED取代
EVAL_BODY_BUFFERED:申请缓冲区,由setBodyContent()函数得到的BodyContent对象来处理tag的body,如果类实现了BodyTag,那么doStartTag()可用,否则非法
EVAL_BODY_BUFFERED 要将BodyContent的内容输出