---
title: "Struts2 国际化"
author: "yongjiucui"
date: "2012-04-17T17:16:06Z"
tags: []
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2012-04-17T17:16:06Z"
id: 1488650
url: "https://cuiyj3.iteye.com/blog/1488650"
---

# [struts.custom.i18n.resources](http://www.cnblogs.com/qinqinmeiren/archive/2010/09/27/2151708.html)

**struts.custom.i18n.resources**

 

 

每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化

 

首先在struts.properties文件中加入以下内容:  
struts.custom.i18n.resources=messageResource  
或在struts.xml中加入  
&lt;constant name="struts.custom.i18n.resources" value="messageResource"&gt;&lt;/constant&gt;

 

资源文件的命名格式: 名称\_语言代码\_国家代码. Properties  
如果创建中文和英语国际化,那么资源文件名称为  
messageResource\_zh\_CN.properties和messageResource\_en\_US.properties

 

1\. jsp页面的国际化  
通过使用标签&lt;s:text name="label.helloWorld"/&gt;输出国际化  
label.helloWorld为资源文件中定义的key

 

在messageResource\_en\_US.properties加入以下内容  
label.hello=hello {0}  
label.helloWorld=hello,world

在messageResource\_zh\_CN.properties加入以下内容  
label.hello=你好 {0}  
label.helloWorld=你好,世界

 

(1). &lt;s:text name="label.helloWorld"/&gt;  
&lt;s:property value="%{getText('label.helloWorld')}"/&gt;  
上面两个都为输出一个hello word的两种表示

 

&lt;s:textfield name="name" key="label.helloWorld"/&gt;  
&lt;s:textfield name="name" label="%{getText('label.helloWorld')}"/&gt;  
显示一个文本框,文本框的标题进行国际化

 

(2). 使用&lt;s:i18n&gt;标签指定从某个特定的资源文件中取数据  
&lt;s:i18n name="messageResource"&gt;  
&lt;s:text name="label.helloWorld"&gt;&lt;/s:text&gt;  
&lt;/s:i18n&gt;  
指定在从messageResource取资源

 

(3).  
&lt;s:text name="label.hello"&gt;  
&lt;s:param&gt;callan&lt;/s:param&gt;  
&lt;/s:text&gt;  
使用带参数的资源.&lt;s:param&gt;可以替换label.hello=hello {0}中的{0}

 

2\. Action的国际化  
Action的国际化主要是通过getText(String key)方法实现的

 

 

Java代码

01. **public** String execute() **throws** Exception {
02. // getText(String) string为key
03. String str1 = getText("label.helloWorld");
04. System.out.println(str1);
05. // 带参数的
06. String str2 = getText("label.hello",**new** String\[]{"fjf"});
07. System.out.println(str2);
08. // 与上一种实现一样
09. List l = **new** ArrayList();
10. l.add("callan");
11. String str3 = getText("label.hello",l);
12. System.out.println(str3);
13. **return** SUCCESS;
14. }

```
public String execute() throws Exception {

  

  // getText(String) string为key

  String str1 = getText("label.helloWorld");

  System.out.println(str1);

  

  // 带参数的

  String str2 = getText("label.hello",new String[]{"fjf"});

  System.out.println(str2);

 

  // 与上一种实现一样

  List l = new ArrayList();

  l.add("callan");

  String str3 = getText("label.hello",l);

  System.out.println(str3);

  

  return SUCCESS;

 }
```

 

3\. 参数化国际化  
在messageResource\_en\_US.properties加入以下内容  
userName=userName  
userName.required=${getText('userName')} is required

 

在messageResource\_zh\_CN.properties加入以下内容  
userName=用户名  
userName.required=${getText('userName')} 不能为空

 

在Action中  
String str4 = getText("userName.required");  
System.out.println(str4);

 

userName.required=${getText('userName')}会取国际化的用户名

 

4\. 使用校验框价时,提示信息可以国际化  
&lt;field name="userName"&gt;  
&lt;field-validator type="requiredstring"&gt;  
&lt;message key=”userName.required”&gt; &lt;/message&gt;  
&lt;/field-validator&gt;  
&lt;/field&gt;

 

国际化资源文件分为三种级别  
(1) 全局资源文件,可以被整个应该程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件  
(2) 包级资源文件,每个包的根目录下可以新建资源文件,仅被当前包中的类访问.文件名格式为:package\_语言代码\_国家代码.  
(3) Action级资源文件,仅被当前Action引用,名称为action名\_语言代码\_国家代码  
查找顺序为从小范围到大范围, Action级优先级最大

&lt;filter&gt;  
&lt;filter-name&gt;struts2&lt;/filter-name&gt;  
&lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;  
&lt;init-param&gt;  
&lt;param-name&gt;struts.custom.i18n.resources&lt;/param-name&gt;  
&lt;param-value&gt;globalMessages&lt;/param-value&gt;  
&lt;/init-param&gt;  
&lt;/filter&gt;

&lt;filter-mapping&gt;  
&lt;filter-name&gt;struts2&lt;/filter-name&gt;  
&lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;