EL表达式自定义函数实战

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/chengqiuming/article/details/100589589

一 开发函数处理类

package lee;

public class Functions
{
    // 对字符串进行反转
    public static String reverse( String text )
    {
        return new StringBuffer( text ).reverse().toString();
    }
    // 统计字符串的个数
    public static int countChar( String text )
    {
        return text.length();
    }
}

二 使用标签库自定义函数

<?xml version="1.0" encoding="GBK"?>
<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
    web-jsptaglibrary_2_0.xsd" version="2.0">
    <tlib-version>1.0</tlib-version>
    <short-name>crazyit</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.crazyit.org/tags</uri>
    <!-- 定义第一个函数 -->
    <function>
        <!-- 定义函数名:reverse -->
        <name>reverse</name>
        <!-- 定义函数的处理类 -->
        <function-class>lee.Functions</function-class>
        <!-- 定义函数的实现方法-->
        <function-signature>
            java.lang.String reverse(java.lang.String)</function-signature>
    </function>
    <!-- 定义第二个函数: countChar -->
    <function>
        <!-- 定义函数名:countChar -->
        <name>countChar</name>
        <!-- 定义函数的处理类 -->
        <function-class>lee.Functions</function-class>
        <!-- 定义函数的实现方法-->
        <function-signature>int countChar(java.lang.String)
            </function-signature>
    </function>
</taglib>

三 在JSP页面的EL中使用函数

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="crazyit" uri="http://www.crazyit.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> new document </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
    <h2>表达式语言 - 自定义函数</h2><hr/>
    请输入一个字符串:
    <form action="useFunctions.jsp" method="post">
        字符串 = <input type="text" name="name" value="${param['name']}">
        <input type="submit"  value="提交">
    </form>
    <table border="1" bgcolor="aaaadd">
        <tr>
        <td><b>表达式语言</b></td>
        <td><b>计算结果</b></td>
        <tr>
        <tr>
            <td>\${param["name"]}</td>
            <td>${param["name"]}&nbsp;</td>
        </tr>
        <!--  使用reverse函数-->
        <tr>
            <td>\${crazyit:reverse(param["name"])}</td>
            <td>${crazyit:reverse(param["name"])}&nbsp;</td>
        </tr>
        <tr>
            <td>\${crazyit:reverse(crazyit:reverse(param["name"]))}</td>
            <td>${crazyit:reverse(crazyit:reverse(param["name"]))}&nbsp;</td>
        </tr>
        <!-- 使用countChar函数 -->
        <tr>
            <td>\${crazyit:countChar(param["name"])}</td>
            <td>${crazyit:countChar(param["name"])}&nbsp;</td>
        </tr>
    </table>
</body>
</html>

四 测试

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/100589589