Struts 1 学习笔记-3-3(JSTL格式化标签库测试)

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.上一张测试截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值