EL表达式(三)自定义 EL 表达式

时间:2023-03-09 16:38:51
EL表达式(三)自定义 EL 表达式

自定义EL函数(静态方法):
编写步骤:
1.编写一个Java类,提供一个静态方法
import java.util.List;
public class GetLength {
public static Integer getLength(List list){

return list.size();
}
}

2.在WEB-INF目录下建立一个拓展名为tld(描述文件)的xml文件
<?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">

<description>this method can get a list length</description>
<display-name>getlength</display-name>
<tlib-version>1.1</tlib-version>
<short-name>myfn</short-name> <!-- 标准访问前缀 -->
<uri>http://www.sdxbxx.con/getLength</uri>
<function> <!-- 定义函数 -->
<description>this method can get a list length</description>
<name>getLength</name>
<function-class>cn.gs.wwg.el.GetLength</function-class>
<function-signature>java.lang.Integer getLength(java.util.List)</function-signature>
</function>
</taglib>
3.(可选)告知应用tld文件和tld中的uri对应
<jsp-config>
<taglib>
<taglib-uri>http://www.sdxbxx.con/getLength</taglib-uri>
<taglib-location>/WEB-INF/getLength.tld</taglib-location>
</taglib>
</jsp-config>

2.在WEB-INF目录下建立一个拓展名为tld(标签描述文件)的xml文件
3.(可选)告知应用tld文件和tld中的uri对应

1. Java

 package cn.gs.ly.mvc.domain;
import java.util.List;
public class GetLength {
public static Integer getLength(List list){ return list.size();
}
}

2. jsp

 <%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="cn.gs.ly.mvc.domain.Person"%>
<%@ taglib uri="/WEB-INF/getLength.tld" prefix="myfn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.liuyang.com/getLength" prefix="myfn1" %>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
pageContext.setAttribute("list", list); %>
${myfn:getLength(list)}<hr/>
${fn:contains("abcd","ab") }<hr/>
${fn:contains("abcd","abd") }<hr/> ${fn:split("2018-1-4--14:41:140","-:")[0] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[1] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[2] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[3] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[4] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[5] }<hr/>
${fn:split("2018-1-4--14:41:140","-:")[6] }<hr/> </body>
</html>

3. 在WEB-INF目录下建立一个拓展名为tld(描述文件)的xml文件

 <?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"> <description>this method can get a List length</description>
<display-name>getLength</display-name>
<tlib-version>1.1</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.liuyang.com/getLength</uri>
<function>
<description>this method can get a List length </description>
<name>getLength</name>
<function-class>cn.gs.ly.mvc.domain.GetLength</function-class>
<function-signature>java.long.Integer getLength(java.util.List)</function-signature>
</function>
</taglib>

4. 配置web.xml 文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WEB2018</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <context-param><!-- 初始化参数 -->
<param-name>info</param-name>
<param-value>value_liuyang</param-value>
</context-param> <jsp-config>
<taglib>
<taglib-uri>http://www.liuyang.com/getLength</taglib-uri>
<taglib-location>/WEB-INF/getLength.tld</taglib-location>
</taglib>
</jsp-config> </web-app>