jstl格式化库(fmt)
Action
package com.lanjh.struts.action;
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");
}
}
struts配置
<action path="/jstlfmt"
type="com.lanjh.struts.action.JstlFmtAction">
<forward name="success" path="/jstl_fmt.jsp"></forward>
</action>
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试jstl格式化库</title>
</head>
<body>
<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"/><br>
${d }<br>
today(pattern="yyyy-MM-dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy-MM-dd HH:mm:ss" /><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>
</body>
</html>