jsp 學習 第3步 - el 自定義方法 tld 說明


   使用 el 的過程中,需要使用到后端代碼處理邏輯,這個時候我們就需要自定義 方法。

如我們后端代碼定義如下:

package com.rhythmk.common;

public class FncHelper {

    
    public static String getBlog()
    {
        return "Rhythmk.cnblogs.com";
    }
    
    public static  Integer getResult(Integer x,Integer y) {
         return x+y;
    }
}

 

在/WebContent/WEB-INF 目錄下 我們新建 tlds 用於存放自定義方法

  如新建 文件  fuc 於路徑 /WebContent/WEB-INF/fnc.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>JSTL 1.1 functions library</description>
  <display-name>JSTL functions sys</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>fnc</short-name>
<!--   <uri>http://java.sun.com/jsp/jstl/functionss</uri> -->
  <function>
    <description>獲取博客地址</description>
    <name>getBlog</name>
    <function-class>com.rhythmk.common.FncHelper</function-class>
    <function-signature>java.lang.String getBlog()</function-signature>
    <example>${fnc:getBlog()}</example>
  </function>
  
    <function>
    <description>求和</description>
    <name>getResult</name>
    <function-class>com.rhythmk.common.FncHelper</function-class>
    <function-signature>java.lang.Integer getResult(java.lang.Integer,java.lang.Integer)</function-signature>
    <example>${fnc:getResult(x,y)}</example>
  </function>
  
  </taglib>

 

在JSP 頁面中使用則先引入:

   <%@ taglib prefix="fnc" uri="/WEB-INF/tlds/fnc.tld" %>

調用如下:

無參數方法
${fnc:getBlog()}
<br/>
求和
${fnc:getResult(100,200)}

 

注意:

       后端JAVA代碼 一定為 靜態方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM