问题?EL调用普通类的静态方法(EL自定义函数),SUN提供的标准EL函数库
一、EL自定义函数编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤):
1、编写一个普通的java类,提供一个静态方法,也就是在EL表达式中要处理的方法。如:
-
package com.dp.javaWeb.function;
-
-
public class toup {
-
public static String toUpperCase(String str){
-
return str.toUpperCase();//小写变大写
-
}
-
}
2、在JavaWeb应用的WEB-INF目录下建立一个扩展名是tld(taglib definition)的XML(Basic...)文件(参考Tomcat中的tlb文件示例,可以找到)。内容如下:Myfunction.tld
-
<?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">
-
<!--该文件主要是受这个的影响:http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd -->
-
<!-- -->
-
<tlib-version>1.0</tlib-version><!-- 定义的版本 -->
-
<short-name>Myfunction</short-name><!--这个名字可以随便取,尽量与文件名相同,这样我们知道文件在哪儿 -->
-
<uri>http://www.itcast.cn/Myfunction</uri><!-- 这个地址是随便取得。到时候jsp页面引入这个地址 -->
-
-
<function><!-- 定义函数 -->
-
<name>toCase</name><!-- 自定义函数名称(随意) -->
-
<function-class>com.dp.javaWeb.function.toup</function-class><!-- 定义函数的类全名称 -->
-
<function-signature>java.lang.String toUpperCase( java.lang.String )</function-signature>
-
<!--java.lang.String:返回值类型 toUpperCase:类定义的函数名(函数类型) -->
-
</function>
-
</taglib>
3、(可选步骤也是可忽略的步骤)前提是把tld文件放到了WEB-INF目录下。告知应用,tld文件和tld中的uri的对应。修改web.xml,增加以下内容:
-
<jsp-config><!-- 这个步骤可以省略,前提是这个tld文件在WEB-INF目录下-->
-
<taglib>
-
<taglib-uri>http://www.itcast.cn/Myfunction</taglib-uri>
-
<taglib-location>/WEB-INF/Myfunction.tld</taglib-location>
-
</taglib>
-
lt;/jsp-config>
因为添加这个映射主要是为了在jsp页面总用taglib指令自动导入函数库时用的,但是实际汇总可以忽略这个。4、在JSP中使用用taglib指令,引入自定义的EL函数库:
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
-
<%@ taglib uri="http://www.itcast.cn/Myfunction" prefix="Myfunction"%>
-
<!-- 如果在xml映射文件中没有jsp-config,这个就不会自动导入,但是那个可以忽略,
-
直接写这里 ,prefix文件名-->
-
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'index.jsp' starting page</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
-
<body>
-
<%
-
pageContext.setAttribute("p", "abcd23");
-
%>
-
${Myfunction:toCase(p) }
-
<!-- 转化大小写,用EL自定义函数. toCase是在tlb中的自定义的方法名-->
-
</body>
-
</html>
结果:

二、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" %>如:写两个常用的函数
-
<span style="font-family:Microsoft YaHei;font-size:18px;"><body>
-
<h1>写两个常用的函数</h1>
-
${fn:contains("asdfg",df) }<!--母串是否包含字串 -->
-
${fn:substring("tomcates",2,4) }<!-- 截取字符串 -->
-
${fn:split("www.baidu.com",".")[0] }
-
</body></span>


