自定义EL函数(以将字母转为大写为例)

Step1

定义一个类:StringFunction.java

  主要作用是来提供转大写的方法;

public class StringFunction {

    public static String toUpper(String str) {
        return str.toUpperCase();
    }
}

Step2

 

定义一个tld文件:dssfn.tdl

<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">
    
    <!-- 版本号 -->
    <tlib-version>1.0</tlib-version>
    <!-- 指定函数库的名称,一个函数库对应一个名称,通常与文件名形同,该名称会在jsp文件中使用的到 -->
    <short-name>dssfn</short-name>
    <!-- 指定该函数库所对应的url,一个库对应一个url,也是会在jsp中用到 -->
    <uri>http://www.monkey1024.com/jsp/monkeyTLD</uri>
    
    <function>
        <!-- 指定将来在jsp el中使用该函数的名称,一般与类中的静态方法一致 -->
        <name>toUpper</name>
        <!-- 指定该函数定义在哪个类中 -->
        <function-class>com.dss.function.StringFunction</function-class>
        <!-- 指定类中的方法 -->
        <function-signature>java.lang.String toUpper(java.lang.String)</function-signature>
    </function>
   
</taglib>

Step3

 

在jsp中调用其方法:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.monkey1024.com/jsp/monkeyTLD" prefix="dssfn"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  ${dssfn:toUpper("dss") } </body> </html>

 

在浏览器中的打印结果为:

 

posted on 2018-09-10 22:36  董大志  阅读(187)  评论(0编辑  收藏  举报

导航