===============================================================================================================================
step1:新建一个java类
这个类有一点需要注意,方法必须是静态的. 这个也好理解,EL调用的函数,可以算是一个工具类,没有必要先实例化再调用. 最理想的方法就是整成静态方法,直接通过类来调用就可以了.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package net.thecatcher.ELdemo;
public class ELDemo1 {
public static String sayHello(String name){
return "hello "+ name;
}
}
===============================================================================================================================
step2: 需要编辑的tld文件
我用的eclipse,查到的方法是这样来建,但是,貌似不太好使. 我都是直接复制下面这段代码,然后再修改...
New ----> XML ----> XML File ----> 输入test.tld
----> Create XML
file from a DTD file ----> Select XML Catalog entry
----> -//Sun
Microsystems, Inc.//DTD JSP Tag Library 1.2//EN
----> finish
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.thecatcher/myfn</uri>
<function>
<name>sayHi</name>
<function-class>net.thecatcher.ELdemo.ELDemo1</function-class>
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
</function>
</taglib>
===============================================================================================================================
step3: jsp文件中通过taglib引用
-------------------------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.thecatcher/myfn" prefix="myfn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${ myfn:sayHi("meimei")}
</body>
</html>