In actual page fragment as a label attribute

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/chengqiuming/article/details/100567401

Development of a custom label

package lee;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;



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");
    }
}

Two established TLD

<?xml version="1.0" encoding="GBK"?>

<taglib xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.crazyit.org/mytaglib</uri>

    <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>

</taglib>

Three using labels

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!-- 导入标签库,指定mytag前缀的标签,
    由http://www.crazyit.org/mytaglib的标签库处理 -->
<%@ taglib uri="http://www.crazyit.org/mytaglib" prefix="mytag"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>自定义标签示范</title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body bgcolor="#ffffc0">
<h2>下面显示的是自定义标签中的内容</h2>
<mytag:fragment>
    <jsp:attribute name="fragment">
    <%-- 使用jsp:attribute标签传入fragment参数(该注释不能放在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>

Four test

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/100567401