1. 使用標籤控制頁面邏輯案例: 類比sun公司開發的標籤–開發<c:if>標籤–開發<c:if><c:else>標籤–開發迭代標籤開發<c:if>標籤案例:標籤控制類:package com.csdn.web.example;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class Demo1 extends SimpleTagSupport {private boolean test;public void setTest(boolean test) {this.test = test;}@Overridepublic void doTag() throws JspException, IOException {if(test){JspFragment jf = this.getJspBody();jf.invoke(null);}}}Tld描述文檔:<?xml version="1.0" encoding="UTF-8" ?><taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0"><tlib-version>1.0</tlib-version><short-name>example</short-name><uri>example</uri><tag><name>if</name><tag-class>com.csdn.web.example.Demo1</tag-class><body-content>scriptless</body-content><attribute><name>test</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag><tag><name>choose</name><tag-class>com.csdn.web.example.ChooseTag</tag-class><body-content>scriptless</body-content></tag><tag><name>when</name><tag-class>com.csdn.web.example.WhenTag</tag-class><body-content>scriptless</body-content><attribute><name>test</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag><tag><name>otherwise</name><tag-class>com.csdn.web.example.OtherwiseTag</tag-class><body-content>scriptless</body-content></tag><tag><name>forEach</name><tag-class>com.csdn.web.example.ForEach</tag-class><body-content>scriptless</body-content><attribute><name>var</name><required>true</required></attribute><attribute><name>items</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>JSP檔案:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="example" prefix="example"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP '1.jsp' starting page</title> </head> <body> <example:if test="${ user==null}"> <h5>aaaaaaaaaaa</h5> </example:if> </body></html>開發迭代(forEach)標籤標籤控制類:package com.csdn.web.example;import java.io.IOException;import java.util.Iterator;import java.util.List;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ForEach extends SimpleTagSupport {private String var;private List items;public void setVar(String var) {this.var = var;}public void setItems(List items) {this.items = items;}@Overridepublic void doTag() throws JspException, IOException {Iterator it = items.iterator();while(it.hasNext()){Object obj = it.next();this.getJspContext().setAttribute(var,obj);this.getJspBody().invoke(null);}}}JSP檔案:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="example" prefix="example"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP '4.jsp' starting page</title> </head> <body> <% List list = new ArrayList(); list.add(1); list.add("aa"); list.add("bb"); list.add(2); request.setAttribute("list",list); %> <example:forEach var="str" items="${list}"> ${ str }<br> </example:forEach> </body></html>開發choose標籤標籤控制類:ChooseTag.javapackage com.csdn.web.example;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ChooseTag extends SimpleTagSupport {private boolean isOk;public boolean isOk() {return isOk;}public void setOk(boolean isOk) {this.isOk = isOk;}@Overridepublic void doTag() throws JspException, IOException {this.getJspBody().invoke(null);}}WhenTag.javapackage com.csdn.web.example;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class WhenTag extends SimpleTagSupport {private boolean test;public void setTest(boolean test) {this.test = test;}@Overridepublic void doTag() throws JspException, IOException {ChooseTag parent = (ChooseTag) this.getParent();if(test && !parent.isOk()){this.getJspBody().invoke(null);parent.setOk(true);}}}OtherwiseTag.javapackage com.csdn.web.example;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class OtherwiseTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {ChooseTag parent = (ChooseTag)this.getParent();if(!parent.isOk()){this.getJspBody().invoke(null);parent.setOk(true);}}}JSP檔案:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="example" prefix="example"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP '3.jsp' starting page</title> </head> <body> <example:choose> <example:when test="<%=false %>">asd</example:when><!-- 這裡,標籤體中如果沒有內容會報錯(null 指標異常) --> <example:otherwise>fghhjj</example:otherwise> </example:choose> </body></html>2. 其他案例設定帶屬性的標籤:標籤控制類package com.csdn.web.simple;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class Demo5 extends SimpleTagSupport{private int count;public void setCount(int count) {this.count = count;}@Overridepublic void doTag() throws JspException, IOException {JspFragment jf = this.getJspBody();for(int i=0;i<=count;i++){jf.invoke(null);}}}Tld描述文檔<?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>simple</short-name> <uri>http://www.csdn.com</uri> <tag> <name>demo1</name> <tag-class>com.csdn.web.simple.Demo1</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>demo2</name> <tag-class>com.csdn.web.simple.Demo2</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>demo3</name> <tag-class>com.csdn.web.simple.Demo3</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>demo4</name> <tag-class>com.csdn.web.simple.Demo4</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>demo5</name> <tag-class>com.csdn.web.simple.Demo5</tag-class> <body-content>scriptless</body-content> <attribute> <name>count</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>JSP檔案<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://www.csdn.com" prefix="simple"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>設定帶屬性的標籤</title> </head> <body> <simple:demo5 count="10"> <h3>dddddddddd</h3> </simple:demo5> </body></html>修改標籤體內容:標籤控制類package com.csdn.web.simple;import java.io.IOException;import java.io.StringWriter;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class Demo4 extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {JspFragment jf = this.getJspBody();StringWriter sw = new StringWriter();jf.invoke(sw);String body = sw.toString().toUpperCase();this.getJspContext().getOut().write(body);}}JSP檔案<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://www.csdn.com" prefix="simple"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>修改標籤體內容</title> </head> <body> <simple:demo4> <h3>nnnnnnnnnnnn</h3> </simple:demo4> </body></html>