流年少年

勿忘初心,不管走多远都得竭尽全力去做...

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<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函数库与自定义函数库

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27550755/article/details/49928101
个人分类: java三大框架
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭