每种框价都会有国际化的支持,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代码
- String execute() Exception {
-
-
-
String str1 =
getText();
- System.out.println(str1);
-
-
-
String str2 =
getText(, String[]{});
- System.out.println(str2);
-
-
-
List l = ArrayList();
-
l.add();
-
String str3 =
getText(,l);
- System.out.println(str3);
-
-
SUCCESS;
- }
Java代码
- String execute() Exception {
-
-
-
String str1 =
getText();
- System.out.println(str1);
-
-
-
String str2 =
getText(, String[]{});
- System.out.println(str2);
-
-
-
List l = ArrayList();
-
l.add();
-
String str3 =
getText(,l);
- System.out.println(str3);
-
-
SUCCESS;
- }
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级优先级最大
相关推荐
struts2i18n.rar struts2 i18n的实例, 环境是myeclipse6. struts2.16. struts国际化。
非常清晰可用的struts2 i18n国际化实例,非常适合初学者,不会让大家失望的,有问题还请求指教,大家互相学习,谢谢
一个超级简单的Struts2国际化demo
struts_I18N的包,已经用jar打包好了,直接用
struts2 中际化示例,加上s2sh所需的Jar就可以运行!如果不行留言,把demo发给你! 示例图:http://blog.csdn.net/liuc0317/archive/2010/12/01/6048552.aspx
struts2国际化_i18n 完整实例。欢迎下载。
struts1中实现了国际化(i18n)的登陆界面
自己写的 本人也是刚刚接触struts2 所以写的东西比较适合刚刚接触struts2的新人。。
struts2 国际化 i18n struts2 国际化 i18n struts2 国际化 i18n
博文链接:https://zhengdl126.iteye.com/blog/413021
用MyEclipse工具实现的一个struts2国际化,用于刚接解struts2中i18n的同学学习。很好的代码,深入浅出。
NULL 博文链接:https://474802150.iteye.com/blog/2145744
根据不同的语言环境,读取不同的本地资源文件,自动实现本地化。
基于Struts2技术的i18n,Web项目的国际化,能够生成,中,日,英三种语言
NULL 博文链接:https://zhouxianglh.iteye.com/blog/521629
i18n Locale ResourceBundle MessageFormat 使用例子 在struts2学习开始前需要普及的知识点
NULL 博文链接:https://tianzhuowan.iteye.com/blog/1275521
Struts2中的validation_多国语言版 国际化+struts2数据校验.zip
struts国际化登录实例
国际化(i18n),struts 2实例,一步步教你如何实现一个示例