1、 定义函数类
- public class SecurityFunction {
- //方法需要是static类型的
- public static String testJstlFunction(String str) {
- System.out.println("执行一些操作"+str);
- return "hello jstl";
- }
- }
2、定义tld文件:myfunction.tld:(一般放到web-inf下)
- <?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">
- <tlib-version>1.0</tlib-version>
- <short-name>my</short-name>
- <uri>http://www.dcy.com/functions</uri>
- <function>
- <name>testJstlFunction</name>
- <function-class>com.dcy.test.SecurityFunction</function-class>
- <function-signature>java.lang.String testJstlFunction(java.lang.String )</function-signature>
- </function>
- </taglib>
3、修改web.xml文件:
- <jsp-config>
- <taglib>
- <taglib-uri>http://www.dcy.com/functions</taglib-uri>
- <taglib-location>/WEB-INF/myFunction.tld</taglib-location>
- </taglib>
- </jsp-config>
4、jsp页面引入(注意uri在web.xml和jsp页面引入需要一致,web.xml中的uri可以与location一致,也可以与tld文件中的uri一致,一般三者的uri一致)
- <%@ taglib prefix="my" uri="http://www.dcy.com/functions" %>
5、 jsp页面使用:
- ${my:testJstlFunction('teststring')}