zoukankan      html  css  js  c++  java
  • 使用EL调用Java方法

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

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

    示例:小写转大写
    1.•编写一个Java类的静态方法. 名称为FunctionDemo
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    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. }  

    2.•编写标签库描述符(tld)文件,在tld文件中描述自定义函数。  在WebRoot 目录下创建名称为myfun.tld的xml文件
    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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>  
    3.•JSP页面中导入和使用自定义函数  在WebRoot 目录下名称为6.jsp文件
    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    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>  

  • 相关阅读:
    【CF989E】A Trance of Nightfall
    [SHOI2012]信用卡凸包
    [HNOI2016]最小公倍数
    [HNOI2012]射箭
    [SCOI2015]小凸想跑步
    [CQOI2006]凸多边形
    ### Hadoop
    ### awk
    ### Theano
    ### Python Learning
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317783.html
Copyright © 2011-2022 走看看