<fmt:setBundle>标签

JSTL <fmt:setBundle> 标签与 <fmt:bundle> 标签都用于设置默认的数据源。

语法

JSP <fmt:setBundle> 标签的语法如下:

实例

<fmt:setBundle basename="resourceName" [var="绑定数据源别名"] [scope="page|session|request|application"]>
    代码块
</fmt:setBundle>

其中:

  • basename:表示绑定的数据源 .properties 文件的名称;
  • var:用于绑定数据源;
  • scope:设置 var 参数的有效范围,默认为 page。

示例

下面为 <fmt:setBundle> 标签的简单实例。

编写 myresource.properties 文件,内容如下:

实例

name=runoops自学
url=www.runoops.com

.properties 文件中的中文默认以 Unicode 编码显示。

index.jsp 代码如下:

实例

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<title>runoops自学(www.runoops.com)</title>
</head>
<body>
    <fmt:setBundle basename="myresource" var="lang" />
    网站名称:
    <fmt:message key="name" bundle="${lang}" />
    <br/>
    网址:
    <fmt:message key="url" bundle="${lang}" />
</body>
</html>

页面输出内容如下:

网站名称: runoops自学
网址: www.runoops.com