zoukankan      html  css  js  c++  java
  • 自定义EL函数、自定义JSTL标签

    自定义EL函数

    1.做一个类(静态)

    package com.maya.el;
    
    public class ELBiaoDaoShi {
    
        public static String TiHuan(String s){
            
            String txt=s.replaceAll(""", "&quote;").replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">", "&gt;");
        
            return txt;
        }
        
        
    }

    2.配置:

    在WEB-INF文件夹下创建后缀名为  .tld   文件在jstl的jar文件fn中(复制粘贴)

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
      version="2.0">
      
        <description>自定义EL函数</description>        
      <display-name>自定义函数</display-name>
      <tlib-version>1.0</tlib-version>
      <short-name>fn</short-name>
      <uri>http://com.maya.el/myel</uri>
      
       <function>
        <description>
         对于特殊字符的转化
        </description>
        <name>zh</name>
        <function-class>com.maya.el.ELBiaoDaoShi</function-class>
        <function-signature>String ZhuanHuan(java.lang.String) </function-signature>
        <example>
        </example>
      </function>
    
      </taglib>

    3、导包、调用

    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        
    <%@ taglib prefix="my" uri="http://com.maya.el/myel" %>    
      
    <!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>
    ${ my:zh("<h1>hehe</h1>")}
    
    
    </body>
    </html>

    输出结果如下

    自定义JSTL标签

    自定义jstl标签与自定义EL函数相似

    做一个类,派生自SimpleTagSupport(继承)

    重写doTag()方法   (  a/t  + /  )

    package com.maya.jstl;
    
    import java.io.IOException;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.*;
    
    public class MyJSTL extends SimpleTagSupport {
    
        private int s;
    
        public void setS(int s) {
            this.s = s;
        }
        
        @Override
        public void doTag() throws JspException, IOException {
        
            JspFragment frag=this.getJspBody();      //获取标签中的值
            for(int i=0; i<s; i++){
                frag.invoke(null);              
                
            }
            
            
        }
    
    }

    2、同样是在WEB-INF下建一个   .tid   配置文件

    在WEB-INF文件夹下创建后缀名为  .tld   文件在jstl的jar文件c中(复制粘贴)

    <?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>自定义的一些jstl标签</description>
      <display-name>自定义jstl</display-name>
      <tlib-version>1.0</tlib-version>
      <short-name>myjstl</short-name>
      <uri>http://com.maya.jstl/myjstl</uri>
      
      
      <tag>
        <description>
                
        </description>
        <name>forxh</name>
        <tag-class>com.maya.jstl.MyJSTL</tag-class>
        <body-content>scriptless</body-content>
       
       <!--属性  -->
     <attribute>
            <description>
            </description>
            <name>s</name>     <!--属性名称  -->
            <required>true</required>        <!--是否为必须   true为必须  -->
            <rtexprvalue>false</rtexprvalue>   <!--是否可以用EL表达式 -->
        </attribute>
      </tag>
      
    </taglib>

    3、引用

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="my" uri="http://com.maya.jstl/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>
    <my:forxh s="5">a</my:forxh>
    </body>
    </html>

    显示结果如下

  • 相关阅读:
    2017.4.18下午
    2017.4.18上午
    2017.4.17上午
    2017.4.14下午
    2017.4.14上午
    4.17下午
    4.17上午
    4.13下午
    4.13上午
    4.10上午
  • 原文地址:https://www.cnblogs.com/zhaotiancheng/p/6393708.html
Copyright © 2011-2022 走看看