Jsp自定义标签
标签的运行原理:
容器依据JSP标签的命名空间uri找到标签的描述文件tld文件,然后依据标签名字(helloWorld)找到标签类(com.java1234.tag.HelloWorldTag)。接下来实例化该标签,同时属性值赋给参数,调用doTag方法。
最简单的自定义标签<java1234:helloWorld/> , 输出一句话。

public class HelloWorldTag extends TagSupport{ /** * */ private static final long serialVersionUID = 1L; @Override public int doStartTag() throws JspException { JspWriter out=this.pageContext.getOut(); try { out.println("自定义标签大爷你好!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return TagSupport.SKIP_BODY; // 直接结束标签 } } ---------------------java1234.tld------------------------------------------ <tag> <name>helloWorld</name> <tag-class> com.java1234.tag.HelloWorldTag </tag-class> <body-content>empty</body-content> </tag> -------------------------HelloWorldTag.jsp-------------------------------- <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> <body> <java1234:helloWorld/> </body>
自定义有属性的标签
<java1234:helloWorld2 name="JspServlet屌炸天"/> name值会作为参数传入tag类。

public class HelloWorldTag2 extends TagSupport{ /** * */ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int doStartTag() throws JspException { JspWriter out=this.pageContext.getOut(); try { out.println(name+"自定义标签大爷你好!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return TagSupport.SKIP_BODY; // 直接结束标签 } } ------------------------------------------------------------------------ <tag> <name>helloWorld2</name> <tag-class> com.java1234.tag.HelloWorldTag2 </tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> <!-- 属性必须要填 --> <rtexprvalue>true</rtexprvalue> <!-- 是否支持EL表达式 --> </attribute> </tag> ------------------------------------------------------------------------ <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> <body> <java1234:helloWorld2 name="JspServlet屌炸天"/> </body>
自定义有标签体的标签(注意:tag类里面是老版本的写法,继承TagSupport)
<java1234:iterate items="people" var="p">
<h2>${p }</h2>
</java1234:iterate>

public class IterateTag extends TagSupport{ /** * */ private static final long serialVersionUID = 1L; private String var; private String items; private Iterator iter; public String getVar() { return var; } public void setVar(String var) { this.var = var; } public String getItems() { return items; } public void setItems(String items) { this.items = items; } public Iterator getIter() { return iter; } public void setIter(Iterator iter) { this.iter = iter; } @Override public int doStartTag() throws JspException { Object value=this.pageContext.getAttribute(items); if(value!=null && value instanceof List){ this.iter=((List)value).iterator(); if(iter.hasNext()){ this.pageContext.setAttribute(var, iter.next()); return TagSupport.EVAL_BODY_INCLUDE; // 执行标签体 }else{ return TagSupport.SKIP_BODY; // 退出标签执行 } }else{ return TagSupport.SKIP_BODY; // 退出标签执行 } } @Override public int doAfterBody() throws JspException { if(iter.hasNext()){ this.pageContext.setAttribute(var, iter.next()); return TagSupport.EVAL_BODY_AGAIN; // 再执行一次标签体 }else{ return TagSupport.SKIP_BODY; // 退出标签执行 } } } ------------------------------------------------------------------------ <tag> <name>iterate</name> <tag-class> com.java1234.tag.IterateTag </tag-class> <body-content>JSP</body-content> <!-- 代表有标签体,老版本的写法 --> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> --------------------------------------------------------------------- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <% List people=new ArrayList(); people.add("王二小"); people.add("丝丝光"); people.add("草泥马"); pageContext.setAttribute("people", people); %> </head> <body> <java1234:iterate items="people" var="p"> <h2>${p }</h2> </java1234:iterate> </body> </html>
简单标签(注意:JSP2.0 新定义的SimpleTagSupport,比老版本TagSupport使用简单)
<java1234:iterate2 items="people" var="p">
<h2>${p }</h2> <!-- 这个就是标签体 -->
</java1234:iterate2>

//JSP2.0 新定义的SimpleTagSupport,比老版本TagSupport使用简单 public class IterateSimpleTag extends SimpleTagSupport{ /** * */ private static final long serialVersionUID = 1L; private String var; private String items; public String getVar() { return var; } public void setVar(String var) { this.var = var; } public String getItems() { return items; } public void setItems(String items) { this.items = items; } @Override public void doTag() throws JspException, IOException { Object value=this.getJspContext().getAttribute(items); if(value!=null && value instanceof List){ Iterator iter=((List)value).iterator(); while(iter.hasNext()){ this.getJspContext().setAttribute(var, iter.next()); this.getJspBody().invoke(null); // 响应页面 } } } } ------------------------------------------------------------------- <tag> <name>iterate2</name> <tag-class> com.java1234.tag.IterateSimpleTag </tag-class> <body-content>scriptless</body-content><!-- JSP2.0 继承SimpleTagSupport,代表有标签体,指运行脚本 --> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> --------------------------------------------------------- <% List people=new ArrayList(); people.add("王二小2"); people.add("丝丝光2"); people.add("草泥马2"); pageContext.setAttribute("people", people); %> </head> <body> <java1234:iterate2 items="people" var="p"> <h2>${p }</h2> </java1234:iterate2> </body>
标签文件总览:

<?xml version="1.0" encoding="UTF-8" ?> <taglib 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-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>java1234Tag</short-name> <tag> <name>helloWorld</name> <tag-class> com.java1234.tag.HelloWorldTag </tag-class> <body-content>empty</body-content> </tag> <tag> <name>helloWorld2</name> <tag-class> com.java1234.tag.HelloWorldTag2 </tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> <!-- 属性必须要填 --> <rtexprvalue>true</rtexprvalue> <!-- 是否支持EL表达式 --> </attribute> </tag> <tag> <name>iterate</name> <tag-class> com.java1234.tag.IterateTag </tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>iterate2</name> <tag-class> com.java1234.tag.IterateSimpleTag </tag-class> <body-content>scriptless</body-content> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>