1、开发自定义标签类,该类继承自javax.servlet.jsp.tagext.SimpleTagSupport
如果标签类包含属性,必须有对应的setter和getter方法
重写doTag()方法,负责生成页面内容
2、建立Tld文件,路径为web-inf目录下
3、使用标签库
在页面中引入<%@ taglib="myTag" uri="http://www.royzhou.com/myTagLib" %>
使用<mytag:HelloWorld userName="royzhou" />输出可以预览效:
4、带标签体的标签
迭代器标签开发处理类
tld文件配置
测试页面:
如果标签类包含属性,必须有对应的setter和getter方法
重写doTag()方法,负责生成页面内容
- package com.royzhou.hello;
- import java.io.IOException;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- public class HelloTag extends SimpleTagSupport{
- private String userName;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public void doTag() throws JspException, IOException {
- getJspContext().getOut().write("Welcome " + userName + "!");
- }
- }
2、建立Tld文件,路径为web-inf目录下
- <?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>myTagLib</short-name>//简写
- <uri>http://www.royzhou.com/myTagLib</uri>//指定标签库的URI,重要属性,在使用标签库的时候用到
- 标签库可包含多个tag
- <tag>
- <name>HelloWorld</name> //标签名称
- <tag-class>com.royzhou.hello.HelloTag</tag-class> //处理类
- <body-content>empty</body-content> //常用的是empty和scriptless
- //标签体内容(4种)
- //tagdependent:标签体内容 直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释,
- // <test:myList>
- // select name,age from users
- // </test:myList>
- //JSP:接受所有JSP语法
- // <my:test>
- // <%=request.getProtocol()%>
- // </my:test>
- //empty:空标记,即起始标记和结束标记之间没有内容。
- // <test:mytag />
- // <test:mytag uname="Tom" />
- // <test:mytag></test:mytag>
- //scriptless:静态HTML或者表达式语言,但不能是JSP脚本
- <attribute>
- <name>userName</name>
- <required>true</required> //该属性是否必须
- <fragment>true</fragment> //该属性是否支持JSP脚本
- //rtexprvalue: 由请求时表达式来指定属性的值,默认为false,如下必须设置为true:
- <test:welcome uname="<%=request.getParameter("username") %>" />
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
3、使用标签库
在页面中引入<%@ taglib="myTag" uri="http://www.royzhou.com/myTagLib" %>
使用<mytag:HelloWorld userName="royzhou" />输出可以预览效:
4、带标签体的标签
迭代器标签开发处理类
- package com.royzhou.hello;
- import java.io.IOException;
- import java.util.Collection;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.PageContext;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- public class IteratorTag extends SimpleTagSupport {
- private String collection;
- private String item;
- public void doTag() throws JspException, IOException {
- //JspContext是pageContext的父类,可以通过向上转型获取pageContext对象
- PageContext pageContext = (PageContext) getJspContext();
- Collection itemList = (Collection)pageContext.getRequest().getAttribute(collection);
- for(Object s : itemList) {
- //设置属性到pageContext中
- getJspContext().setAttribute(item, s);
- //输出标签体
- getJspBody().invoke(null);
- }
- }
- 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;
- }
- }
tld文件配置
- <tag>
- <name>Iterator</name>
- <tag-class>com.royzhou.hello.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>
测试页面:
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@ taglib uri="http://www.royzhou.com/myTagLib" prefix="myTag" %>
- <%@ page import="java.util.*" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- List list = new ArrayList();
- list.add("1");
- list.add("2");
- list.add("3");
- list.add("4");
- list.add("5");
- request.setAttribute("list",list);
- %>
- <myTag:HelloWorld userName="royzhou" />
- <table>
- <myTag:Iterator item="item" collection="list">
- <tr>
- <td>${item }</td>
- </tr>
- </myTag:Iterator>
- </table>
- </body>
- </html>