step 1:
写一个java类,定义一个或多个public static 方法,在方法中实现函数的逻辑
step 2:
在.tld文件中描述该函数可以参考fn.tld
step 3:
在jsp文件中,导入并使用函数
如,java类如下:
1 package myfunc; 2 3 public class MyElF { 4 public static String toUpperCase(String s){ 5 return s.toUpperCase(); 6 } 7 8 }
tld文件如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 8 <description>JSTL 1.1 functions library</description> 9 <display-name>JSTL functions1</display-name> 10 <tlib-version>1.1</tlib-version> 11 <short-name>fn1</short-name> 12 <uri>http://www.caxa.com/fn1</uri> 13 14 <function> 15 <description> 16 Tests if an input string contains the specified substring. 17 </description> 18 <name>toUpperCase</name> 19 <function-class>myfunc.MyElF</function-class> 20 <function-signature>String toUpperCase(java.lang.String)</function-signature> 21 </function> 22 23 24 </taglib>
jsp导入和使用函数如下:
1 <%@ page language="java" import="java.util.*,java.lang.*" pageEncoding="ISO-8859-1"%> 2 <%@ taglib prefix="fn1" 3 uri="http://www.caxa.com/fn1" %> 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 14 <title>My JSP ‘MyJsp.jsp‘ starting page</title> 15 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 25 </head> 26 27 <body> 28 <% 29 request.setAttribute("str1","xvgfdgsdfgfdgg"); 30 %> 31 s1:${fn1:toUpperCase(str1)} 32 33 This is my JSP page. <br> 34 </body> 35 </html>
时间: 2024-11-08 12:38:01