1.编写java类
package myEL;
public class ELFun {
public static String processStr(String s) {
s=s.toUpperCase();
return s;
}
}
EL函数对应的java类的方法必须是静态的
2.编写tld文件<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<uri>myelfun</uri>
<function>
<name>elfunction</name>
<function-class>myEL.ELFun</function-class>
<function-signature>
java.lang.String processStr(java.lang.String)
</function-signature>
</function>>
</taglib>
TLD文件的扩展名必须是.tld
3.web.xml配置
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/TLD/elfun.tld
</taglib-uri>
<taglib-location>
/WEB-INF/TLD/elfun.tld
</taglib-location>
</taglib>
</jsp-config>
4.jsp中调用
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/WEB-INF/TLD/elfun.tld" prefix="elfun" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'elfun.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>
<form method="POST">
请输入一个字符串:
<input type="text" name="text"/>
<p>
<input type="submit" value ="提交"/>
</form>
<p>
直接输出文本框中的内容:
<p>
${param.text }
<p>
${elfun:elfunction(param.text)}
<p>
<br>
</body>
</html>
如果用URI引用TLD文件,JSP引擎会先在WEB-INF目录及子目录中寻找所有的*.tld文件,如果发现某个.tld文件中的<uri>标签定义的URI和talib中的uri属性的值相等,就会记住这个.tld路径,在生成servlet的同时就会将这个TLD文件的路径也加进来。