package foo;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class SimpleTagTest1 extends SimpleTagSupport {
// 標籤處理代碼
}
public void doTag() throws JspException, IOException {
getJspContext().getOut().print("This is xxxxxx");
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="https://java.sun.com/xml/ns/j2ee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.2</tlib-version>
<uri>simpleTags</uri>
<tag>
<name>simple1</name>
<description>xxxxxxxx</description>
<tag-class>foo.SimpleTagTest1</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
TLD 放 WEB-INF,標籤處理器放 WEB-INF/classes (遵循包結構)。
<%@ taglib prefix="myTags" uri="simpleTags" %>
<html><body>
<myTags:simple1 />
</body></html>
URI 需與 TLD 中 uri 一致。
<myTags:simple2> This is the body </myTags:simple2>
doTag() 加入 getJspBody().invoke(null);,TLD body-content 改為 scriptless。
<myTags:simple3> Message is : ${message} </myTags:simple3>
doTag() 設定屬性:getJspContext().setAttribute("message","wear sunscreen"); getJspBody().invoke(null);
<table>
<myTags:simple3>
<tr><td>${movie}</td></tr>
</myTags:simple3>
</table>
doTag() 迴圈設定屬性並多次 invoke:
String[] movies = {"Monsoon Wedding", "Saved!", "..."};
public void doTag() throws JspException, IOException {
for(int i=0; i
<myTags:simple5 movieList="${movieCollection}">
<tr><td>${movie.name}</td><td>${movie.genre}</td></tr>
</myTags:simple5>
TLD 加入 attribute 定義,標籤處理器加入對應屬性與 setter。
empty:標籤主體為空scriptless:無腳本元素,可有模板文字、EL、自定義/標準動作tagdependent:主體視為純文字,不計算 EL、不觸發標籤/動作JSP:JSP 允許的內容皆可放入轉自:https://www.cnblogs.com/elaron/archive/2012/10/10/2717797.html