JSP开发之JSP 标准标签库(JSTL)之格式化标签parseDate标签&bundle标签的使用

xiaoxiao2021-03-01  17

<fmt:parseDate> 标签用于解析日期,语法如下:

<fmt:parseDate value="<string>" type="<string>" dateStyle="<string>" timeStyle="<string>" pattern="<string>" timeZone="<string>" parseLocale="<string>" var="<string>" scope="<string>"/>

<fmt:parseDate>标签有如下属性:

属性描述是否必要默认值value要显示的日期是无typeDATE, TIME, 或 BOTH否datedateStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULT否defaulttimeStyleFULL, LONG, MEDIUM, SHORT, 或 DEFAULT否defaultpattern自定义格式模式否无timeZone显示日期的时区否默认时区var存储格式化日期的变量名否显示在页面scope存储格式化日志变量的范围否页面

我们来通过实例感受下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>JSTL fmt:parseDate 标签</title> </head> <body> <h3>日期解析:</h3> <c:set var="now" value="20-10-2015" /> <fmt:parseDate value="${now}" var="parsedEmpDate" pattern="dd-MM-yyyy" /> <p>解析后的日期为: <c:out value="${parsedEmpDate}" /></p> </body> </html>

<fmt:bundle>标签将指定的资源束对出现在<fmt:bundle>标签中的<fmt:message>标签可用。这可以使我们省去为每个<fmt:message>标签指定资源束的很多步骤。举例来说,下面的两个<fmt:bundle>块将产生同样的输出:

<fmt:bundle basename="com.tutorialspoint.Example"> <fmt:message key="count.one"/> </fmt:bundle> <fmt:bundle basename="com.tutorialspoint.Example" prefix="count."> <fmt:message key="title"/> </fmt:bundle>

看下它的语法格式:

<fmt:bundle baseName="<string>" prefix="<string>"/>

<fmt:bundle>标签有如下属性:

属性描述是否必要默认值basename指定被载入的资源束的基础名称是无prefix指定<fmt:message>标签key属性的前缀否无

资源束包含区域特定对象。资源束包含键值对。当我们的程序需要区域特定资源时,可以将所有的关键词对所有的locale共享,但是也可以为locale指定转换后的值。资源束可以帮助提供指定给locale的内容 。

一个Java资源束文件包含一系列的键值对。我们所关注的方法涉及到创建继承自java.util.ListResourceBundle 类的已编译Java类。我们必须编译这些类然后放在您的Web应用程序的CLASSPATH中。

完事让我们来定义一个默认的资源束:

package com.luyaran; import java.util.ListResourceBundle; public class Example_En extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { {"count.one", "One"}, {"count.two", "Two"}, {"count.three", "Three"}, }; }

编译以上文件为Examble.class,然后放在Web应用程序的CLASSPATH能找到的地方。现在可以使用JSTL来显示这三个数字了,就像这样:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <html> <head> <title>JSTL fmt:bundle 标签</title> </head> <body> <fmt:bundle basename="com.luyaran.Example" prefix="count."> <fmt:message key="one"/><br/> <fmt:message key="two"/><br/> <fmt:message key="three"/><br/> </fmt:bundle> </body> </html>

运行结果如下:

One Two Three

然后我们可以将其改为无prefix属性:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <html> <head> <title>JSTL fmt:bundle 标签</title> </head> <body> <fmt:bundle basename="com.luyaran.Example"> <fmt:message key="count.one"/><br/> <fmt:message key="count.two"/><br/> <fmt:message key="count.three"/><br/> </fmt:bundle> </body> </html>

运行结果同上。

好啦,本次记录就到这里了。

如果感觉不错的话,请多多点赞支持哦。。。

转载请注明原文地址: https://www.6miu.com/read-4150064.html

最新回复(0)