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

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 list = new ArrayList();
  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" %>




测试JSTL函数库


 

测试JSTL函数库



 
  • JSTL函数库


  •  hello.length(JSP脚本)=<%=request.getAttribute("hello").toString().length() %>

     hello.length(JSTL标签)=${fn:length(hello) }

     list.size=${fn:length(list) }

     


     

  • 测试自定义函数库


  •  sayHello:${mf:sayHello(username) }

     

     

    注:使用自定义函数库

    * 定义类和方法(方法必须是public static)

    * 编写自定义tld文件,并且将此文件放到WEB-INF或WEB-INF任意子目录下

     

    3.my.tld,放到WEB-INF下

     

     

      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">
       
      my functions library
      my functions
      1.1
      mf
      myURI
     
      
        sayHello
        com.MyFunction
        java.lang.String sayHello(java.lang.String)
      

     

    4.MyFunction.java

     

    package com;

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

     

     

    运行结果:

    SSH:Struts1框架(自定义标签函数库)_第1张图片

    你可能感兴趣的:(java三大框架)