記錄一種自定義標籤的用法

魔豆發表於2017-12-07

這種方式很少有人用,也不太友好,只是記錄一下

WEB-INF下新增myTemplate.tld

<?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>My Custom Template</description>    
    <tlib-version>1.0</tlib-version>    
    <short-name>my</short-name>    
    <uri>/myTemplate</uri>    
         
      <function>    
        <name>encoder</name>    
        <function-class>com.app.web.tag.jsp.MyTempalte</function-class>    
        <function-signature>java.lang.String encoder(java.lang.String,java.lang.String)</function-signature>  
        <example>${my.encoder('','')}</example>  
       </function>  
        
      <function>    
        <name>decoder</name>    
        <function-class>com.app.web.tag.jsp.MyTempalte</function-class>    
        <function-signature>java.lang.String decoder(java.lang.String,java.lang.String)</function-signature>  
        <example>${my.decoder('','')}</example>  
      </function>  
</taglib>  

 

MyTempalte.java
package com.app.web.tag.jsp;

public class MyTemplate {
    
    public static String encoder(String source,String charset){ 
        return "encoder:" + source + "," + charset;
    }  
      
    public static String decoder(String source,String charset){  
        return "encoder:" + source + "," + charset;
    }  

}

 

page1.jsp

<%@ taglib prefix="my" uri="/myTemplate"%>
${my:encoder("aa","bb") }

 

相關文章