自定義標籤的優勢:
- 自定義標籤可以降低jsp開發的複雜度和維護量,從html角度來說,可以使html不用去過多的關注那些比較複雜的商業邏輯(業務邏輯)。
- 利用自定義標籤,可以軟件開發人員和頁面設計人員合理分工:頁面設計人員可以把精力集中在使用標籤(HTML,XML中或者JSP)創建網站上,而軟件開發人員則可以將精力集中在實現底層功能上面,如國際化等,從而提高了工程生產力將具有共用特性的tag庫應用於不同的項目中,體現了軟件複用的思想。
步驟:
- 創建一個標籤處理器類,並且實現SimpleTag接口(或者直接繼承SimpleTagSupport)
- 在WEB-INF文件夾下新建一個.tld(標籤庫描述文件)爲擴展名xml文件,並拷入固定的部分:並對描述文件進行處理,如下:
<description>HelloTag</description>
<display-name>Hello core</display-name>
<tlib-version>1.0</tlib-version>
<!-- 建議在JSP頁面上使用的前綴 -->
<short-name>h</short-name>
<!-- 作爲tld文件的id,用來唯一標識當前文件的TLD文件,
多個tld文件的URI不能重複,通過JSP頁面的taglib的uri引用 -->
<uri>http://www.hxz.com/mytaag/core</uri>
3.對自定義標籤進行描述:
<!-- 描述自定義的HelloTag標籤 -->
<tag>
<!-- 標籤的名字 -->
<name>hello</name>
<!-- 標籤所在的類名 -->
<tag-class>com.xz.tag.HelloTag</tag-class>
<!-- 標籤的類型 -->
<body-content>empty</body-content>
</tag>
4.在頁面中進行使用;
附源文件:
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">
<!-- 描述TLD文件 -->
<description>HelloTag</description>
<display-name>Hello core</display-name>
<tlib-version>1.0</tlib-version>
<!-- 建議在JSP頁面上使用的前綴 -->
<short-name>h</short-name>
<!-- 作爲tld文件的id,用來唯一標識當前文件的TLD文件,
多個tld文件的URI不能重複,通過JSP頁面的taglib的uri引用 -->
<uri>http://www.hxz.com/mytaag/core</uri>
<!-- 描述自定義的HelloTag標籤 -->
<tag>
<!-- 標籤的名字 -->
<name>hello</name>
<!-- 標籤所在的類名 -->
<tag-class>com.xz.tag.HelloTag</tag-class>
<!-- 標籤的類型 -->
<body-content>empty</body-content>
</tag>
<tag>
<name>printcount</name>
<tag-class>com.xz.tag.NameCountTag</tag-class>
<body-content>empty</body-content>
<attribute>
<!-- 標籤名 -->
<name>name</name>
<!-- 是否爲必須 -->
<required>true</required>
<!-- rtexprvalue->[runtime expression value]
表示當前標籤是否接受運行時動態表達式的值 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>foreach</name>
<tag-class>com.xz.tag.TestForeach</tag-class>
<body-content>scriptless</body-content>
<attribute>
<!-- 標籤名 -->
<name>items</name>
<!-- 是否爲必須 -->
<required>true</required>
<!-- rtexprvalue->[runtime expression value]
表示當前標籤是否接受運行時動態表達式的值 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>testJspFragMent</name>
<tag-class>com.xz.tag.TestJspFragment</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
繼承自SimpleTag類的彷ForEach類:
package com.xz.tag;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TestForeach extends SimpleTagSupport {
private Collection<?> items;
public void setItems(Collection<?> items) {
this.items = items;
}
private String var;
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
//*遍歷items對應的集合
if(items!=null) {
for(Object obj:items) {
//把正在遍歷裏的對象輸出到pageContext中,鍵:var 值:正在便利的對象
getJspContext().setAttribute(var, obj);
//把標籤體的內容直接輸出到頁面上
getJspBody().invoke(null);
}
}
super.doTag();
}
}
遍歷所需的Demo類:
package com.xz.pojo;
public class User {
private String name;
private String age;
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public User(String name, String age) {
super();
this.name = name;
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
JSP頁面:
<%@page import="java.util.ArrayList"%>
<%@page import="com.xz.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="hxz" uri="http://www.hxz.com/mytaag/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList<User> users=new ArrayList<User>();
users.add(new User("hxz","11"));
users.add(new User("zy","12"));
users.add(new User("xy","13"));
request.setAttribute("users", users);
%>
<hxz:foreach var="user" items="${requestScope.users }">
姓名:${user.name } 年齡${user.age }<br>
</hxz:foreach>
<hxz:testJspFragMent>Hello World!</hxz:testJspFragMent>
</body>
</html>
運行結果: