关闭

SSH:Struts1框架(自定义标签函数库)

标签: struts函数框架标签自定义
364人阅读 评论(0) 收藏 举报
分类:

JSTL函数库

1.JstlFnAction.java

 

package com;

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 {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  
  request.setAttribute("hello", "Hello World !");
  
  List<String> list = new ArrayList<String>();
  list.add("a");
  list.add("b");
  request.setAttribute("list", list);
  
  request.setAttribute("username", "KIMURA SAORI");
  return mapping.findForward("success");
 }

}

 

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"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://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.1</tlib-version>
  <short-name>mf</short-name>
  <uri>myURI</uri>
 
  <function>
    <name>sayHello</name>
    <function-class>com.MyFunction</function-class>
    <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
  </function>

</taglib>

 

4.MyFunction.java

 

package com;

public class MyFunction {
 
 public static String sayHello(String name) {
  return "Hello "+name+" !";
 }
}

 

 

运行结果:

Struts <wbr>1.2笔记:JSTL函数库与自定义函数库

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:39433次
    • 积分:1031
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:25篇
    • 译文:1篇
    • 评论:2条
    最新评论