[转载]使用EL调用Java方法

本文介绍如何通过EL表达式语法开发自定义函数来扩展EL功能,包括编写静态方法、描述自定义函数的TLD文件及在JSP页面中的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

lEL表达式语法允许开发人员开发自定义函数,以调用Java类的方法。
•示例:${prefix:method(params)}
•在EL表达式中调用的只能是Java类的静态方法。
•这个Java类的静态方法需要在TLD文件中描述,才可以被EL表达式调用。(Taglibrary Definition)
•EL自定义函数用于扩展EL表达式的功能,可以让EL表达式完成普通Java程序代码所能完成的功能。


一般来说, , EL自定义函数开发与应用包括以下三个步骤:
1.• 编写一个 Java 类的静态方法
2.• 编写 标签库描述符( tld )文件 ,在 tld 文件中描述自定义函数。
3.• 在 JSP 页面中导入和使用自定义函数

示例:小写转大写
1.• 编写一个 Java 类的静态方法. 名称为FunctionDemo
  1. package com.itheima.base;  
  2. /* 
  3.  * 小写转换为大写 
  4.  * 需要静态方法 
  5.  */  
  6. public class FunctionDemo {  
  7.       public  static String toUpperCase(String value){  
  8.           return value.toUpperCase();  
  9.       }  
  10. }  
package com.itheima.base;
/*
 * 小写转换为大写
 * 需要静态方法
 */
public class FunctionDemo {
      public  static String toUpperCase(String value){
          return value.toUpperCase();
      }
}

2.• 编写 标签库描述符( tld )文件 ,在 tld 文件中描述自定义函数。  在WebRoot 目录下创建名称为myfun.tld的xml文件
  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <taglib xmlns=“http://java.sun.com/xml/ns/j2ee”  
  3.   xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.   xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd”  
  5.   version=“2.0”>  
  6.       
  7.   <tlib-version>1.0</tlib-version>  
  8.   <short-name>itheima</short-name>  
  9.   <uri>http://www.itheima.com/jsp/jstl/myfun</uri>  
  10.   
  11.   <function>  
  12.     <description>  
  13.       toUpperCase  
  14.     </description>  
  15.     <name>toUpperCase</name>  
  16.     <function-class>com.itheima.base.FunctionDemo</function-class>  
  17.     <function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>  
  18.     <example>  
  19.       <itheima:toUpperCase(‘aaaa’)  
  20.     </example>  
  21.   </function>  
  22. </taglib>  
<?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">

  <tlib-version>1.0</tlib-version>
  <short-name>itheima</short-name>
  <uri>http://www.itheima.com/jsp/jstl/myfun</uri>

  <function>
    <description>
      toUpperCase
    </description>
    <name>toUpperCase</name>
    <function-class>com.itheima.base.FunctionDemo</function-class>
    <function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature>
    <example>
      <itheima:toUpperCase('aaaa')
    </example>
  </function>
</taglib>
3.• 在 JSP 页面中导入和使用自定义函数  在WebRoot 目录下名称为6.jsp文件
  1. <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>  
  2. <%@ taglib uri=”http://www.itheima.com/jsp/jstl/myfun”   prefix=“itheima” %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>  
  5. <html>  
  6.   <head>  
  7.       
  8.     <title></title>  
  9.       
  10.     <meta http-equiv=”pragma” content=“no-cache”>  
  11.     <meta http-equiv=”cache-control” content=“no-cache”>  
  12.     <meta http-equiv=”expires” content=“0”>      
  13.       
  14.   
  15.   
  16.   </head>  
  17.     
  18.   <body>  
  19.     ${itheima:toUpperCase(’aaaa’) }  
  20.       
  21.   </body>  
  22. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.itheima.com/jsp/jstl/myfun"   prefix="itheima" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title></title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    



  </head>

  <body>
    ${itheima:toUpperCase('aaaa') }

  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值