`

JSTL中format标签的使用

    博客分类:
  • J2EE
阅读更多



 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <jsp-config>
  	<taglib>
  		<taglib-uri>jstl-c</taglib-uri>
  		<taglib-location>/WEB-INF/c.tld</taglib-location>
  	</taglib>
  	<taglib>
  		<taglib-uri>jstl-fmt</taglib-uri>
  		<taglib-location>/WEB-INF/fmt.tld</taglib-location>
  	</taglib>
  	<taglib>
  		<taglib-uri>jstl-fn</taglib-uri>
  		<taglib-location>/WEB-INF/fn.tld</taglib-location>
  	</taglib>
  </jsp-config>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 taglib.jsp
<%@taglib uri="jstl-c" prefix="c" %>
<%@taglib uri="jstl-fmt" prefix="fmt" %>
<%@taglib uri="jstl-fn" prefix="fn" %>
 设置语言环境的标签setLocale
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>ftm:setLoacle标记的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
	<h2 align="center">用于指定所使用的语言环境</h2><br>
	<h3>value: 语言环境的字符串,或java.util.Locale的对象</h3><br>
	<h3>scope: 指定作用的范围</h3><br>
	<h3>variant: 针对特定的平台或供应商定制语言环境</h3><br>
	<br><br>
	<h2>执行下面的代码,将忽略用户浏览器设置中所指定的语言</h2>
	<fmt:setLocale value="zh_CN" scope="session"/>
  </body>
</html>
 设置时区setTimeZone
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>fmt:setTimeZone标记的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">设置时区的标记</h2>
    <h3>value: 时区名或java.util.TimeZone对象</h3>
    <h3>scope: 作用的范围</h3>
    <h3>var: 保存TimeZone对象的变量的名称</h3>
    <fmt:setTimeZone value="GMT+8" scope="session" />
    <fmt:formatDate type="both" value="<%=new java.util.Date() %>"/><br>
  </body>
</html>
 另一个设置时区的timeZone
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>fmt:timeZone标记的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">设置时区的标记,只对所包含的内容有效</h2><br>
    <fmt:formatDate type="both" value="<%=new java.util.Date() %>"/><br>
    <fmt:timeZone value="GMT+8">
    	<fmt:formatDate type="both" value="<%=new java.util.Date() %>"/>
    </fmt:timeZone>
  </body>
</html>
 formateDate
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>fmt:formateDate标记的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">格式化和显示日期</h2><br>
    <h3>value: 指定日期和时间</h3>
    <h3>type: 设置显示的是时间还是日期,有效取值是time,date或both,默认的是date</h3>
    <h3>dateStyle: 设置如何格式化日期信息。有效值是:default,short,medium,long和full</h3>
    <h3>timeStyle: 设置如何格式化时间信息。有效值是:default,short,medium,long和full</h3>
    <h3>pattern: 日期和时间的样式yy-MM-dd HH:mm:ss</h3>
    <h3>timeZone: 设置显示那个时区的日期和时间,要是没有就使用fmt:timeZone设置的</h3>
    <h3>var: 保存格式化的时间或日期,要是没这属性就输出时间或日期的结果</h3>
    <h3>scope: 作用的范围</h3>
    
    <fmt:formatDate type="both" value="<%=new java.util.Date() %>" pattern="yy-MM-dd HH:mm:ss"/><br>
    
    <fmt:timeZone value="GMT+8">
    	<fmt:formatDate type="both" value="<%=new java.util.Date() %>" />
    </fmt:timeZone>
  </body>
</html>
 formateNumber
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>fmt:formateNumber标记的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h2 align="center">格式化和显示数字</h2><br>
    <h3>value: 指定格式化的数值</h3>
    <h3>type: 指明要对哪种类型的数值进行格式化,有效取值是number,currency和percent</h3>
    <h3>pattern: 数字格式化的样式</h3>
    <h3>groupingUsed: 设置是否要对小数点前面的数字进行分组。例如,将较大数的每三个数字分为一组</h3>
    <h3>var: 保存格式化的数字的名称,要是没这属性就直接在页面输出</h3>
    <h3>scope: 作用的范围</h3>
    
    <c:set value="1234567890.12345678" var="number" />
   	 默认方式的格式化数字<fmt:formatNumber type="number" value="${number}" /><br>
  	 数字不分组<fmt:formatNumber type="number" groupingUsed="false" value="${number}" /><br>
  	 整数部分最大三位<fmt:formatNumber type="number" maxIntegerDigits="3" value="${number}" /><br>
  	 整数部分最小取12位<fmt:formatNumber type="number" minIntegerDigits="12" value="${number}" /><br>
  	 小数部分最大取2位<fmt:formatNumber type="number" maxFractionDigits="2" value="${number}" /><br>
  	 小数部分最小取5位<fmt:formatNumber type="number" minFractionDigits="5" value="${number}" /><br>
  	 格式化货币数字<fmt:formatNumber type="currency" value="${number}" /><br>
  	 格式化百分比数字<fmt:formatNumber type="percent" value=".348" /><br>
  </body>
</html>
 

  • 大小: 11.2 KB
分享到:
评论

相关推荐

    JSTL标签JSTL标签

    JSTL Format标签的示例:** - ****:将日期或时间转换为特定的格式。 - ****:将字符串解析为数字,并进行格式化。 **5. JSTL与EL(Expression Language)的结合:** JSTL常与EL一起使用,EL负责获取和设置页面...

    JSTL标签库及使用方法

    ### 使用JSTL Format标签库 **日期和时间格式化**:`&lt;fmt:formatDate&gt;`标签用于格式化日期: ```jsp ${now}" pattern="yyyy-MM-dd HH:mm:ss" /&gt; ``` **国际化**:`&lt;fmt:message&gt;`和`&lt;fmt:setBundle&gt;`标签支持多...

    jstl标签库与使用教程

    在项目中使用JSTL,需要将`jstl.jar`和`standard.jar`两个库添加到类路径中。这两个文件通常位于`WEB-INF/lib`目录下。`standard.jar`包含了JSTL所需的EL表达式解析器和其他支持库。 **3. JSTL配置:** 在`web.xml`...

    JSTL标签库-tomcat10-简化JSP中java代码

    然后,就可以在JSP页面中直接使用JSTL标签了。 使用JSTL带来的好处包括: - **提高代码可读性**:将Java代码逻辑移出JSP页面,使得页面更专注于展现逻辑,而不是业务逻辑。 - **增强维护性**:JSTL标签具有统一的...

    jsp中标签JSTL中英文对照的中文帮助文档(chm格式)

    2. **I18N和Format标签库**:支持国际化和格式化日期、数字和货币等。 3. **XML标签库**:用于处理XML文档,包括解析、创建和转换XML。 4. **Function标签库**:提供了一系列有用的功能函数,可以在EL(Expression ...

    JSTL标签使用手册-中文版.zip

    **JSTL(JavaServer Pages Standard Tag Library)标签库**是Java Web开发中常用的一组标准标签,它为JSP页面提供了...通过阅读《JSTL标签使用手册-中文版》这份资料,你将能够深入理解JSTL并灵活运用到实际项目中。

    jsp中使用jstl标签的驱动

    在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的技术,而JSTL(JavaServer Pages Standard ...在实际开发中,应当尽可能地避免在JSP页面中直接写Java脚本,转而使用JSTL等标签库来实现业务逻辑。

    JSTL中文帮助文档

    2. **Format标签库**:用于日期、时间、数字格式化以及国际化。例如,`&lt;fmt:formatDate&gt;`可以将日期转换为指定格式。 3. **Function标签库**:提供了一些常用函数,如字符串操作、数组操作等。这些函数可以像EL...

    jstl标签详解.doc

    **JSTL(JSP Standard Tag Library)**是Java服务器端的一个强大工具,它提供了一系列标准的标签,用于简化JSP页面的...在实际开发中,JSTL通常与Spring MVC或其他MVC框架一起使用,进一步增强Web应用的功能和性能。

    jstl-1.1.2标签库

    JavaServer Pages Standard Tag Library (JSTL) 是一个用于增强JSP页面功能的开源标签库,它旨在提高JSP页面的可读性和可维护性,减少在页面中直接编写Java代码的情况。JSTL 1.1.2是该库的一个版本,它包含了多种...

    jstl标签库的配置

    3. **使用JSTL标签**:在JSP页面中,通过以下方式引入JSTL库: ```jsp &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt; ...

    JSTL标签库jar包

    - 考虑到安全性和性能,不要在生产环境中使用JSTL的SQL标签库进行数据库操作。 总的来说,JSTL是Java Web开发中一个非常实用的工具,它通过提供一系列标签和函数,使得开发者能以声明式的方式处理页面逻辑,从而...

    jstl标签jar包

    当你在项目中使用JSTL标签时,需要将此jar包添加到项目的类路径中,以便服务器能够在运行时正确解析和执行JSTL标签。 **JSTL的主要功能模块:** **1. Core标签库:** 这是JSTL的核心标签库,提供了一系列用于控制...

    关与jstl,jsp标签的综合教程

    相比传统的JSP自定义标签,JSTL的标签更加简洁,易于理解和使用。 EL是Java EE的一部分,它允许开发者在JSP页面中方便地访问和操作JavaBeans或其他作用域内的对象。EL表达式通常被用在JSP页面的属性和值的设置中,...

    jstl标签jstl--jstl.jar-standard.jar

    JSTL Format标签库** 这个库主要用于国际化(I18N)和格式化数据,如日期、数字和货币等。它提供了如下的标签: - `&lt;fmt:formatDate&gt;`:格式化日期。 - `&lt;fmt:formatNumber&gt;`:格式化数字。 - `&lt;fmt:message&gt;`:...

    JSTL标签课件PPT

    - 避免在JSP页面中混杂Java脚本,尽量使用JSTL标签。 - 使用EL表达式与JSTL标签配合,减少不必要的服务器端计算。 - 对于复杂的逻辑,考虑使用自定义标签库或转向服务器端组件(如Struts、Spring MVC)。 综上所...

    jstl 自定义标签

    在这个场景中,我们要讨论的是如何使用JSTL来自定义标签,并将一个`int`类型的值转换为时间格式。 首先,理解自定义标签的创建过程是必要的。JSTL自定义标签允许开发者扩展标准标签库的功能,满足特定需求。自定义...

    JSP中常用的JSTL fmt(format格式化)标签用法整理

    fmt标签库是JSTL中用于国际化的标签库,它支持i18n(国际化)格式标准,为Web应用提供了数字、日期和消息等的格式化和解析功能。 fmt标签库包含以下三类标签: 1. 数字日期格式化标签 - 标签:根据区域设置或者...

    jstl中文参考手册

    - **增强可维护性**:将逻辑封装在JSTL标签中,使得表示层更清晰,易于维护。 - **提高可读性**:JSTL标记库使用类似于HTML或XML的语法,这有助于提高代码的可读性。 #### 三、JSTL的组成部分 ##### 3.1 JSTL 1.0...

Global site tag (gtag.js) - Google Analytics