`
summer_021
  • 浏览: 55036 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Struts1_学习笔记5_struts0400_jstl_格式化库_函数库

阅读更多
jstl标签库的配置
* 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)

注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
     是目前较为常用的环境

    
标签库的使用
* 采用taglib指令引入
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>


自定义函数库:
1、定义类和方法(方法必须是public static)
2、编写自定义tld文件,并且将此文件放到WEB-INF或WEB-INF任意子目录下
3、在jsp中采用taglib指令引入自定义函数库
4、采用 前缀+冒号+函数名 调用即可


1、格式化库:

页面代码:
<h1>测试jstl格式化库</h1>
	<hr>
	<li>测试日期的格式化</li><br>
	today(default):<fmt:formatDate value="${today}"/><br>
	today(type="date"):<fmt:formatDate value="${today}" type="date"/><br>
	today(type="time"):<fmt:formatDate value="${today}" type="time"/><br>
	today(type="both"):<fmt:formatDate value="${today}" type="both"/><br>
	today(dateStyle="short"):<fmt:formatDate value="${today}" dateStyle="short"/><br>
	today(dateStyle="medium"):<fmt:formatDate value="${today}" dateStyle="medium"/><br>
	today(dateStyle="long"):<fmt:formatDate value="${today}" dateStyle="long"/><br>
	today(dateStyle="full"):<fmt:formatDate value="${today}" dateStyle="full"/><br>
	today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss"/><br>
	today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="d"/>${d }<br>
	today(pattern="yyyy/MM/dd HH:mm"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm" /><br>
	<p>
	<li>测试数字的格式化</li><br>
	n(default):<fmt:formatNumber value="${n}"/><br>
	n(pattern="###,###.##"):<fmt:formatNumber value="${n}" pattern="###,###.##"/><br>
	n(pattern="###,###.0000"):<fmt:formatNumber value="${n}" pattern="###,###.0000"/><br>
	n(groupingUsed="false"):<fmt:formatNumber value="${n}" groupingUsed="false"/><br>
	n(minIntegerDigits="10"):<fmt:formatNumber value="${n}" minIntegerDigits="10"/><br>
	n(type="currency"):<fmt:formatNumber value="${n}" type="currency"/><br>
	n(type="currency"):<fmt:formatNumber value="${n}" type="currency" currencySymbol="$"/><br>
	n(type="percent"):<fmt:formatNumber value="${p}" type="percent" maxFractionDigits="2" minFractionDigits="2"/><br>



Action代码:
request.setAttribute("today", new Date());
		request.setAttribute("n", 123456.123);
		request.setAttribute("p", 0.12345);
		return mapping.findForward("success");


页面效果:
测试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%





2、函数库

tld文件:myfunctions.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/j2ee http://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.0</tlib-version>
  <short-name>my</short-name>
  <uri>http://www.aowin.com/functions</uri>
  
  <function>
    <name>sayHello</name>
    <function-class>com.aowin.struts.MyFunctions</function-class>
    <function-signature>java.lang.String sayHello(java.lang.String)</function-signature>
  </function>
</taglib>


页面代码:
头部:<%@ taglib prefix="my" uri="http://www.aowin.com/functions"%> 

	<h1>测试jstl函数库</h1>
	<hr>
	hello.length=(jsp脚本):<%=((String)request.getAttribute("hello")).length() %><br>
	hello.length(jstl函数库,函数调用必须在el表达式中 前缀+冒号+函数名):${fn:length(hello) }<br>
	list.length:${fn:length(list) }<br>
	<p>
	<li>测试自定义函数库</li><br>
	${my:sayHello(name) }<br>


自己的函数类:
public class MyFunctions {
	
	/**
	 * 方法必须是public static
	 * @param name
	 * @return
	 */
	public static String sayHello(String name) {
		return "Hello " + name;
	}
}




Action代码:
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");


使用自己的函数库标签:一份Action类、一份函数类、一份tld文件


页面效果:
测试jstl函数库

--------------------------------------------------------------------------------
hello.length=(jsp脚本):11
 hello.length(jstl函数库,函数调用必须在el表达式中 前缀+冒号+函数名):11
 list.length:2


•测试自定义函数库

Hello Tom





页面处理选择下拉框选中技巧:

<select name="categoryId"  id="categoryId">
        	<c:forEach items="${categoryList}" var="ic" >
        		<c:set var="select" value=""/>
        		<c:if test="${ ic.id eq category.id}">
        			<c:set var="select" value="selected"/>
        		</c:if>
         		<option value="${ic.id}" ${ select }>${ic.name}</option>
        	</c:forEach>
        </select>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics