Struts2 消息传递Message Resource Files



Struts2教程七——Message Resource Files

本教程假设你已完成Form Validation教程,可以在Google Code -http://code.google.com/p/struts2-examples/downloads/list下载 Message_Resource_Struts2_Ant或者Message_Resource_Struts2_Mvn。

引言

本课学习使用Struts 2消息资源功能(也叫资源bundles)。消息资源提供了一个简单的在view页面中放置文本的方式,文本可以用来完成用户本地化。

消息资源属性文件

在Struts 2 web application中可以创建跟struts2 action相应的消息资源属性文件,这个文件和action同名,以.properties为后缀,本文件要与action类在同一个包目录下,本课用properties文件放置表单标签的label值,以实现不同语言之间的转换。

如果你完成了Form Validation ,可在原项目中改写。

在org.apache.struts.register.action包下建立Register.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 properties文件。等号左边是key,右边是value,当Register action执行时,这些属性对view页面来说就是可用的。(通过对key的引用)

Struts 2 Key属性

Struts 2 的key属性可以用在textfield标签,value作为textfield的label,key作为name。

register.jsp页面有如下Struts 2 textfield标签:

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

用key属性可以取代name和label

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

key使得Struts 2 framework使用相同的值作为name属性,对于label的值,key的值可以使Struts 2 framework在properties文件中找到与其同名的key,将其对应的值作为label的值。

为了使key属性能找到properties文件,view页面必须是action类的result页面。我们看index.jsp中register的链接,只是一个标准的url:

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

我们要改写一下上面的链接使得他可以通过Struts 2 的Action类Register.java,换成下面的内容:

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

我们用struts2 url标签创建一个到registerInput action的链接。然后在struts.xml中配置该action。加入下面内容:

registerInput action node for struts.xml
<action name="registerInput" class="org.apache.struts.register.action.Register" method="input" >
	<result name="input">/register.jsp</result>
</action>

以上配置告诉Struts 2 framework执行Register类的input方法,该方法继承自ActionSupport,默认返回input。

通过以上步骤,view页面可以使用Register.properties的内容。Struts 2 framework会使Register.properties中定义的properties可用,因为view页面是在Register.java执行后返回的。

发布应用,转到home页面,会看到一个到registerInput.action的超链接。

点击register,出现register.jsp页面,代表域的label将会是Register.properties文件相应key的value值。

Struts 2 Text标签

Struts 2 text标签也可以使用properties文件,在thankyou.jsp加入以下内容取代h3标签。

text tag
<h3><s:text name="thankyou" /></h3>

因为thankyou.jsp也是Register.java Action类执行后返回,所以key和value是可用的。

那么在thankyou.jsp中怎样使用用户的输入呢?像personBean.firstName。回头看看Register.properties文件的thankyou key。

Register.properties
thankyou=Thank you for registering %{personBean.firstName}.

%{personBean.firstName}告诉Struts 2通过调用getPersonBean取得返回值,然后调用getFirstName方法返回firstName,替换它。

包级别的Properties


如果想让properties文件中的内容被不同action类返回的view页面使用怎么办呢?Struts 2提供了包级别的properties文件来解决这个问题。

将下面内容写入package.properties文件并放在org.apache.struts包下。

package.properties
greeting=Welcome to The Wonderful World of Struts 2

现在org.apache.struts…包下的action返回的view页面都可以使用了。例如在helloworld.jsp的h2标签前加入如下内容:

<h1><s:text name="greeting" /></h1>

重新部署,转到index.jsp,点击Hello World链接,会看到:

全局的Properties

你也可以在struts.xml中配置一个全局的properties文件。这个property文件中定义的key和value值对所有action执行后返回的view页面都是可用的。

加入如下global.properties文件(文件名不强制要求为global)。

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

在src目录下保存global.properties文件。

为了让Struts 2 framework了解global.properties文件,在struts.xml的name=”struts.devmode”的constant节点后加入下面节点。

Specify Global Property File In struts.xml
<constant name="struts.custom.i18n.resources" value="global" />

为了在view页面中使用contact键,在index.jsp中加入以下标签。

Using contact property
<hr />
<s:text name="contact" />

重新部署,转到index.action,会看到:

Struts 2会先从匹配action类的properties文件中查找,如找不到,则在包级别的properties文件中查找,最后在struts.xml文件中配置的文件中查找。这里会在global文件中找到。

也可以在所有jsp页面中加入上述text标签。

国际化

使用属性资源文件可以提供不同语言的文本,默认情况下,struts 2 会使用用户的本地语言。如果本地语言为英语,则struts 2会找到没有地区标记的文件,例如Register.properties。如果本地语言非英语,例如西班牙语,则struts 2会查找Register_es.properties。

为了说明,我们创建一个Register_es.properties文件并加入以下内容(value为西班牙语)。

Register_es.properties
personBean.firstName=Nombre
personBean.lastName=Apellidos
personBean.age=Edad
personBean.email=Correo
thankyou=Gracias por registrarse, %{personBean.firstName}.

将Register_es.properties文件保存在与Register.properties相同的目录下。

例子中,我们要告诉Struts 2使用西班牙地区语言代替英语,在index.jsp中加入以下标记。

Specify The Locale As a URL Parameter
<h3>Registro español</h3>
<s:url action="registerInput" var="registerInputLinkES">
    <s:param name="request_locale">es</s:param>
</s:url>
<p><a href="${registerInputLinkES}">Por favor, regístrese</a> para nuestro sorteo</p>

上述标记我们在url中加入了一个request_locale参数,值为es,action发现这个参数,会查找es结尾的properties文件,例如Register_es.properties文件,会使用这个文件中的内容。

点击注册链接后,会看到西班牙语label的表单。

带_es的文件对于包级别的properties文件和全局的properties文件都是一样的,使用这些,我们可以建立全页面都是西班牙语的应用。

总结

学习了消息资源文件和国际化,想了解更多请参考官方文档http://struts.apache.org

下篇

下篇教程学习配置Struts 2来处理未捕获异常。