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