/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lee; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; /** * * @author Administrator */ public class FragmentTag extends SimpleTagSupport { private JspFragment fragment; // fragment的setter和getter方法 public void setFragment(JspFragment fragment) { this.fragment = fragment; } public JspFragment getFragment() { return this.fragment; } @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("<div style='padding:10px;border:1px solid black;" + ";border-radius:20px'>"); out.println("<h3>下面是动态传入的JSP片段</h3>"); // 调用、输出“页面片段” fragment.invoke(null); out.println("</div"); } }
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>mytaglib</short-name> <uri>/WEB-INF/tlds/mytaglib</uri> <!-- A validator verifies that the tags are used correctly at JSP translation time. Validator entries look like this: <validator> <validator-class>com.mycompany.TagLibValidator</validator-class> <init-param> <param-name>parameter</param-name> <param-value>value</param-value> </init-param> </validator> --> <!-- A tag library can register Servlet Context event listeners in case it needs to react to such events. Listener entries look like this: <listener> <listener-class>com.mycompany.TagLibListener</listener-class> </listener> --> <!-- 定义第一个标签 --> <!-- 定义第一个标签 --> <tag> <!-- 定义标签名 --> <name>helloWorld</name> <!-- 定义标签处理类 --> <tag-class>lee.HelloWorldTag</tag-class> <!-- 定义标签体为空 --> <body-content>empty</body-content> </tag> <!-- 定义第二个标签 --> <tag> <!-- 定义标签名 --> <name>query</name> <!-- 定义标签处理类 --> <tag-class>lee.QueryTag</tag-class> <!-- 定义标签体为空 --> <body-content>empty</body-content> <!-- 配置标签属性:driver --> <attribute> <name>driver</name> <required>true</required> <fragment>true</fragment> </attribute> <!-- 配置标签属性:url --> <attribute> <name>url</name> <required>true</required> <fragment>true</fragment> </attribute> <!-- 配置标签属性:user --> <attribute> <name>user</name> <required>true</required> <fragment>true</fragment> </attribute> <!-- 配置标签属性:pass --> <attribute> <name>pass</name> <required>true</required> <fragment>true</fragment> </attribute> <!-- 配置标签属性:sql --> <attribute> <name>sql</name> <required>true</required> <fragment>true</fragment> </attribute> </tag> <!-- 定义第三个标签 --> <tag> <!-- 定义标签名 --> <name>iterator</name> <!-- 定义标签处理类 --> <tag-class>lee.IteratorTag</tag-class> <!-- 定义标签体不允许出现JSP脚本 --> <body-content>scriptless</body-content> <!-- 配置标签属性:collection --> <attribute> <name>collection</name> <required>true</required> <fragment>true</fragment> </attribute> <!-- 配置标签属性:item --> <attribute> <name>item</name> <required>true</required> <fragment>true</fragment> </attribute> </tag> <tag> <!-- 定义标签名 --> <name>fragment</name> <!-- 定义标签处理类 --> <tag-class>lee.FragmentTag</tag-class> <!-- 指定该标签不支持标签体 --> <body-content>empty</body-content> <!-- 定义标签属性:fragment --> <attribute> <name>fragment</name> <required>true</required> <fragment>true</fragment> </attribute> </tag> <!-- 定义接受动态属性的标签 --> <tag> <name>dynaAttr</name> <tag-class>lee.DynaAttributesTag</tag-class> <body-content>empty</body-content> <!-- 指定支持动态属性 --> <dynamic-attributes>true</dynamic-attributes> </tag> </taglib>
<%-- Document : fragmentTag Created on : 2020-5-4, 23:43:14 Author : Administrator --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@ taglib uri= "/WEB-INF/tlds/mytaglib" prefix="mytag"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>自定义标签示范</title> </head> <body bgcolor="#ffffc0"> <h2>下面显示的是自定义标签中的内容</h2> <mytag:fragment> <jsp:attribute name="fragment"> <%-- 下面是动态的JSP页面片段 --%> <mytag:helloWorld/> </jsp:attribute> </mytag:fragment> <br/> <mytag:fragment> <jsp:attribute name="fragment"> <%-- 下面是动态的JSP页面片段 --%> ${pageContext.request.remoteAddr} </jsp:attribute> </mytag:fragment> </body> </html>
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lee; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; /** * * @author Administrator */ public class HelloWorldTag extends SimpleTagSupport { // 重写doTag()方法,该方法为标签生成页面内容 public void doTag() throws JspException, IOException { // 获取页面输出流,并输出字符串 getJspContext().getOut().write("Hello World " + new java.util.Date()); } }