1.当然是引入jar包,这个已在3-2中提到。
2.struts-config.xml:(其中还包含了以前El以及JSTLCore的action)
<?
xml version="1.0" encoding="ISO-8859-1"
?>

<!
DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

<
struts-config
>
<
action-mappings
>
<
action
path
="/jstlel"
type
="com.codedestiny.struts.JSTLELAction"
scope
="request"
>
<
forward
name
="success"
path
="/jstlel.jsp"
></
forward
>
</
action
>
<
action
path
="/jstlcore"
type
="com.codedestiny.struts.JSTLCoreAction"
scope
="request"
>
<
forward
name
="success"
path
="/jstlcore.jsp"
></
forward
>
</
action
>
<
action
path
="/jstlfmt"
type
="com.codedestiny.struts.JSTLFmtAction"
scope
="request"
>
<
forward
name
="success"
path
="/jstlfmt.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="MessageResources"
/>
</
struts-config
>

3.JSTLFmtAction.java:
package
com.codedestiny.struts;
import
java.util.Date;
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
JSTLFmtAction
extends
Action
...
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
request.setAttribute("today", new Date());
request.setAttribute("num", 12345.1234);
return mapping.findForward("success");
}
}
4.显示页面jstlfmt.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>

<%
...
@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
</
head
>
<
body
>
<
h1
>
测试JSTL中的fmt标签库
</
h1
>
<
hr
>
<
li
>
测试日期格式化
</
li
><
br
>
today(default) :
<
fmt:formatDate
value
="${today}"
/><
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(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" var="now") :
<
fmt:formatDate
value
="${today}"
pattern
="yyyy-MM-dd HH:mm:ss"
var
="now"
/><
br
>
now : ${now}
<
hr
>
<
li
>
测试数字格式化
</
li
><
br
>
num(default) :
<
fmt:formatNumber
value
="${num}"
></
fmt:formatNumber
><
br
>
num(type="currency") :
<
fmt:formatNumber
value
="${num}"
type
="currency"
></
fmt:formatNumber
><
br
>
num(pattern="###,###.000") :
<
fmt:formatNumber
value
="${num}"
pattern
="###,###.000"
></
fmt:formatNumber
><
br
>
num(minFractionDigits="2" - 最少保留小数点后几位) :
<
fmt:formatNumber
value
="${num}"
minFractionDigits
="2"
></
fmt:formatNumber
><
br
>
num(maxFractionDigits="2" - 最多保留小数点后几位) :
<
fmt:formatNumber
value
="${num}"
maxFractionDigits
="2"
></
fmt:formatNumber
><
br
>
0.5678(type="percent" maxFractionDigits="1") :
<
fmt:formatNumber
value
="${0.5678}"
type
="percent"
maxFractionDigits
="1"
></
fmt:formatNumber
><
br
>
</
body
>
</
html
>
5.上一张测试截图:

本文详细介绍了如何使用JSTL的fmt标签库进行日期和数字格式化,包括不同格式化选项的应用示例。
2652

被折叠的 条评论
为什么被折叠?



