<%@ taglib prefix="標籤前綴" uri="tld文件路徑"%>
,而且調用<標籤前綴 jsp標籤使用名稱>
。package taeyeon.com.jsp.tld; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import java.io.IOException; public class HelloTag extends TagSupport { @Override public int doStartTag() throws JspException { JspWriter writer = super.pageContext.getOut(); // 取得jsp的輸出流對象 try { writer.println("<h2>Hello World!</h2>"); } catch (IOException e) { e.printStackTrace(); } return super.SKIP_BODY;//沒有標籤體 } }
<?xml version="1.0" encoding="ISO-8859-1"?> <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>hello</short-name><!--標籤庫在tld中的描述名稱--> <uri>http://mycompany.com</uri><!--jsp頁面中taglib標籤中的uri映射路徑,可本身定義。只要知足書寫標準--> <tag> <name>hello</name><!--在jsp中使用的名稱--> <tag-class>taeyeon.com.jsp.tld.HelloTag</tag-class><!--標籤指向的操做類--> <body-content>empty</body-content><!--是否有標籤體--> </tag> </taglib>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="hello" uri="/WEB-INF/tld/hello.tld" %> <html> <head> <title>第一個tld標籤頁面</title> </head> <body> <hello:hello/> </body> </html>
輸出html
Hello World!
<!--映射tag的uri,操做tld文件--> <jsp-config> <taglib> <taglib-uri>hello_uri</taglib-uri> <taglib-location>/WEB-INF/tld/hello.tld</taglib-location> </taglib> </jsp-config>
注意:這裏在web.xml文件中配置的 <jsp-config>,在web.xml2.4版本以前是能夠書寫的,可是在以後書寫會報錯,由於tld文件中新增了一個標籤<uri>能夠直接映射jsp頁面中的引用uri,因此只要在tld文件中書寫就能夠了。具體在tld文本的那一個版本修改的有興趣的能夠本身查閱一下。前端
package taeyeon.com.jsp.tld; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateTag extends TagSupport { private String formateDate; public String getFormateDate() { return formateDate; } public void setFormateDate(String formateDate) { this.formateDate = formateDate; } @Override public int doStartTag() throws JspException { LocalDateTime date = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(this.formateDate); try { super.pageContext.getOut().write(date.format(formatter)); } catch (IOException e) { e.printStackTrace(); } return TagSupport.SKIP_BODY; } }
<?xml version="1.0" encoding="ISO-8859-1"?> <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>myshortname</short-name> <uri>dateuri</uri> <tag> <name>date</name> <tag-class>taeyeon.com.jsp.tld.DateTag</tag-class> <body-content>empty</body-content> <attribute> <name>formateDate</name><!--屬性名稱--> <required>true</required><!--是否爲必輸項--> <rtexprvalue>true</rtexprvalue><!--是否支持表達式輸出--> </attribute> </tag> </taglib>
-引用tldjava
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="date" uri="dateuri" %> <html> <head> <title>帶屬性的標籤體</title> </head> <body> <h2><date:date formateDate="yyyy-MM-dd HH:mm:ss"/></h2> </body> </html>
2019-12-02 17:03:28
注意:這裏沒有用SimpleDateFormat類來格式化時期,實用爲SimpleDateFormat類時非線程安全的,而在jdk1.8以後提供了新的DateTimeFormatter類,該類線程安全也是做用於日期的格式化,二者的具體不一樣和使用,怎樣讓SimpleDateFormat變的線程安全能夠參考我後面博文寫的文章web
<名稱: tld中name名稱 屬性="${}"/屬性="<%= %>"/>
public class TagSupport extends Object implements IterationTag,Serializable
public interface IterationTag extends Tag { public final static int EVAL_BODY_AGAIN = 2; int doAfterBody() throws JspException;
public interface Tag extends JspTag { public final static int SKIP_BODY = 0; public final static int EVAL_BODY_INCLUDE = 1; public final static int SKIP_PAGE = 5; public final static int EVAL_PAGE = 6; void setPageContext(PageContext pc); void setParent(Tag t); Tag getParent(); int doStartTag() throws JspException; int doEndTag() throws JspException; void release(); }
NO | 常量或方法 | 類型 | 描述 |
---|---|---|---|
1 | protected PageContext pageContext | 屬性 | 表示PageContext對象,能夠操做四種屬性範圍 |
2 | public static final int SKIP_BODY | 常量 | 忽略標籤體內容,將操做轉交給doEndTag() |
3 | public static final int EVAL_BODY_INCLUDE | 常量 | 正常執行標籤體操做,但不處理任何運算 |
4 | public final static int SKIP_PAGE | 常量 | 全部在jsp上操做都將中止,會將全部輸出的內容馬上顯示在瀏覽器上 |
5 | public final static int EVAL_PAGE | 常量 | 正常執行jsp頁面 |
6 | public final static int EVAL_BODY_AGAIN | 常量 | 重複執行標籤內容,會再次調用doAfterBody(),直到出現SKIP_BODY爲止 |
7 | public abstract int doStartTag() throws JspException | 方法 | 處理標籤開始部分 |
8 | public abstract int doEndTag() throws JspException | 方法 | 處理標籤結束部分 |
9 | public abstract int doAfterBody() throws JspException | 方法 | 處理標籤主體部分 |
10 | public abstract void release() | 方法 | 釋放標籤資源 |
package taeyeon.com.jsp.tld; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; public class BodyTag extends TagSupport { private String name; private String scope; public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int doStartTag() throws JspException { Object value=null; if("page".equals(this.scope)){ value=super.pageContext.getAttribute(name , PageContext.PAGE_SCOPE); }else if("request".equals(this.scope)){ value=super.pageContext.getAttribute(name , PageContext.REQUEST_SCOPE); }else if("session".equals(this.scope)){ value=super.pageContext.getAttribute(name , PageContext.SESSION_SCOPE); }else if("application".equals(this.scope)){ value=super.pageContext.getAttribute(name , PageContext.APPLICATION_SCOPE); } if(value==null){ return super.SKIP_BODY; } else { return super.EVAL_BODY_INCLUDE; } } }
<?xml version="1.0" encoding="ISO-8859-1"?> <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>myshortname</short-name> <uri>bodytag</uri> <tag> <name>body</name> <tag-class>taeyeon.com.jsp.tld.BodyTag</tag-class> <body-content>JSP</body-content><!-- 執行標籤體內容--> <attribute> <name>scope</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="body" uri="bodytag" %> <html> <head> <title>有標籤體的標籤</title> </head> <body> <%! String scope = "session";%> <% session.setAttribute("name", "Yoona"); %> <body:body name="name" scope="<%=scope%>"> <h2>session屬性範圍</h2> </body:body> </body> </html>
session屬性範圍
注意:編程
package taeyeon.com.jsp.tld; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import java.util.Iterator; import java.util.List; public class IterationTag extends TagSupport { private String name; private String scope; private Iterator<?> iter; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public Iterator<?> getIter() { return iter; } public void setIter(Iterator<?> iter) { this.iter = iter; } @Override public int doStartTag() throws JspException { Object value = null; if ("page".equals(this.scope)) { value = super.pageContext.getAttribute(name, PageContext.PAGE_SCOPE); } else if ("request".equals(this.scope)) { value = super.pageContext.getAttribute(name, PageContext.REQUEST_SCOPE); } else if ("session".equals(this.scope)) { value = super.pageContext.getAttribute(name, PageContext.SESSION_SCOPE); } else if ("application".equals(this.scope)) { value = super.pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE); } if (value != null && value instanceof List<?>) { this.iter = ((List<?>) value).iterator(); if (iter.hasNext()) { super.pageContext.setAttribute("msg", iter.next()); return super.EVAL_BODY_INCLUDE; } else { return super.SKIP_BODY; } } else { return super.SKIP_BODY; } } @Override public int doAfterBody() throws JspException { if (iter.hasNext()) { super.pageContext.setAttribute("msg", iter.next()); return super.EVAL_BODY_AGAIN; } else { return super.SKIP_BODY; } } }
<?xml version="1.0" encoding="ISO-8859-1"?> <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>myshortname</short-name> <uri>iteratortag</uri> <tag> <name>iterator</name> <tag-class>taeyeon.com.jsp.tld.IterationTag</tag-class> <body-content>JSP</body-content> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>scope</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="iterator" uri="iteratortag" %> <html> <head> <title>迭代標籤庫</title> </head> <body> <% String name1 = "session"; List<String> list = new ArrayList<String>(); list.add("18"); list.add("yoona"); list.add("Korea"); session.setAttribute("name", list); %> <iterator:iterator name="name" scope="<%=name1%>"> <h2>${msg}</h2> </iterator:iterator> </body> </html>
18 yoona Korea
在獲取屬性值的時候必定要注意訪問域的問題,否則有可能獲取不到值。瀏覽器
public class BodyTagSupport extends TagSupport implements BodyTag
public interface BodyTag extends IterationTag { public final static int EVAL_BODY_TAG = 2; public final static int EVAL_BODY_BUFFERED = 2; void setBodyContent(BodyContent b); void doInitBody() throws JspException; }
NO | 方法 | 類型 | 描述 |
---|---|---|---|
1 | public final static int EVAL_BODY_BUFFERED | 常量 | 表示標籤體的內容應該被處理,全部的處理結果都將保存在BodyContent類中 |
2 | protected BodyContent bodyContent | 屬性 | 存放標籤體的處理結果 |
3 | public JspWriter getPreviousOut() | 方法 | 取得JspWriter的輸出流對象 |
public abstract class BodyContent extends JspWriter
NO | 方法 | 類型 | 描述 |
---|---|---|---|
1 | public abstract void writeOut(Writer out) throws IOException | 方法 | 指定BodyContent內容的輸出流對象,並進行內容輸出 |
2 | public abstract String getString() | 將全部內容變爲String類型 | |
3 | public abstract Reader getReader() | 方法 | 將內容變爲Reader對象 |