• 自己封装JSTL 自定义标签


    第一步,做一个类,派生自SimpleTagSupport,“alt+/”选重写doTag()方法。

    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+">");//添加的属性
            for(int i=0;i<count;i++){
            frag.invoke(null);//生成输出
            }
            this.getJspContext().getOut().write("</"+outerTagName+">");
        }
    }

    第二步,配置

    在WEB-INF文件夹中新建一个XML.File文件,扩展名更改为tld,选择XML template,完成创建。

    taglib为根元素

    <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>自定义jstl标签</description> 描述
    <display-name>JSTL自定义标签</display-name> 显示的名字
    <tlib-version>1.1</tlib-version> 版本
    <short-name>c</short-name> 前缀
    <uri>http://www.itnba.com/maya/myjstl</uri> 唯一标识

      <description>自定义jstl标签</description> 
      <display-name>JSTL自定义标签</display-name>
      <tlib-version>1.1</tlib-version>
      <short-name>c</short-name>
      <uri>http://www.itnba.com/maya/myjstl</uri>

    后面写<tag>标签

    <name>catch</name> 引用时:后面就是这个名字
    <tag-class>com.itnba.maya.myel.TestTag</tag-class> com.itnba.maya.myel是第一步做的包名 TestTag是类名
    <body-content>scriptless</body-content> 不能写脚本,即不能写<%%>
    <required>false</required>是否必填,true是自动生成必须填,false可写可不写
    <rtexprvalue>false</rtexprvalue>是否可以用el表达式<tag>

    <tag>
      <description>
      ...描述
      </description>
      <name>catch</name> 
      <tag-class>com.itnba.maya.myel.TestTag</tag-class>
      <body-content>scriptless</body-content>
        <attribute>
        <description>
        第一条属性
        </description>
        <name>outerTagName</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
        </attribute>
        
    
        <attribute>
        <description>
        第二条属性
        </description>
        <name>count</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
        </attribute>
      </tag>
    </taglib>

    第三步,现在就可以引用这个JSTL自定义的标签了

    先导一下写好的el函数,

    prefix是在${}中用的名,uri是第二步配置中的那个唯一标识

    <%@ taglib prefix="c" uri="http://www.itnba.com/maya/myjstl" %>

    用prefix名:<tag>中<name>,后面就可以把写好的属性加上

    <c:catch outerTagName="h3" count="2">
    Hello Lee<br>
    </c:catch>

    这段代码应该显示三号标题,count是循环次数,两次,显示如下图。

  • 相关阅读:
    uva489HangMan
    uva1339Ancient Cipher
    uva11292 Dragon of Loowater
    数字图像处理中所用数学工具2---线性操作与非线性操作
    数字图像处理中所用数学工具1---阵列与矩阵操作
    像素的邻接性、连通性与距离度量
    Matlab 数字图像处理1---图像的收缩和放大
    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(me
    MapRdeuce&Yarn的工作机制(YarnChild是什么)
    Hive介绍及安装
  • 原文地址:https://www.cnblogs.com/liyh123/p/6395889.html
Copyright © 2020-2023  润新知