封装JSTL自定义标签

步骤:

1.编写一个实现Tag接口的java类(标签处理类),派生自SimpleTagSupport,重写doTag()方法。getJspBody(),getJspContext(),invoke(null)

2.编写标签库描述符(tld)文件,在tld文件中对标签处理类进行描述。

3.调用

实例一:重复显示标签收中的内容

1.建类

package maya;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;

public class TestTag extends SimpleTagSupport {
    private int count;
    public void setCount(int count){
        this.count = count;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment frag = this.getJspBody();
        for(int i=0;i<count;i++){
            frag.invoke(null);
        }
        
    }
}

2.配置.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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"
    version="2.1">
    
  <description>我自定义的标签</description>
  <display-name>XXX自定义标签</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>m</short-name>
  <uri>http://www.itnba.com/maya/myjstl</uri>
  
    <tag>
    <description>
        .....待写
    </description>
    <name>show</name>
    <tag-class>maya.TestTag</tag-class>
    <body-content>scriptless</body-content>
    
    <attribute>
        <description>
            ......
        </description>
        <name>count</name>
        <required>true</required><!--是否提交之前填写输入字段-->
        <rtexprvalue>false</rtexprvalue><!-- 是否可用jsp表达式 -->
    </attribute>
  </tag>
  
</taglib>    

3.在jsp中调用

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="m" uri="http://www.itnba.com/maya/myjstl" %>
<!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>

<m:show count="3">//count在本例中属于必填字段
hello<br>
</m:show>

</body>
</html>

运行结果:

hello
hello
hello

实例二:为标签中的内容加其它额外的属性或文字。

1.建类

package maya;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;

public class TestTag extends SimpleTagSupport {
    private String outerTagName="p";

    public void setOuterTagName(String outerTagName) {
        this.outerTagName = outerTagName;
    }
    
    private int count;
    public void setCount(int count){
        this.count = count;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment frag = this.getJspBody();
        this.getJspContext().getOut().write("<"+outerTagName+">");//括号里面可以改成任意文字,即为标签中的内容加其它额外的文字。例如:getJspContext().getOut().write("asdf")
        for(int i=0;i<count;i++){
            frag.invoke(null);
        }
        this.getJspContext().getOut().write("</"+outerTagName+">");
    }
}

2.配置.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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"
    version="2.1">
    
  <description>我自定义的标签</description>
  <display-name>XXX自定义标签</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>m</short-name>
  <uri>http://www.itnba.com/maya/myjstl</uri>
  
    <tag>
    <description>
        .....待写
    </description>
    <name>show</name>
    <tag-class>maya.TestTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
        <description>
            ......
        </description>
        <name>outerTagName</name>
        <required>false</required><!--是否提交之前填写输入字段-->
        <rtexprvalue>false</rtexprvalue><!-- 是否可用jsp表达式 -->
    </attribute>
    <attribute>
        <description>
            ......
        </description>
        <name>count</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>

3.在jsp中调用

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="m" uri="http://www.itnba.com/maya/myjstl" %>
<!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>

<m:show count="3" outerTagName="h1">
hello<br>
</m:show>
</body>
</html>

运行结果:

hello
hello
hello

原文地址:https://www.cnblogs.com/jonsnow/p/6393880.html