1、 JstlFmtAction.java文件

Code
package com.bjsxt.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;
/**
* 测试jstl格式化库
* @author Administrator
*
*/
public class JstlFmtAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//测试日期格式化
request.setAttribute("today", new Date());
//格式化数字
request.setAttribute("n", 123456.123);
//百分比
request.setAttribute("p", 0.12345);
return mapping.findForward("success");
}
}
2、 strut-config.xml文件中添加
在文件中首先引入标签,如:
Code
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>测试日期的格式化</li><br>
today(default--默认是日期:xxxx-x-x):<fmt:formatDate value="${today}"/><br>
today(type="date"--显示日期:xxxx-x-x):<fmt:formatDate value="${today}" type="date"/><br>
today(type="time"--显示时间:xx:xx:xx):<fmt:formatDate value="${today}" type="time"/><br>
today(type="both"--显示日期和时间):<fmt:formatDate value="${today}" type="both"/><br>
<!-- 设置日期的样式dateStyle--与控制面板/区域和语言选项 -->
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>
<!-- 放在变量var="d"中 -->
today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="d"/><br>
${d }<br>
<p>
<li>测试数字的格式化</li><br>
n(default):<fmt:formatNumber value="${n}"/><br>
<!-- pattern="###,###.##"保留两位,pattern="###,###.0000"小数位不够是补0 -->
n(pattern="###,###.##"):<fmt:formatNumber value="${n}" pattern="###,###.##"/><br>
n(pattern="###,###.0000"):<fmt:formatNumber value="${n}" pattern="###,###.0000"/><br>
<!-- 默认(groupingUsed=true)是分组了,即显示三个后,用一个逗号分割,为false时不分组 -->
n(groupingUsed="false"):<fmt:formatNumber value="${n}" groupingUsed="false"/><br>
<!-- minIntegerDigits="10"表示整数位最小保留10位 -->
n(minIntegerDigits="10"):<fmt:formatNumber value="${n}" minIntegerDigits="10"/><br>
<!-- type的默认值是number-数字,(currency-货币, currencySymbol表示使用的货币符号), percent表示百分比 -->
n(type="currency"):<fmt:formatNumber value="${n}" type="currency"/><br>
n(type="currency"):<fmt:formatNumber value="${n}" type="currency" currencySymbol="$"/><br>
<!-- maxFractionDigits="2"小数位最大为2位 minFractionDigits="2"小数位最小为2位 -->
n(type="percent"):<fmt:formatNumber value="${p}" type="percent" maxFractionDigits="2" minFractionDigits="2"/><br>
</body>
</html>
package com.bjsxt.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;
/**
* 测试jstl格式化库
* @author Administrator
*
*/
public class JstlFmtAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//测试日期格式化
request.setAttribute("today", new Date());
//格式化数字
request.setAttribute("n", 123456.123);
//百分比
request.setAttribute("p", 0.12345);
return mapping.findForward("success");
}
}
<action path=”/jstlfmt” type=”com.bjsxt.sturts.JstlFmtAction”>
<forward name=”success” path=”jstl_fmt.jsp”>
</action>
3、 jstl_fmt.jsp文件<forward name=”success” path=”jstl_fmt.jsp”>
</action>
在文件中首先引入标签,如:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
详细代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>测试日期的格式化</li><br>
today(default--默认是日期:xxxx-x-x):<fmt:formatDate value="${today}"/><br>
today(type="date"--显示日期:xxxx-x-x):<fmt:formatDate value="${today}" type="date"/><br>
today(type="time"--显示时间:xx:xx:xx):<fmt:formatDate value="${today}" type="time"/><br>
today(type="both"--显示日期和时间):<fmt:formatDate value="${today}" type="both"/><br>
<!-- 设置日期的样式dateStyle--与控制面板/区域和语言选项 -->
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>
<!-- 放在变量var="d"中 -->
today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="d"/><br>
${d }<br>
<p>
<li>测试数字的格式化</li><br>
n(default):<fmt:formatNumber value="${n}"/><br>
<!-- pattern="###,###.##"保留两位,pattern="###,###.0000"小数位不够是补0 -->
n(pattern="###,###.##"):<fmt:formatNumber value="${n}" pattern="###,###.##"/><br>
n(pattern="###,###.0000"):<fmt:formatNumber value="${n}" pattern="###,###.0000"/><br>
<!-- 默认(groupingUsed=true)是分组了,即显示三个后,用一个逗号分割,为false时不分组 -->
n(groupingUsed="false"):<fmt:formatNumber value="${n}" groupingUsed="false"/><br>
<!-- minIntegerDigits="10"表示整数位最小保留10位 -->
n(minIntegerDigits="10"):<fmt:formatNumber value="${n}" minIntegerDigits="10"/><br>
<!-- type的默认值是number-数字,(currency-货币, currencySymbol表示使用的货币符号), percent表示百分比 -->
n(type="currency"):<fmt:formatNumber value="${n}" type="currency"/><br>
n(type="currency"):<fmt:formatNumber value="${n}" type="currency" currencySymbol="$"/><br>
<!-- maxFractionDigits="2"小数位最大为2位 minFractionDigits="2"小数位最小为2位 -->
n(type="percent"):<fmt:formatNumber value="${p}" type="percent" maxFractionDigits="2" minFractionDigits="2"/><br>
</body>
</html>
浙公网安备 33010602011771号