---
title: "Struts1_学习笔记5_struts0400_jstl_格式化库_函数库"
author: "summer_021"
date: "2012-05-29T14:49:40Z"
tags: []
categories: ["编程语言"]
slug: "struts1-5-struts0400-jstl"
draft: false
lastmod: "2012-06-04T13:40:49Z"
id: 1543926
url: "https://mozzz.iteye.com/blog/1543926"
---

jstl标签库的配置 * 将jstl.jar和standard.jar拷贝到WEB-INF/lib下（如果使用el表达式，不用拷贝这两个jar） 注意：jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行，这个环境 是目前较为常用的环境 标签库的使用 * 采用taglib指令引入 &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%&gt; 自定义函数库： 1、定义类和方法（方法必须是public static） 2、编写自定义tld文件，并且将此文件放到WEB-INF或WEB-INF任意子目录下 3、在jsp中采用taglib指令引入自定义函数库 4、采用 前缀+冒号+函数名 调用即可 \[size=medium] \[b]\[color=blue]1、格式化库：\[/color]\[/b]\[/size] 页面代码： \[code="html"]

# 测试jstl格式化库

* * *

测试日期的格式化

today(default):  
today(type="date"):  
today(type="time"):  
today(type="both"):  
today(dateStyle="short"):  
today(dateStyle="medium"):  
today(dateStyle="long"):  
today(dateStyle="full"):  
today(pattern="yyyy/MM/dd HH:mm:ss"):  
today(pattern="yyyy/MM/dd HH:mm:ss"):${d }  
today(pattern="yyyy/MM/dd HH:mm"):

测试数字的格式化

n(default):  
n(pattern="###,###.##"):  
n(pattern="###,###.0000"):  
n(groupingUsed="false"):  
n(minIntegerDigits="10"):  
n(type="currency"):  
n(type="currency"):  
n(type="percent"):  
\[/code] \[color=blue] Action代码：\[/color]\[code="java"] request.setAttribute("today", new Date()); request.setAttribute("n", 123456.123); request.setAttribute("p", 0.12345); return mapping.findForward("success"); \[/code] 页面效果： \[code="html"] 测试jstl格式化库 -------------------------------------------------------------------------------- •测试日期的格式化 today(default):2012-5-29 today(type="date"):2012-5-29 today(type="time"):14:43:39 today(type="both"):2012-5-29 14:43:39 today(dateStyle="short"):12-5-29 today(dateStyle="medium"):2012-5-29 today(dateStyle="long"):2012年5月29日 today(dateStyle="full"):2012年5月29日 星期二 today(pattern="yyyy/MM/dd HH:mm:ss"):2012/05/29 14:43:39 today(pattern="yyyy/MM/dd HH:mm:ss"):2012/05/29 14:43:39 today(pattern="yyyy/MM/dd HH:mm"):2012/05/29 14:43 •测试数字的格式化 n(default):123,456.123 n(pattern="###,###.##"):123,456.12 n(pattern="###,###.0000"):123,456.1230 n(groupingUsed="false"):123456.123 n(minIntegerDigits="10"):0,000,123,456.123 n(type="currency"):￥123,456.12 n(type="currency"):$123,456.12 n(type="percent"):12.34% \[/code] \[size=medium]\[color=blue]\[b]2、函数库\[/b]\[/color]\[/size] \[color=blue]tld文件：myfunctions.tld. 放在web-inf下面\[/color] \[code="xml"] my functions library my functions 1.0 my http://www.aowin.com/functions sayHello com.aowin.struts.MyFunctions java.lang.String sayHello(java.lang.String) \[/code] \[color=blue]页面代码：\[/color] \[code="html"] 头部：&lt;%@ taglib prefix="my" uri="http://www.aowin.com/functions"%&gt;

# 测试jstl函数库

* * *

hello.length=(jsp脚本):&lt;%=((String)request.getAttribute("hello")).length() %&gt;  
hello.length(jstl函数库，函数调用必须在el表达式中 前缀+冒号+函数名):${fn:length(hello) }  
list.length:${fn:length(list) }

测试自定义函数库

${my:sayHello(name) }  
\[/code] \[color=blue]自己的函数类：\[/color] \[code="java"] public class MyFunctions { /\** * 方法必须是public static * @param name * @return \*/ public static String sayHello(String name) { return "Hello " + name; } } \[/code] \[color=blue] Action代码：\[/color]\[code="java"] request.setAttribute("hello", "hello world"); List list = new ArrayList(); list.add("t1"); list.add("t2"); request.setAttribute("list", list); request.setAttribute("name", "Tom"); return mapping.findForward("success");\[/code] \[color=red]使用自己的函数库标签：一份Action类、一份函数类、一份tld文件\[/color] \[color=blue]页面效果：\[/color] \[code="html"] 测试jstl函数库 -------------------------------------------------------------------------------- hello.length=(jsp脚本):11 hello.length(jstl函数库，函数调用必须在el表达式中 前缀+冒号+函数名):11 list.length:2 •测试自定义函数库 Hello Tom \[/code] \[size=medium]\[color=red] 页面处理选择下拉框选中技巧：\[/color]\[/size] \[code="html"] ${ic.name} \[/code]