• 自定义标签(JSTL)


    自定义标签的步骤:

      1、确定需求,如:用<my:date/>输出当前时间

      2、编写Java类:需要实现实现接口javax.servlet.jsp.tagext.JspTag

              具体的接口为:

                  javax.servlet.jsp.tagext.SimpleTag,简单标签,JSP2.0**
                          javax.servlet.jsp.tagext.Tag,经典标签

      3、编写tdl文件(xml)

    Demo:

     1 public class MyTag implements SimpleTag {
     2     
     3     private PageContext pageContext;
     4     public void setJspContext(JspContext pc) {
     5         
     6         this.pageContext=(PageContext) pc;//设置pageContext
     7     }
     8     
     9     public void doTag() throws JspException, IOException {
    10         
    11         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss:SSS");
    12         String date=format.format(new Date());
    13         pageContext.getOut().print(date);//输出到页面
    14     }
    15 
    16     public JspTag getParent() {
    17         // TODO Auto-generated method stub
    18         return null;
    19     }
    20 
    21     public void setJspBody(JspFragment arg0) {
    22         // TODO Auto-generated method stub
    23         
    24     }


     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 
     3 <taglib xmlns="http://java.sun.com/xml/ns/javaee"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
     6     version="2.1">

        <!-- 确定当前标签库的版本 --> 7 <tlib-version>1.0</tlib-version> 8 <short-name>my</short-name> <!-- 建议引入当前便签库是,使用的简写--> 9 <uri>http://www.cnblogs.com/liuwt365/mytag</uri> <!-- 为当前标签库配置文件,起一个名称        * 内容:自定义,全球唯一         --> 10 11 <tag> 12 <name>date</name> <!-- 确定标签名称 --> 13 <tag-class>cn.test.simpleTag.MyTag</tag-class> <!-- 确定实现类 --> 14 <body-content>empty</body-content> <!-- 格式化当前标签的body内容--> 15 </tag> 16 17 </taglib>
    1 <%@ taglib uri="http://www.cnblogs.com/liuwt365/mytag" prefix="my" %>   <!--  页面注册  -->
    2 
    3 <my:date/>

    PS:如果标签中含有属性,则tdl中需要设置:<my:maxValue num1="13" num2="15"></my:maxValue>

      

     1 <tag>
     2         <name>max</name>
     3         <tag-class>cn.test.simpleTag.MyMaxTag</tag-class>
     4         <body-content>empty</body-content>
     5         <attribute>
     6             <name>num1</name>
     7             <required>true</required>
     8             <rtexprvalue>true</rtexprvalue>
     9             <type>java.lang.Integer</type>
    10         </attribute>
    11         
    12         <attribute>//设置属性
    13             <name>num2</name>//属性名
    14             <required>true</required>//是否必须  true|false    (yes|no)(非必须)
    15             <rtexprvalue>true</rtexprvalue>  //runtime expression value  运行时表达式
    16             <type>java.lang.Integer</type>   //类型(非必须)
    17         </attribute>
    18     </tag>

      如果含有标签体,tdl中设置如下:<my:toUpper>JUHjdksds</my:toUpper>

    <tag>
            <name>toUpper</name>
            <tag-class>cn.test.simpleTag.MyToUpperTag</tag-class>
            <body-content>scriptless</body-content>

    <!-- 确定标签体的类型
                * empty:没有标签体
                * JSP:支持JSP所有的语法,在Tag中使用
                * scriptless:对于JSP页面的脚本不支持 <%%>
                * tagdependent:标签体不做处理,原样输出
    -->
    </tag>

    如果含有标签体 ,java类中的

    1 public void setJspBody(JspFragment jf) {
    2         this.jspFragment=jf;   //获取     
    3     }

    向jsp页面输出时

    1 StringWriter stringWriter=new StringWriter();//获得流
    2         
    3         this.jspFragment.invoke(stringWriter);//将数据写入到流中
    4         String data=stringWriter.toString().toUpperCase();
    5         
    6         this.pageContext.getOut().print(data);
    7         
    this.jspFragment.invoke(pageContext.getOut())=this.jspFragment.invoke();
  • 相关阅读:
    mysql服务设置远程连接 解决1251 client does not support ..问题
    Docker的简单使用
    Kick Start 2018
    Kick Start 2018
    Kick Start 2018
    LeetCode——三维形体的表面积
    面试金典——按摩师
    LeetCode——使数组唯一的最小增量
    LeetCode——单词接龙 II
    LeetCode——N皇后 II
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4088197.html
Copyright © 2020-2023  润新知