函数库是可以扩展,可以自己定义,只能用于el
表达式,函数库中用以下函数
fn:contains, fn:containsIgnoreCase, fn:endsWith, fn:escapeXml, fn:indexOf, fn:join, fn:length, fn:replace, fn:split, fn:startsWith, fn:substring, fn:substringAfter, fn:substringBefore, fn:toLowerCase, fn:toUpperCase, fn:trim
1、 JstlFnAction.java文件

Code
package com.bjsxt.struts;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 测试jstl函数库
* @author Administrator
*
*/
public class JstlFnAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("hello", "hello world");
List list = new ArrayList();
list.add("t1");
list.add("t2");
request.setAttribute("list", list);
request.setAttribute("name", "Tom");
return mapping.findForward("success");
}
}
2、 Struts-config.xml文件中设置配置路径
<action path=”/jstlfn” type=”com.bjsxt.struts.JstlFnAction”>
<forward name=”success” path=”/jstl_fn.jsp”/>
</action>
3、 自定义函数库
· 定义类和方法(方法必须是public static类型的)

Code
package com.bjsxt.struts;
public class MyFunctions {
/**
* 方法必须是public static
* @param name
* @return
*/
public static String sayHello(String name) {
return "Hello " + name;
}
}
· 编写自定义的tld文件,并且将此文件放到WEB-INF或WEB-INF的任意子目录中,从一个已经写好的tld文件中拷贝如下信息

Code
<?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">
<description>my functions library</description>
<display-name>my functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my</short-name>
<!-- 自己定义uri,不能重复 -->
<uri>http://www.bjsxt.com/functions</uri>
<!-- -------------------自定的函数--------------------------- -->
<function>
<!-- 调用此自定义的函数,使用sayHello -->
<name>sayHello</name>
<!-- 对应函数所在类的路径 -->
<function-class>com.bjsxt.struts.MyFunctions</function-class>
<!-- 包装类使用java.lang.String,不是包装类的话可以使用int 如 sayHello ,sayHello是函数明-->
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
</function>
</talib>
·在jsp中采用taglib指令引入自定义函数库
<%@ taglib prefix="my" uri="http://www.bjsxt.com/functions"%>
·采用 前缀+冒号+函数名 调用即可
${my:sayHello(name) }
4、 jstl_fn.jsp
·引入标签库以及自定义的函数标签库,如:

Code
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="my" uri="http://www.bjsxt.com/functions"%>
•详细代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="my" uri="http://www.bjsxt.com/functions"%>
<!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=GB18030">
<title>测试jstl函数库</title>
</head>
<body>
<h1>测试jstl函数库</h1>
<hr>
<!-- 分别用jsp脚本,函数库,得到变量hello的长度 -->
hello.length=(jsp脚本):<%=((String)request.getAttribute("hello")).length() %><br>
hello.length(jstl函数库,函数调用必须在el表达式中 前缀+冒号+函数名):${fn:length(hello) }<br>
list.length:${fn:length(list) }<br>
<p>
<li>测试自定义函数库</li><br>
${my:sayHello(name) }<br>
</body>
</html>