SSH:Struts1框架(自定義標籤函式庫)
JSTL函式庫
1.JstlFnAction.java
package com;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action; public class JstlFnAction extends Action {
@Override } |
2.jstl_fn.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="mf" uri="myURI" %> <!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> <li>JSTL函式庫</li><br> hello.length(JSP指令碼)=<%=request.getAttribute("hello").toString().length() %><br> hello.length(JSTL標籤)=${fn:length(hello) }<br> list.size=${fn:length(list) }<br> <p> <li>測試自定義函式庫</li><br> sayHello:${mf:sayHello(username) } </body> </html> |
注:使用自定義函式庫
* 定義類和方法(方法必須是public static)
* 編寫自定義tld檔案,並且將此檔案放到WEB-INF或WEB-INF任意子目錄下
3.my.tld,放到WEB-INF下
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" </taglib> |
4.MyFunction.java
package com;
public class MyFunction { |
執行結果: