EL调用普通类的静态方法(EL自定义函数)

问题?EL调用普通类的静态方法(EL自定义函数),SUN提供的标准EL函数库

一、EL自定义函数编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤):
1、编写一个普通的java类,提供一个静态方法,也就是在EL表达式中要处理的方法。如:

[cpp] view plain copy
 print?
  1. package com.dp.javaWeb.function;  
  2.   
  3. public class toup {  
  4.     public static String toUpperCase(String str){  
  5.         return str.toUpperCase();//小写变大写  
  6.     }  
  7. }  

2、在JavaWeb应用的WEB-INF目录下建立一个扩展名是tld(taglib definition)的XML(Basic...)文件(参考Tomcat中的tlb文件示例,可以找到)。内容如下:Myfunction.tld

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  4. version="2.0">  
  5. <!--该文件主要是受这个的影响:http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd  -->  
  6. <!--  -->  
  7.     <tlib-version>1.0</tlib-version><!-- 定义的版本 -->  
  8.     <short-name>Myfunction</short-name><!--这个名字可以随便取,尽量与文件名相同,这样我们知道文件在哪儿  -->  
  9.     <uri>http://www.itcast.cn/Myfunction</uri><!-- 这个地址是随便取得。到时候jsp页面引入这个地址 -->  
  10.                   
  11.     <function><!-- 定义函数 -->  
  12.         <name>toCase</name><!-- 自定义函数名称(随意) -->  
  13.         <function-class>com.dp.javaWeb.function.toup</function-class><!-- 定义函数的类全名称 -->  
  14.         <function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature>  
  15.         <!--java.lang.String:返回值类型  toUpperCase:类定义的函数名(函数类型) -->  
  16.     </function>  
  17. </taglib>  

3、(可选步骤也是可忽略的步骤)前提是把tld文件放到了WEB-INF目录下。告知应用,tld文件和tld中的uri的对应。修改web.xml,增加以下内容:

[html] view plain copy
 print?
  1. <jsp-config><!-- 这个步骤可以省略,前提是这个tld文件在WEB-INF目录下-->  
  2. <taglib>  
  3.     <taglib-uri>http://www.itcast.cn/Myfunction</taglib-uri>  
  4.     <taglib-location>/WEB-INF/Myfunction.tld</taglib-location>  
  5. </taglib>  
  6. lt;/jsp-config>  
因为添加这个映射主要是为了在jsp页面总用taglib指令自动导入函数库时用的,但是实际汇总可以忽略这个。
4、在JSP中使用用taglib指令,引入自定义的EL函数库:

[html] view plain copy
 print?
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.   
  3. <%@ taglib uri="http://www.itcast.cn/Myfunction"  prefix="Myfunction"%>  
  4. <!-- 如果在xml映射文件中没有jsp-config,这个就不会自动导入,但是那个可以忽略,  
  5. 直接写这里 ,prefix文件名-->  
  6.   
  7. <%  
  8. String path = request.getContextPath();  
  9. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  10. %>  
  11.   
  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  13. <html>  
  14.   <head>  
  15.     <base href="<%=basePath%>">  
  16.       
  17.     <title>My JSP 'index.jsp' starting page</title>  
  18.     <meta http-equiv="pragma" content="no-cache">  
  19.     <meta http-equiv="cache-control" content="no-cache">  
  20.     <meta http-equiv="expires" content="0">      
  21.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  22.     <meta http-equiv="description" content="This is my page">  
  23.     <!-- 
  24.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  25.     -->  
  26.   </head>  
  27.     
  28.   <body>  
  29.       <%  
  30.       pageContext.setAttribute("p", "abcd23");  
  31.       %>  
  32.       ${Myfunction:toCase(p) }  
  33.       <!-- 转化大小写,用EL自定义函数. toCase是在tlb中的自定义的方法名-->  
  34.   </body>  
  35. </html>  

结果:

                     EL调用普通类的静态方法(EL自定义函数)

二、SUN提供的标准EL函数库
JSTL标准标签:(Jsp Standard Tag Libary)
***Core:核心
** Fmt:国际化
    SQL:数据库操作
     XML:xml操作
**fn:EL函数库

web应用下,在WEB-INF目录下lib中导入JSTL的两个jar包。standard.jar和 jstl.jar(这两个jar包在我的资源中已经上传(名叫:SUN提供的标准EL函数库及jar包))
jsp页面导:<%@ taglib uri="http://java.sun.com/jsp/jstl/functions"   prefix="fn" %>如:写两个常用的函数

[html] view plain copy
 print?
  1. <span style="font-family:Microsoft YaHei;font-size:18px;"><body>  
  2.    <h1>写两个常用的函数</h1>  
  3.    ${fn:contains("asdfg",df) }<!--母串是否包含字串 -->  
  4.    ${fn:substring("tomcates",2,4) }<!-- 截取字符串 -->  
  5.    ${fn:split("www.baidu.com",".")[0] }  
  6.   </body></span>  
EL调用普通类的静态方法(EL自定义函数)

EL调用普通类的静态方法(EL自定义函数)

EL调用普通类的静态方法(EL自定义函数)