Struts2 国际化支持
2014-04-21 19:17
393 查看
struts2 的国际化支持
文章分类:Java编程 关键字: struts2 国际化 i18n
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化
首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties
1. jsp页面的国际化
通过使用标签<s:text name="label.helloWorld"/>输出国际化
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). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面两个都为输出一个hello word的两种表示
<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
显示一个文本框,文本框的标题进行国际化
(2). 使用<s:i18n>标签指定从某个特定的资源文件中取数据
<s:i18n name="messageResource">
<s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在从messageResource取资源
(3).
<s:text name="label.hello">
<s:param>callan</s:param>
</s:text>
使用带参数的资源.<s:param>可以替换label.hello=hello {0}中的{0}
2. Action的国际化
Action的国际化主要是通过getText(String key)方法实现的
Java代码
1. public String execute() throws Exception {
2.
3.
4.
5. // getText(String) string为key
6.
7. String str1 = getText("label.helloWorld");
8.
9. System.out.println(str1);
10.
11.
12.
13. // 带参数的
14.
15. String str2 = getText("label.hello",new String[]{"fjf"});
16.
17. System.out.println(str2);
18.
19.
20.
21. // 与上一种实现一样
22.
23. List l = new ArrayList();
24.
25. l.add("callan");
26.
27. String str3 = getText("label.hello",l);
28.
29. System.out.println(str3);
30.
31.
32.
33. return SUCCESS;
34.
35. }
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. 使用校验框价时,提示信息可以国际化
<field name="userName">
<field-validator type="requiredstring">
<message key=”userName.required”> </message>
</field-validator>
</field>
国际化资源文件分为三种级别
(1) 全局资源文件,可以被整个应该程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包级资源文件,每个包的根目录下可以新建资源文件,仅被当前包中的类访问.文件名格式为:package_语言代码_国家代码.
(3) Action级资源文件,仅被当前Action引用,名称为action名_语言代码_国家代码
查找顺序为从小范围到大范围, Action级优先级最大
struts2 国际化的介绍
1、应用基础
(国际化Internationalization-I18N,本地化Localization-L10N。)
Java程序国际化主要通过3个类完成:
iava.util.ResourceBundle:用于加载一个资源包。
java.util.Locale:对应一个特定的国家/区域、语言环境。
java.util.ResourceBundle:用于将消息格式化。
2、准备资源
为了实现国际化,必须先提供程序所需的资源文件(key-value对)。资源文件的命名可以有一下三种形式:
baseName_language_country.properties
baseName_language.properties
baseName.properties
其中baseName是资源文件的基本名,用户可以自由定义。而language和country都不是可以随便定义的,必须是Java所支持的语言和国家。
3、Struts2加载全局资源文件
加载全局资源文件的方式是通过配置常量来实现的。(struts.custom.i18n.resources)
4、访问国际化资源
JSP页面输出:<s:text name=”username” /> 用name属性来加载资源文件的key值。
Action中访问:可以使用ActionSupport类的getText方法。该方法可以接受一个name参数,既资源文件的key值。
表单元素的Label里输出:直接用对应表单的key属性。
5、输出带占位符号的国际化信息
JSP页面输出:在<s:text name=”username” />标签中使用多个<s:param …/>
Action中访问:使用getText(String aTextName,List args)或者getText(String key,String[] args)。
struts2 的国际化支持
文章分类:Java编程 关键字: struts2 国际化 i18n
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化
首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties
1. jsp页面的国际化
通过使用标签<s:text name="label.helloWorld"/>输出国际化
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). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面两个都为输出一个hello word的两种表示
<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
显示一个文本框,文本框的标题进行国际化
(2). 使用<s:i18n>标签指定从某个特定的资源文件中取数据
<s:i18n name="messageResource">
<s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在从messageResource取资源
(3).
<s:text name="label.hello">
<s:param>callan</s:param>
</s:text>
使用带参数的资源.<s:param>可以替换label.hello=hello {0}中的{0}
2. Action的国际化
Action的国际化主要是通过getText(String key)方法实现的
Java代码
1. public String execute() throws Exception {
2.
3.
4.
5. // getText(String) string为key
6.
7. String str1 = getText("label.helloWorld");
8.
9. System.out.println(str1);
10.
11.
12.
13. // 带参数的
14.
15. String str2 = getText("label.hello",new String[]{"fjf"});
16.
17. System.out.println(str2);
18.
19.
20.
21. // 与上一种实现一样
22.
23. List l = new ArrayList();
24.
25. l.add("callan");
26.
27. String str3 = getText("label.hello",l);
28.
29. System.out.println(str3);
30.
31.
32.
33. return SUCCESS;
34.
35. }
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. 使用校验框价时,提示信息可以国际化
<field name="userName">
<field-validator type="requiredstring">
<message key=”userName.required”> </message>
</field-validator>
</field>
国际化资源文件分为三种级别
(1) 全局资源文件,可以被整个应该程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包级资源文件,每个包的根目录下可以新建资源文件,仅被当前包中的类访问.文件名格式为:package_语言代码_国家代码.
(3) Action级资源文件,仅被当前Action引用,名称为action名_语言代码_国家代码
查找顺序为从小范围到大范围, Action级优先级最大
struts2 国际化的介绍
1、应用基础
(国际化Internationalization-I18N,本地化Localization-L10N。)
Java程序国际化主要通过3个类完成:
iava.util.ResourceBundle:用于加载一个资源包。
java.util.Locale:对应一个特定的国家/区域、语言环境。
java.util.ResourceBundle:用于将消息格式化。
2、准备资源
为了实现国际化,必须先提供程序所需的资源文件(key-value对)。资源文件的命名可以有一下三种形式:
baseName_language_country.properties
baseName_language.properties
baseName.properties
其中baseName是资源文件的基本名,用户可以自由定义。而language和country都不是可以随便定义的,必须是Java所支持的语言和国家。
3、Struts2加载全局资源文件
加载全局资源文件的方式是通过配置常量来实现的。(struts.custom.i18n.resources)
4、访问国际化资源
JSP页面输出:<s:text name=”username” /> 用name属性来加载资源文件的key值。
Action中访问:可以使用ActionSupport类的getText方法。该方法可以接受一个name参数,既资源文件的key值。
表单元素的Label里输出:直接用对应表单的key属性。
5、输出带占位符号的国际化信息
JSP页面输出:在<s:text name=”username” />标签中使用多个<s:param …/>
Action中访问:使用getText(String aTextName,List args)或者getText(String key,String[] args)。
相关文章推荐
- struts2 的国际化支持
- struts2国际化支持
- struts2之5--国际化支持
- struts2的国际化支持
- 使用Struts2国际化支持
- java struts2国际化支持
- struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持
- 关于displaytag中支持struts2国际化问题解答方法
- struts2国际化支持
- Struts2 配置国际化支持
- struts2国际化支持
- struts2 的国际化支持
- Struts2国际化支持(中英文选择)
- Struts2对国际化的支持
- struts2 的国际化支持
- Struts2的国际化支持
- Struts2对国际化的支持---笔记
- struts2对国际化的支持
- Struts2对于国际化(i18n)的支持
- 2个小问题:Struts2 国际化支持和下载文件的开发思路