1. 程式人生 > >EL表達式(三)自定義 EL 表達式

EL表達式(三)自定義 EL 表達式

abd sun title contain tld sta 參數 func lns

自定義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

1 package cn.gs.ly.mvc.domain;
2 import java.util.List;
3 public class GetLength {
4     public static Integer getLength(List list){
5         
6         return list.size();
7     }
8 }

2. jsp

 1 <%@page import="java.util.HashMap"%>
 2 <%@page import="java.util.Map"%>
 3 <%
@page import="java.util.ArrayList"%> 4 <%@page import="java.util.List"%> 5 <%@page import="cn.gs.ly.mvc.domain.Person"%> 6 <%@ taglib uri="/WEB-INF/getLength.tld" prefix="myfn" %> 7 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 8 <%@ taglib uri="http://www.liuyang.com/getLength
" prefix="myfn1" %> 9 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> 10 <!DOCTYPE html> 11 <html> 12 <head> 13 <title>Insert title here</title> 14 </head> 15 <body> 16 <% 17 List list = new ArrayList(); 18 list.add("a"); 19 list.add("b"); 20 list.add("c"); 21 pageContext.setAttribute("list", list); 22 23 %> 24 ${myfn:getLength(list)}<hr/> 25 ${fn:contains("abcd","ab") }<hr/> 26 ${fn:contains("abcd","abd") }<hr/> 27 28 ${fn:split("2018-1-4--14:41:140","-:")[0] }<hr/> 29 ${fn:split("2018-1-4--14:41:140","-:")[1] }<hr/> 30 ${fn:split("2018-1-4--14:41:140","-:")[2] }<hr/> 31 ${fn:split("2018-1-4--14:41:140","-:")[3] }<hr/> 32 ${fn:split("2018-1-4--14:41:140","-:")[4] }<hr/> 33 ${fn:split("2018-1-4--14:41:140","-:")[5] }<hr/> 34 ${fn:split("2018-1-4--14:41:140","-:")[6] }<hr/> 35 36 37 38 </body> 39 </html>

3. 在WEB-INF目錄下建立一個拓展名為tld(描述文件)的xml文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <taglib 
 3     xmlns="http://java.sun.com/xml/ns/j2ee"
 4       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 6       version="2.0">
 7     
 8       <description>this method can get a List length</description>
 9       <display-name>getLength</display-name>
10       <tlib-version>1.1</tlib-version>
11       <short-name>myfn</short-name>
12       <uri>http://www.liuyang.com/getLength</uri>
13       <function>
14         <description>this method can get a List length </description>
15         <name>getLength</name>
16         <function-class>cn.gs.ly.mvc.domain.GetLength</function-class>
17         <function-signature>java.long.Integer getLength(java.util.List)</function-signature>
18       </function> 
19 </taglib>

4. 配置web.xml 文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3   <display-name>WEB2018</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>    
12       
13       <context-param><!-- 初始化參數 -->
14           <param-name>info</param-name>
15           <param-value>value_liuyang</param-value>
16       </context-param>     
17       
18       <jsp-config>
19           <taglib>
20               <taglib-uri>http://www.liuyang.com/getLength</taglib-uri>
21               <taglib-location>/WEB-INF/getLength.tld</taglib-location>
22           </taglib>
23       </jsp-config>
24       
25   
26 </web-app>

EL表達式(三)自定義 EL 表達式