1、首先编写EL处理类 预定义为表达式的方法必须为 公共静态
package com.hkrt.demo;
public class Util {
public static int length(){
return 100;
}
}
2、定义util.tld /WEB-INF/util.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3,org/2001/XMLSchema-instancce"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/cml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>自定义EL</short-name>
<uri>http://www.icardpay.com/util</uri>
<function>
<description>Length</description>
<name>length</name>
<function-class>
com.hkrt.demo.Util
</function-class>
<function-signature>
int length()
</function-signature>
</function>
</taglib>
3、页面调用自定义EL
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="util" uri="/WEB-INF/util.tld"%>
<%
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>
后面会输出自定义表达式的返回值 ${ util:length() }<br>
</body>
</html>