有格式化:日期,数字的标签:
看例子:
JstlFormatAction.java:
package com.keith.formatJSTL;
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 JstlFormatAction 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.784);
return mapping.findForward("JstlFormatAction");
}
}
struts-config.xml:
<?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="/JstlFormatAction"
type="com.keith.formatJSTL.JstlFormatAction">
<forward name="JstlFormatAction" path="/FormatJsp.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
用到格式化标签,首先要引入标签库:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
jsp:
<h2>"l测试format</h2>
<hr color="orange">
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>
<p></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>