JSP 自定義標籤
自定義標籤是用戶定義的JSP語言元素。當JSP頁面包含一個自定義標籤時將被轉化爲servlet,標籤轉化爲對被稱爲tag handler的對象的操作,即當servlet執行時Web container調用那些操作。JSP標籤擴展可以讓你創建新的標籤並且可以直接插入到一個JSP頁面。 JSP 2.0規範中引入Simple Tag Handlers來編寫這些自定義標記。
可以繼承SimpleTagSupport類並重寫的doTag()方法來開發一個最簡單的自定義標籤。
接下來,我們想創建一個自定義標籤叫作<ex:Hello>,標籤格式爲:
創建"Hello"標籤
<ex:Hello />要創建自定義的JSP標籤,你首先必須創建處理標籤的Java類。所以,讓我們創建一個HelloTag類,如下所示:
package Demo;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloTag extends SimpleTagSupport{
StringWriter sw = new StringWriter();
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("你好林佳炎");
}
}
標籤處理類創建好之後 在WEB-INF下創建tld文件
在 Eclipse 相應工程中右鍵單擊 WEB-INF 目錄彈出選項框,依次選:->New->Other->XML->XML File->next->要取的文件名.tld
->next->Create XML File from an XML schema file
->next->Select XML Catalog entry
選擇 http://xmlns.jcp.org/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd 這一項,點擊 Finish 即可。
以下是tld代碼
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>empty</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
接下來,我們就可以在JSP文件中使用Hello標籤
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>empty</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
輸出結果爲 你好林佳炎
訪問標籤體
你可以像標準標籤庫一樣在標籤中包含消息內容。如我們要在我們自定義的Hello中包含內容,格式如下:
<ex:Hello>
This is message body
</ex:Hello>
import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }
接下來修改tld文件
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>scriptless</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
把empty改爲scriptless
現在我們可以在JSP使用修改後的標籤,如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="ex" uri="/WEB-INF/hellotag.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>
</head>
<body>
<h1>666</h1>
<ex:Hello>
你好啊你好啊
</ex:Hello>
</body>
</html>
以上程序輸出結果如下所示:
你好啊你好啊