struts2官方 中文教程 系列七:消息资源文件

时间:2023-03-08 21:48:57

介绍

在本教程中,我们将探索使用Struts 2消息资源功能(也称为 resource bundles 资源绑定)。消息资源提供了一种简单的方法,可以将文本放在一个视图页面中,通过应用程序,创建表单字段标签,并根据用户的语言环境将文本更改为特定的语言。

贴个本帖的地址,以免被爬:struts2官方 中文教程 系列七:消息资源文件  即 http://www.cnblogs.com/linghaoxinpian/p/6911223.html

下载本章节代码

信息资源属性文件

在Struts2 web应用程序中,我们可以通过创建具有与Action类相同名称的属性文件(.properties )来将消息资源属性文件与每个Struts 2 Action类关联起来。这个属性文件必须与Action类在相同的包下。对于我们的教程示例,假设我们希望将label标签显示的内容放置到一个单独的文件中,在这个文件中我们可以轻松地更改它们,并提供在其他语言中显示的功能。

新建属性properties文件

Register.properties

personBean.firstName=First name
personBean.lastName=Last name
personBean.age=Age
personBean.email=Email
thankyou=Thank you for registering %{personBean.firstName}.

以上只是一个标准的Java属性文件。key左value右。当Register action 被执行时,试图页面通过key引用该资源文件。

Key Attribute

Register.properties的key属性可以在textfield标签中使用,以告诉struts2框架 name和label属性使用什么值。避免了直接提供这些属性的值。打开register.jsp

      <s:textfield name="personBean.firstName" label="First name" />

修改为

<s:textfield key="personBean.firstName"  />

key属性指示Struts 2框架textfield的name属性(<s:textfield name="...."/>)的值使用与key的值(即name="personbean.firstName")。对于label属性的值,Struts 2在register.properties中找到键(key)personBean.firstName,然后将对应的值赋予label属性。

为了使能经过Action的处理后在跳转到register.jsp而不是直接打开register.jsp,我们需要打开index.jsp,进行修改

<p><a href="register.jsp">Please register</a> for our prize drawing.</p>

修改为

<s:url action="registerInput" var="registerInputLink" />
<p><a href="${registerInputLink}">Please register</a> for our prize drawing.</p>

我们使用Struts2的url标签来创建一个到registerInput的链接。然后我们使用该链接作为a标签的href属性值,并且在struts.xml中定义registerInput。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<!--hello-->
<action name="hello" class="action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action> <!-- register -->
<action name="register" class="action.RegisterAction" method="execute">
<result name="success">/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action> <!-- registerInput -->
<action name="registerInput" class="action.RegisterAction" method="input" >
<result name="input">/register.jsp</result>
</action>
</package> </struts>

运行

struts2官方 中文教程 系列七:消息资源文件

官网话太多,我直接画张图来说明执行流程。

图片太小就把图片拖到另一个浏览器窗口查看原图。

struts2官方 中文教程 系列七:消息资源文件

我们也可以使用 struts2的text标签来访问资源文件,因为thankyou.jsp也是通过RegisterAction来处理渲染给用户的,所以在thankyou中也可以访问RegisterAction.properties资源文件,修改thankyou.jsp中h3标签

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Registration Successful</title>
</head>
<body>
<h3><s:text name="thankyou" /></h3> <p>Your registration information: <s:property value="personBean" /> </p> <p><a href="<s:url action='index' />" >Return to home page</a>.</p>
</body>
</html>

运行有同样的效果

struts2官方 中文教程 系列七:消息资源文件

%{personBean.firstName} 告诉Struts 2调用getPersonBean,该方法返回一个Person对象。然后调用该对象的getFirstName方法,该方法返回一个字符串(用户输入到form表单的值),用最后的结果来替换这个部分

Package Level Properties(包级别的属性

上面的方法中,我们的属性文件(/struts_basic/src/action/RegisterAction.properties)必须要与对应的Action类(/struts_basic/src/action/RegisterAction.java)同名才可使用,那么如何才能是所有Action都能使用同一个属性文件(.properties)呢?

新建一个名为:package.properties的文件src/action/ 下,那么该包下所有Action均可使用该属性文件

struts2官方 中文教程 系列七:消息资源文件

package.properties内容如下:

greeting=Welcome to The Wonderful World of Struts 2

为了测试,在HelloWorld.jsp中添加一个h1标签

struts2官方 中文教程 系列七:消息资源文件

运行如下

struts2官方 中文教程 系列七:消息资源文件

全局属性文件

当然我们也可以在struts.xml文件中定义一个全局的属性文件(Global Properties),这个属性文件可以被所有视图页面引用。

添加以下内容到名为 global.properties 的文件中,当然,文件名称不必要是global.properties

/src/global.properties

contact=For assistance contact <a href='mailto:contact@email.com'>contact@email.com</a>

 struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" />
<!-- 全局属性文件start -->
<constant name="struts.custom.i18n.resources" value="global" />
<!-- 全局属性文件end -->
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<!--hello-->
<action name="hello" class="action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>

接着我们在index.jsp中使用这个global.properties文件

index.jsp

struts2官方 中文教程 系列七:消息资源文件

运行如下

struts2官方 中文教程 系列七:消息资源文件

国际化

使用消息资源文件(即属性文件.properties)还允许您提供不同语言的文本。默认情况下,Struts 2将使用用户的默认语言环境。如果该地区是英语的,那么使用的属性文件将是没有地区规范(例如RegisterAction.properties)的属性文件。如果地区不是英语,而是说西班牙语(es),那么Struts 2将寻找一个名为RegisterAction_es.properties的属性文件。创建一个名为RegisterAction_zh.properties的文件。该文件中添加了下面的中文的翻译。

src/action/RegisterAction_zh.properties

personBean.firstName=姓氏
personBean.lastName=名字
personBean.age=年龄
personBean.email=邮箱
thankyou=感谢您的注册, %{personBean.firstName}.

运行如下

struts2官方 中文教程 系列七:消息资源文件