- 开发自定义标签处理类
- 建立一个*.tld文件,每个tld文件对应一个标签库,每个标签库包含多个标签
- 在JSP文件中使用自定义标签
开发自定义标签类
继承一个父类:javax.sesrvlet.jsp.tagext.SimpleTagSupport
如果标签类包含属性,每个属性都有对那个的getter和setter方法
重写doTag方法,这个方法负责生成页面内容。
public class ShowCurrentTimeTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
Date now=new Date();
DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str=dFormat.format(now);
PageContext pc=(PageContext) getJspContext();
pc.getOut().write(str);
}
}
在WEB-INF目录下,建立一个扩展名为tld(Tag Libary Definition)的xml文件。
注:tld如果在WEB-INF或者在jar包的META-INF目录下,都能自动找到。
<!--myTag.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>myTag</short-name>
<uri>http://www.cai.com/myTag</uri>
<tag>
<description>show current time</description>
<name>showTime</name>
<tag-class>com.cai.tag.ShowCurrentTimeTag</tag-class>
<!-- 开始和结束标签之间的内容类型 -->
<body-content>empty</body-content>
</tag>
</taglib>
如果tld不在WEB-INF或jar的META-INF目录下,修改web.xml,对tld和url进行一个映射
<!--web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<jsp-config>
<taglib>
<taglib-uri>http://www.cai.com/myTag</taglib-uri>
<taglib-location>/tld/myTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
在JSP中使用tag
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="myTag" uri="http://www.cai.com/myTag" %>
<!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>
<myTag:showTime/>
</body>
</html>
带属性的标签
需要为tag元素添加attribute元素
1. name:设置属性名,即变量名
2. required:设置属性是否为必须属性
3. fragment:该属性是否zhichiJSP脚本,表达式等动态内容,子元素的值是true或false
//Person.java
public class Person extends SimpleTagSupport {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void doTag() throws JspException, IOException {
Writer out=getJspContext().getOut();
out.write("用户名:"+userName+"<br />");
out.write("密码:"+password);
}
}
<!--myTag.tld-->
<tag>
<description>show current time</description>
<name>ShowInfo</name>
<tag-class>com.cai.tag.Person</tag-class>
<!-- 开始和结束标签之间的内容类型 -->
<body-content>empty</body-content>
<attribute>
<name>userName</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>password</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
<myTag:ShowInfo password="fdsfsdf" userName="gsdgsdg"/>
带标签体的标签
//IteratorTag.java
public class IteratorTag extends SimpleTagSupport {
private String collection;
private String item;
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
@Override
public void doTag() throws JspException, IOException {
//从page scope中获取属性名为collection的集合
Collection itemList=(Collection) getJspContext().getAttribute(collection);
//遍历集合
for(Object s:itemList) {
//将集合的元素设置到page范围
getJspContext().setAttribute(item, s);
//输出标签体
getJspBody().invoke(null);
}
}
}
<!--myTag.xml-->
<tag>
<name>iterator</name>
<tag-class>com.cai.tag.IteratorTag</tag-class>
<!-- 开始和结束标签之间的内容类型 -->
<body-content>scriptless</body-content>
<attribute>
<name>collection</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
<!--Demo3.jsp-->
<body>
<h2>带标签体的标签-迭代器标签</h2>
<%
List<String> a=new ArrayList<String>();
a.add("one");
a.add("two");
a.add("three");
pageContext.setAttribute("a", a);
%>
<table border="1" bgcolor="#aaaadd" width="300">
<myTag:iterator item="item" collection="a">
<tr>
<td>${pageScope.item}</td>
</tr>
</myTag:iterator>
</table>
</body>
以页面片段为属性的标签
//FragmentTag.java
public class FragmentTag extends SimpleTagSupport {
private JspFragment fragment;
public JspFragment getFragment() {
return fragment;
}
public void setFragment(JspFragment fragment) {
this.fragment = fragment;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out=getJspContext().getOut();
out.println("<div style='padding:10px;border:1px solid black;'>");
out.println("<h3>下面是动态传入的JSP片段</h3>");
fragment.invoke(null);
out.println("</div>");
}
}
<!--myTag.xml-->
<tag>
<name>fragment</name>
<tag-class>com.cai.tag.FragmentTag</tag-class>
<!-- 开始和结束标签之间的内容类型 -->
<body-content>empty</body-content>
<attribute>
<name>fragment</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
<!--Demo4.jsp-->
<body>
<h2>下面显示的是自定义标签的内容</h2>
<myTag:fragment>
<jsp:attribute name="fragment">
<myTag:showTime/>
</jsp:attribute>
</myTag:fragment>
<br/>
<myTag:fragment>
<jsp:attribute name="fragment">
${pageContext.request.remoteAddr}
</jsp:attribute>
</myTag:fragment>
</body>
动态属性的标签
用于处理属性个数和属性名个数不确定的情况
1. 还需实现DynamicAttributes接口
2. 配置标签时通过dynamic-attributes指定标签支持动态属性
public class DynaAttributeTag extends SimpleTagSupport implements DynamicAttributes {
//保存每个属性名的集合
private ArrayList<String> keys=new ArrayList<String>();
//保存每个属性值的集合
private ArrayList<Object> values=new ArrayList<Object>();
@Override
public void doTag() throws JspException, IOException {
JspWriter out=getJspContext().getOut();
out.println("<ol>");
for(int i=0;i<keys.size();i++) {
String key=keys.get(i);
Object value=values.get(i);
out.println("<li>"+key+"="+value+"</li>");
}
out.println("</ol>");
}
@Override
public void setDynamicAttribute(String arg0, String arg1, Object arg2) throws JspException {
//添加属性名
keys.add(arg1);
//添加属性值
values.add(arg2);
}
}
<tag>
<name>dynaAttr</name>
<tag-class>com.cai.tag.DynaAttributeTag</tag-class>
<!-- 开始和结束标签之间的内容类型 -->
<body-content>empty</body-content>
<dynamic-attributes>true</dynamic-attributes>
</tag>
<body>
<myTag:dynaAttr name="aaaaaa" password="fdsfsdf"/>
</body>