JSTL详解(一)
一般用途的标签
在JSTL中,一般用途的标签只要是指具有输出,设置变量,和错误处理等功能的标签,他们在jsp中使用很频繁,它们有:
- l<c:out>
- 2<c:set>
- 3<c:remove>
- 4<c:catch>
下面,我来讲述一下他们的作用:
<c:out>
它是一个输出的标签,负责把计算出的结果输出到jspwriter,就跟调用out.println()基本一样。没有BODY的时候,使用下面的语法:
- <c:outvalue=”value”[escapeXml]=”{true|false}”[default=”defaultValue”]/>
<c:out value=”value” [escapeXml]=”{true|false}” [default=”defaultValue”]/>
有BODY的时候,使用下面的语法:
- <c:outvalue=”value”[escapeXml]=”{true|false}”>
- 这里是BODY,可以是任何合法的jsp或者是html代码。
- </c:out>
<c:out value=”value” [escapeXml]=”{true|false}” >
这里是BODY,可以是任何合法的jsp或者是html代码。
</c:out>
注意,escapeXml的作用是是否将代码交给xml解析器解释,true为交给xml解析器解释,false为交给浏览器解释,见红色部分代码
- 举例:c_out.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:c:out的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <hr>
- <c:setvar="sessionAttr"value="sessionValue"scope="session"/>
- <c:setvar="pageAttr"value="pageValue"scope="page"/>
- <c:setvar="requestAttr"value="requestValue"scope="request"/>
- <c:outvalue="以下输出的是前面设置的属性<br>"[color=red]escapeXml="false"[/color]/>
- <c:outvalue="${sessionAttr}"/>
- <c:outvalue="${pageAttr}"/>
- <c:outvalue="${requestAttr}"/>
- <c:outvalue="${test_nodef}"default="没有test_nodef这个变量"/>
- </html>
举例:c_out.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL: c:out的使用</title>
</head>
<body bgcolor="#FFFFFF">
<hr>
<c:set var="sessionAttr" value="sessionValue" scope="session"/>
<c:set var="pageAttr" value="pageValue" scope="page"/>
<c:set var="requestAttr" value="requestValue" scope="request"/>
<c:out value="以下输出的是前面设置的属性<br>" [color=red]escapeXml="false"[/color]/>
<c:out value="${sessionAttr}"/>
<c:out value="${pageAttr}"/>
<c:out value="${requestAttr}"/>
<c:out value="${test_nodef}" default="没有test_nodef这个变量"/>
</html>
<c:set>
这个标签用于在某个范围(page,request,session,application)里面设置特定的值(默认为page),或者设置某个已经存在的javabean的属性。类似于<%request.setAttribute(“name”,value)%>
语法:
1、使用value属性设置一个特定范围里面的属性:
<c:set value=”value” var=”varName” [scope= “{page|request|session|application}”]/ >
2、使用value属性设置一个特定范围里面的属性,带有BODY:
<c:set value=”value” var=”varName” [scope= “{page|request|session|application}”] >
Body content
</c:set>
3、设置某个特定对象的一个属性:
<c:set value=”value” target=”target” property=”propertyName”/ >
4、设置某个特定对象的一个属性, 带有BODY:
<c:set value=”value” target=”target” property=”propertyName” >
Body content
</c:set>
- 举例:c_set.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <jsp:useBeanid="user"class="com.j2ee14.ch12.User"/>
- <html>
- <head>
- <title>JSTL:的使用c:set</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <hr>
- 设置一个属性,然后输出它<br>
- <c:setvar="maxCount"value="100"/>
- <c:outvalue="${maxCount}"/>
- <hr>设置属性时,把它的值放在标签的body中。
- <c:setvar="password">
- ksdjfxsdf234234
- </c:set>
- <c:outvalue="${password}"/>
- <hr>设置javaBean的属性,然后输出这些属性值:
- <c:setvalue="hellking"target="${user}"property="userName"/>
- userName=<c:outvalue="${user.userName}"/>
- <hr>设置属性,并且指定它们的范围,属性的默认范围是page。
- <c:setvalue="20"var="maxIdelTime"scope="session"/>
- <c:setvalue="next.jsp"var="nextPage"scope="page"/>
- </body>
- </html>
举例:c_set.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<jsp:useBean id="user" class="com.j2ee14.ch12.User"/>
<html>
<head>
<title>JSTL:的使用c:set</title>
</head>
<body bgcolor="#FFFFFF">
<hr>
设置一个属性,然后输出它<br>
<c:set var="maxCount" value="100"/>
<c:out value="${maxCount}"/>
<hr>设置属性时,把它的值放在标签的body中。
<c:set var="password">
ksdjfxsdf234234
</c:set>
<c:out value="${password}"/>
<hr>设置javaBean的属性,然后输出这些属性值:
<c:set value="hellking" target="${user}" property="userName"/>
userName=<c:out value="${user.userName}"/>
<hr>设置属性,并且指定它们的范围,属性的默认范围是page。
<c:set value="20" var="maxIdelTime" scope="session"/>
<c:set value="next.jsp" var="nextPage" scope="page"/>
</body>
</html>
<c:remove>
它的作用是删除某个变量或者属性。类似于<%session.removeAttribute(“name”)%>,它的语法是:
<c:remove var=”varName” [scope= “{page|request|session|application}”]/ >
- 举例:c_remove.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:c:remove的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:setvalue="10000"var="maxUser"scope="application"/>
- <c:setvalue="10"var="count"scope="session"/>
- maxUser=<c:outvalue="${maxUser}"/><br>
- count=<c:outvalue="${count}"/>
- <hr>调用c:remove...
- <c:removevar="maxUser"scope="application"/>
- <c:removevar="count"/>
- 调用了c:remove后,参数值为:
- maxUser=<c:outvalue="${maxUser}"/>,
- count=<c:outvalue="${count}"/>,
- </body>
- </html>
举例:c_remove.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL:c:remove的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:set value="10000" var="maxUser" scope="application"/>
<c:set value="10" var="count" scope="session"/>
maxUser=<c:out value="${maxUser}"/><br>
count=<c:out value="${count}"/>
<hr>调用c:remove...
<c:remove var="maxUser" scope="application"/>
<c:remove var="count"/>
调用了c:remove后,参数值为:
maxUser=<c:out value="${maxUser}"/>,
count=<c:out value="${count}"/>,
</body>
</html>
JSTL详解(二)
<c:catch>
它的作用是捕捉由嵌套在它里面的标签所抛出来的异常。类似于<%try{}catch{}%>,它的语法是:
<c:catch [var=”varName”]>nested actions</c:catch>
- 举例:c_catch.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:catch的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:catchvar="error">
- <%
- Integer.parseInt("sdkfj");
- %>
- </c:catch>
- <hr>异常:
- <c:outvalue="${error}"/>
- <hr>异常exception.getMessage=
- <c:outvalue="${error.message}"/>
- <hr>异常exception.getCause=
- <c:outvalue="${error.cause}"/>
- </body>
- </html>
举例:c_catch.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL:catch的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:catch var="error">
<%
Integer.parseInt("sdkfj");
%>
</c:catch>
<hr>异常:
<c:out value="${error}"/>
<hr>异常 exception.getMessage=
<c:out value="${error.message}"/>
<hr> 异常exception.getCause=
<c:out value="${error.cause}"/>
</body>
</html>
JSTL详解(三)
条件标签包括以下几种:
l <c:if>;
2 <c:choose>;
3 <c:when>;
4 <c:otherwise>;
下面,我来分别介绍:
l.<c:if>; 它用来做条件判断,功能类似jsp中的<%if(boolean){}%>
语法:
1、无body的情况
<c:if test=”testCondition” var=”varName” [scope=“{page|request|session|application}”]/>
2、有body的情况
<c:if test=”testCondition” var=”varName” [scope=“{page|request|session|application}”]>
Body内容
</c:if>
- 举例:c_if.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:c:if的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:setvar="count"value="100"/>
- <c:iftest="${count>78}">
- count>78
- </c:if>
- </body>
- </html>
举例:c_if.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL:c:if的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:set var="count" value="100"/>
<c:if test="${count>78}">
count>78
</c:if>
</body>
</html>
2.<c:choose>; 它是用于条件选择,和<c:when>,<c:otherwise>一起使用,它的条件选择是排斥性的,相当于jsp中的<%switch(i){case 0: … case 1:… default :}%>,也可以完成<%if{}…else if{}….else if{} else{} %>的功能
语法:<c:choose>
Body内容(<c:when>,<c:otherwise>)
</c:choose>
注意:它的body内容只能是由以下的元素组成:
1.空格;
2.0个或多个<c:when>子标签,<c:when>必须在<c:otherwise>子标签之前出现;
3.0个或多个<c: otherwise >子标签
<c:when> 它是<c:choose>的一个分支
语法:
<c:when test=”testCondition”>
Body内容
</c:when>
注意:它有两个限制:
1.必须以<c:choose>作为它的父标签
2.必须在<c:otherwise>标签之前出现
<c:otherwise>他代表了<c:choose>的最后选择相当于jsp的<%switch(i){case 0: … case 1:… default :}%> 中的default语句。
语法:
<c:otherwise>
Conditional block
</c:otherwise>
它的使用有两个限制:
1.必须以<c:choose>作为它的父标签;
2.必须以<c:choose>的最后分支方式出现;
<c:choose>,<c:otherwise>,<c:when>
- 举例:c_choose.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <c:setvar="count"value="100"/>
- <html>
- <head>
- <title>JSTL:c:choose的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:choose>
- <c:whentest="${count<=0}">
- <fontcolor="blue">
- </c:when>
- <c:whentest="${count<=60&&count>0}">
- <fontcolor="red">
- </c:when>
- <c:otherwise>
- <fontcolor="green">
- </c:otherwise>
- </c:choose>
- count的值是:<c:outvalue="${count}"/>
- </font>
- </body>
- </html>
举例:c_choose.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<c:set var="count" value="100"/>
<html>
<head>
<title>JSTL:c:choose的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:choose>
<c:when test="${count <=0}">
<font color="blue">
</c:when>
<c:when test="${count<=60&&count>0}">
<font color="red">
</c:when>
<c:otherwise>
<font color="green">
</c:otherwise>
</c:choose>
count的值是:<c:out value="${count}"/>
</font>
</body>
</html>
JSTL详解(四)
迭代标签的使用
如果使用scriptlets,那么,我们经常使用Iterator或者Enumeration来进行迭代,如:
Iterator it = collection.iterator();
while(it.hasNext()){
SomeBean someBean = (SomeBean)it.next();
out.println(someBean.getXXX());
…………..
}
在前面的一次中,我们开发过一个迭代标签,JSTL中也提供了对迭代进行支持的标签,并且,他的功能比上一章的强大的多。JSTL中的迭代标签有两个:
1.<c:forEach>;
2.<c:forTokens>;
1、<c:forTokens>专门处理TokenString的迭代,可以指定一个或者是多个分隔符。由于他们使用的很少,在这里就不介绍了。
2、<c:forEach>是最常用的,他几乎能够完成所有的迭代任务,就象jsp中的for(int i=j;i<k;i++)下面我来详细介绍。(注意,“[]”里面的是可选条件)
语法:a、在Collection中迭代:
<c:forEach [var=”varName”] items=”collection” [varStatus=”varStatusName”] [begin=”begin”] [end=”end”] [step=”step”]>
Body 内容
</c:forEach>
b、迭代固定的次数:
<c:forEach [var=”varName”] [varStatus=”varStatusName”] begin=”begin” end=”end” [step=”step”]>
Body 内容
</c:forEach>
- 举例:c_forEach1.jsp(在Collection中迭代)
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <%@pageimport="java.util.*,com.j2ee14.ch12.User"%>
- <%
- Collectionusers_c=newArrayList();
- for(inti=0;i<3;i++)
- {
- Useruser=newUser();
- user.setUserName("foo"+i);
- user.setPassword("foo"+i);
- users_c.add(user);
- }
- session.setAttribute("users",users_c);
- %>
- <html>
- <head>
- <title>JSTL:c:forEach的使用之一</title>
- </head>
- <bodybgcolor="#FFFFFF"><center>
- <h4>迭代某个collection中的元素。</h4>
- <tableborder=1>
- <tr><td>用户名</td><td>密码</td></tr>
- <c:forEachvar="users"items="${users}">
- <tr>
- <td><c:outvalue="${users.userName}"/></td>
- <td><c:outvalue="${users.password}"/></td>
- </tr>
- </c:forEach>
- </table>
- </center>
- </body>
- </html>
举例:c_forEach1.jsp(在Collection中迭代)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="java.util.*,com.j2ee14.ch12.User"%>
<%
Collection users_c=new ArrayList();
for(int i=0;i<3;i++)
{
User user=new User();
user.setUserName("foo"+i);
user.setPassword("foo"+i);
users_c.add(user);
}
session.setAttribute("users",users_c);
%>
<html>
<head>
<title>JSTL:c:forEach的使用之一</title>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>迭代某个collection中的元素。</h4>
<table border=1>
<tr><td>用户名</td><td>密码</td></tr>
<c:forEach var="users" items="${users}">
<tr>
<td><c:out value="${users.userName}"/></td>
<td><c:out value="${users.password}"/></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
- 举例:c_forEach2.jsp(迭代固定的次数)
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <tcounttle>JSTL:c:forEach的使用之二</tcounttle>
- </head>
- <bodybgcolor="#FFFFFF"><center>
- <h4>第二种迭代:50-60</h4>
- <c:forEachvar="count"begin="50"end="60">
- <c:outvalue="${count}"/>**
- </c:forEach>
- <h4>第二种迭代:10to100,step=10</h4>
- <c:forEachvar="count"begin="10"end="100"step="10">
- <c:outvalue="${count}"/>**
- </c:forEach>
- </center>
- </body>
- </html>
举例:c_forEach2.jsp(迭代固定的次数)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<tcounttle>JSTL:c:forEach的使用之二</tcounttle>
</head>
<body bgcolor="#FFFFFF"><center>
<h4>第二种迭代:50-60</h4>
<c:forEach var="count" begin="50" end="60">
<c:out value="${count}"/> **
</c:forEach>
<h4>第二种迭代:10 to 100,step=10</h4>
<c:forEach var="count" begin="10" end="100" step="10">
<c:out value="${count}"/>**
</c:forEach>
</center>
</body>
</html>
JSTL详解(五)
首先,感谢大家的关注,下面我来继续讲解:URL相关的标签,他们的作用主要是负责页面的导航、重定向、资源的获得以及参数的传递等等,他们有:
1.<c:import>
2.<c:redirect>
3.<c:url>
4.<c:param>
<c:import>
作用:导入一个url的资源,相当于jsp 中的<jsp:include page=”path”>标签,同样也可以把参数传递到被导入的页面。
语法:a、资源的内容使用string对象向外暴露
<c:import url=”url” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”] [charEncoding=”charEncoding”]>
Optional body content for <c:param> subtags
</c:import>
b、资源的内容使用redirect对象向外暴露
<c:import url=”url” [context=”context”] varReader=”varReaderName” [charEncoding=”charEncoding”]>
Body content where varReader is consumed by another action
</c:import>
- 举例:c_import.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:c:import的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <h3>绝对路径URL</h3>
- <blockquote>
- <ex:escapeHtml>
- <c:importurl="http://127.0.0.1:8080/ch12/footer.jsp"/>
- </ex:escapeHtml>
- </blockquote>
- <h3>相对路径并且传递参数到指定的URL</h3>
- <blockquote>
- <c:importurl="footer.jsp"charEncoding="gb2312">
- <c:paramname="userName"value="hellking"/>
- </c:import>
- </blockquote>
- </body>
- </html>
举例:c_import.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL:c:import的使用</title>
</head>
<body bgcolor="#FFFFFF">
<h3>绝对路径 URL</h3>
<blockquote>
<ex:escapeHtml>
<c:import url="http://127.0.0.1:8080/ch12/footer.jsp"/>
</ex:escapeHtml>
</blockquote>
<h3>相对路径并且传递参数到指定的URL</h3>
<blockquote>
<c:import url="footer.jsp" charEncoding="gb2312">
<c:param name="userName" value="hellking"/>
</c:import>
</blockquote>
</body>
</html>
<c:redirect>
作用:把客户的请求发送到另一个资源,相当于jsp中的<% request.sendRedirect(“other.jsp”)%>或者servlet中的RequestDispatch.forward(“other.jsp”)的功能。
语法:a、没有body的情况
<c:redirect url=”value” [context=”context”]/>
b、有body,在body 中查询指定的参数
<c:redirect url=”value” [context=”context”]>
<c:param> subtags
</c:redirect>
- 举例:c:redirect.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTL:c:redirect的使用</title>
- </head>
- <bodybgcolor="#cc99cc">
- <c:urlvalue="footer.jsp"var="nextpage">
- <c:paramname="userName"value="hellking"/>
- </c:url>
- <c:redirecturl="${nextpage}"/>
- </body>
- </html>
举例:c:redirect.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL:c:redirect的使用</title>
</head>
<body bgcolor="#cc99cc">
<c:url value="footer.jsp" var="nextpage">
<c:param name="userName" value="hellking"/>
</c:url>
<c:redirect url="${nextpage}"/>
</body>
</html>
<c:url>
作用:用于构造URL,主要的用途是URL的重写。
语法:a、没有body的情况
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”]/>
b、有body ,并在body 中有重写的参数
<c:url value=”value” [context=”context”] [var=”varName”] [scope=”{page|request|session|application}”]>
<c:param> subtags
</c:url>
- 举例:c_url.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTLc:url的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:urlvar="footer"value="footer.jsp"scope="page">
- <c:paramname="id"value="hellking"/>
- </c:url>
- <c:outvalue="${footer}"/>
- <br>另一种没有参数的URL<br>
- <c:urlvalue="footer.jsp"/>
- </body>
- </html>
举例:c_url.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL c:url 的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:url var="footer" value="footer.jsp" scope="page">
<c:param name="id" value="hellking"/>
</c:url>
<c:out value="${footer}"/>
<br>另一种没有参数的URL<br>
<c:url value="footer.jsp"/>
</body>
</html>
<c:param>
作用:它是在<c:import>,<c:redirectt>,<c:url>中添加请求的参数。和一般的参数没什么区别。
语法:a、参数的值使用value属性指定
<c:param name=”name” value=”value”/>
b、参数的值在body 中指定
<c:param name=”name”>
参数值
</c:param>
- 举例:c_param.jsp
- <%@taglibprefix="c"uri="http://java.sun.com/jstl/core"%>
- <%@pagecontentType="text/html;charset=gb2312"language="java"%>
- <html>
- <head>
- <title>JSTLc:param的使用</title>
- </head>
- <bodybgcolor="#FFFFFF">
- <c:redirecturl="footer.jsp">
- <c:paramname="userName">
- hellking
- </c:param>
- </c:redirect>
- </body>
- </html>
举例:c_param.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSTL c:param的使用</title>
</head>
<body bgcolor="#FFFFFF">
<c:redirect url="footer.jsp">
<c:param name="userName">
hellking
</c:param>
</c:redirect>
</body>
</html>
JSTL详解(六) 格式化标签
<fmt: formatNumber value =”12.3” pattern=”.000”/>
将输出 12.300. 应用样式 ”.000”, 将使格式化后的小数部分有 3 位。不足 3 位将以 0 补齐。
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”date” pattern="yyyy/M/d" />
格式化的结果是: 2007-5-27 .
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”time” pattern="hh:mm:ss"/>
格式化的结果是: 9:25:11
<fmt:formatDate value=”<%=new java.util.Date() %>” type=”both” />
格式化的结果是: 2007-5-27 9:25:11
- 举例:详细的使用如下例
- <c:setvar="now"value="<%=newjava.util.Date()%>"/>
- <tableborder="1"cellpadding="0"cellspacing="0"
- style="border-collapse:collapse"bordercolor="#111111"width="63%"id="AutoNumber2">
- <tr>
- <tdwidth="100%"colspan="2"bgcolor="#0000FF">
- <palign="center">
- <b><fontcolor="#FFFFFF"size="4">
- Formatting:<fmt:formatDatevalue="${now}"type="both"timeStyle="long"dateStyle="long"/>
- </font>
- </b>
- </p>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">formatDatetype="time"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="time"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">type="date"</td>
- <tdwidth="49%"><fmt:formatDatetype="date"value="${now}"/></td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"</td>
- <tdwidth="49%"><fmt:formatDatetype="both"value="${now}"/></td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"dateStyle="default"timeStyle="default"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="both"dateStyle="default"timeStyle="default"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"dateStyle="short"timeStyle="short"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="both"dateStyle="short"timeStyle="short"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"dateStyle="medium"timeStyle="medium"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="both"dateStyle="medium"timeStyle="medium"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"dateStyle="long"timeStyle="long"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="both"dateStyle="long"timeStyle="long"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">type="both"dateStyle="full"timeStyle="full"</td>
- <tdwidth="49%">
- <fmt:formatDatetype="both"dateStyle="full"timeStyle="full"value="${now}"/>
- </td>
- </tr>
- <tr>
- <tdwidth="51%">pattern="yyyy-MM-dd"</td>
- <tdwidth="49%"><fmt:formatDatepattern="yyyy-MM-dd"value="${now}"/></td>
- </tr>
- </table>
举例:详细的使用如下例
<c:set var="now" value="<%=new java.util.Date()%>" />
<table border="1" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#111111" width="63%" id="AutoNumber2">
<tr>
<td width="100%" colspan="2" bgcolor="#0000FF">
<p align="center">
<b><font color="#FFFFFF" size="4">
Formatting:<fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" />
</font>
</b>
</p>
</td>
</tr>
<tr>
<td width="51%">formatDate type="time"</td>
<td width="49%">
<fmt:formatDate type="time" value="${now}" />
</td>
</tr>
<tr>
<td width="51%">type="date"</td>
<td width="49%"><fmt:formatDate type="date" value="${now}" /></td>
</tr>
<tr>
<td width="51%">type="both"</td>
<td width="49%"><fmt:formatDate type="both" value="${now}" /></td>
</tr>
<tr>
<td width="51%">type="both" dateStyle="default" timeStyle="default"</td>
<td width="49%">
<fmt:formatDate type="both" dateStyle="default" timeStyle="default" value="${now}" />
</td>
</tr>
<tr>
<