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

最新文章

  1. 【转载】SweetAlert2 使用
  2. animation动画兼容所有手机
  3. awk脚本
  4. github文件上传及github pages博客搭建教程
  5. CSS3_边框属性之圆角的基本图形案例
  6. Linux进程实时IO监控iotop命令详解
  7. Oralce Plsql 中文显示乱码问题无需修改注册表完美解决
  8. Centos7多网卡绑定操作,通过nmcli命令操作。
  9. C++基础之引用与指针的区别与联系、常引用使用时应注意的问题
  10. WPF自定义产品框架
  11. LANMP系列教程之Apache编译安装CentOS7环境
  12. IDEA jsp模板
  13. flask+redis实现抢购(秒杀)功能
  14. HDU3915 Game 高斯消元
  15. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
  16. MYSQL 事务测试
  17. aarch64_l3
  18. 给iOS开发新手送点福利,简述UIAlertView的属性和用法
  19. async await的使用
  20. 在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据

热门文章

  1. Acwing143. 最大异或对
  2. 从ES6重新认识JavaScript设计模式: 装饰器模式
  3. vue+element Form键盘回车事件页面刷新解决
  4. 【推荐系统】知乎live入门
  5. centos7操作防火墙
  6. Git的基本操作命令
  7. 01. Linux-Kali系统网卡配置
  8. ffmpeg参数
  9. qt05 音乐播放器
  10. IDEA中项目引入独立包打包失败问题解决(找不到包)