JSP开发之JSP 标准标签库(JSTL)之格式化标签setTimeZone标签&message标签&requestEncoding标签的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luyaran/article/details/82144075

<fmt:setTimeZone>标签用来复制一个时区对象至指定的作用域,语法如下:

<fmt:setTimeZone value="<string>" var="<string>" scope="<string>"/>

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

属性 描述 是否必要 默认值
value 时区
var 存储新时区的变量名 Replace default
scope 变量的作用域 Page

实例如下:

<%@ 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:setTimeZone 标签</title>
</head>
<body>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>当前时区时间: <fmt:formatDate value="${now}" 
             type="both" timeStyle="long" dateStyle="long" /></p>
<p>修改为 GMT-8 时区:</p>
<fmt:setTimeZone value="GMT-8" />
<p>Date in Changed Zone: <fmt:formatDate value="${now}" 
             type="both" timeStyle="long" dateStyle="long" /></p>
</body>
</html>

<fmt:message>标签映射一个关键字给局部消息,然后执行参数替换,语法如下:

<fmt:message
   key="<string>"
   bundle="<string>"
   var="<string>"
   scope="<string>"/>

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

属性 描述 是否必要 默认值
key 要检索的消息关键字 Body
bundle 要使用的资源束 默认资源束
var 存储局部消息的变量名 Print to page
scope var属性的作用域 Page

实例如下:

<%@ 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:message 标签</title>
</head>
<body>

<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.luyaran.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>

</body>
</html>

<fmt:requestEncoding>标签用来指定返回给Web应用程序的表单编码类型,它的语法如下:

<fmt:requestEncoding value="<string>"/>

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

属性 描述 是否必要 默认值
key 字符编码集的名称,用于解码request参数

我们使用<fmt:requestEncoding>标签来指定字符集,用于解码来自表单的数据。在字符集不是ISO-8859-1时必须使用这个标签。由于大多数浏览器在它们的请求中不包含Content-Type头,所以需要这个标签。

<fmt:requestEncoding>标签的目的就是用来指定请求的Content-Type。所以我们必须指定一个Content-Type,就算response是通过Page指令的contentType属性来编码。这是因为response的实际区域可能与Page指令所指定的不同。

如果页面包含 I18N-capable格式行为用于设置response的locale属性(通过调用ServletResponse.setLocale()方法),任何在页面中指定的编码集将会被覆盖。

来看下实例:

<%@ 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:message 标签</title>
</head>
<body>

<fmt:requestEncoding value="UTF-8" />
<fmt:setLocale value="es_ES"/>
<fmt:setBundle basename="com.luyaran.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>

</body>
</html>

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

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

猜你喜欢

转载自blog.csdn.net/luyaran/article/details/82144075
今日推荐