一.JSTL函数库的使用:
1.先来struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<action-mappings>
<action path="/jstlfn" type="com.codedestiny.struts.JSTLFnAction" scope="request">
<forward name="success" path="/jstlfn.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>


2.再上JSTLFnAction.java:
package com.codedestiny.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;


public class JSTLFnAction extends Action ...{


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
request.setAttribute("hello", "Hello World");
List<String> list = new ArrayList<String>();

for(int i=0; i<10; i++) ...{
list.add("value" + i);
}
request.setAttribute("list", list);
return mapping.findForward("success");
}

}

3.显示界面jstlfn.jsp:

<%...@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<%...@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>测试JSTL函数标签库</h1>
<hr>
hello.length(jsp) : <%=((String)request.getAttribute("hello")).length()%><br>
hello.length(用JSTL函数库取得,函数库调用在EL表达式中采用前缀+冒号+函数名,例如:fn:length) : ${fn:length(hello)}<br>
list.size(用JSTL函数库取得) : ${fn:length(list)}<br>
</body>
</html>

4.测试截图:

二.自定义函数库:
1.定义类和方法(方法必须是public以及静态的)
本人定义了一个MyFunctions.java,其中包括一个静态方法sayHello( ):
package com.codedestiny.struts;


public class MyFunctions ...{
//注意:自定义函数,方法必须是静态以及public的

public static String sayHello(String name) ...{
return "Hello, " + name;
}
}

2.编写自定义的tld文件,并将该文件放到 web-inf 下或其子目录下:
本人编写的myFunctions.tld:
<?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</description>
<display-name>My functions </display-name>
<tlib-version>1.0</tlib-version>
<short-name>my</short-name>
<uri>http://blog.youkuaiyun.com/CodeDestiny/functions</uri>
<function>
<name>sayHello</name>
<function-class>com.codedestiny.struts.MyFunctions</function-class>
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
</function>
</taglib>
3.在jsp中通过taglib指令,引入自定义函数库,并调用:
我就在之前的jstlfn.jsp上继续编写:

<%...@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<%...@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<%...@ taglib prefix="my" uri="http://blog.youkuaiyun.com/CodeDestiny/functions" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>测试JSTL函数标签库</h1>
<hr>
hello.length(jsp) : <%=((String)request.getAttribute("hello")).length()%><br>
hello.length(用JSTL函数库取得,函数库调用在EL表达式中采用前缀+冒号+函数名,例如:fn:length) : ${fn:length(hello)}<br>
list.size(用JSTL函数库取得) : ${fn:length(list)}<br>
sayHello : ${my:sayHello("Jack")}<br>
</body>
</html>

4.测试截图:
