每种框架都会有国际化的支持,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)方法实现的
- public String execute() throws Exception {
-
-
-
-
-
- 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;
-
- }
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.0国际化实现,文档说明文档说明文档说明
struts1.0,struts2.0国际化
http://blog.csdn.net/liuhongjavaen/article/details/7655385 具体操作事项请查阅博客 struts2国际化源码 eclipse国际化插件
这是我用struts2.0 spring2.0 hibernate3.0 开发的一个小小的新闻系统,其中主要用到了struts2 的相关技术,对于想学习struts2.0的人来说,还是有不少的收获的.系统支持国际化,目前有中文和英文两个版本支持.系统内多处...
struts2.0标签、国际化、ioc、上传文件等说明
本源码是目前刚开始流行的struts2.0框架的源码,其中包括基本的数据库操作,并且含有消息国际化和报表打印,适合企业开发。
采用了struts2.0框架,拦截器,国际化,i18n,action自动获取表单的属性值,FileterDispatch等等,struts标签
最近收集到的struts2.0教程.包含有拦截器,上下传,CRUD,tablibs,国际化,struts2与ajax结合等等.........
学习Struts的前提 Struts简介 第一个Struts例子 Struts常用的标签 Struts的Action Struts的Result Struts的表单 ...国际化(i18n)的实现 Struts的文件上传 上传应用:学生多媒体信息管理 OGNL的应用
·Struts2国际化(i18n)您的应用程序 ·Struts2.0转换器(Converter) ·Struts 2.0中实现表单数据校验(Validation) ·拦截器(Interceptor) ·Struts 2中实现IoC ·Struts 2中实现文件上传 ·Struts 2中的...
主要讲述struts2的核心概念、表单验证、国际化、拦截器、IOC模式、标签、表达式、Ajax、上传下载、视图等,内容详细,不容错过的
学习如何配制,应用struts2(包括国际化上传等一些小列子)
在Struts 2.0中国际化(i18n)您的应用程序 国际化是商业系统中不可或缺的一部分,所以无论您学习的是什么Web框架,它都是必须掌握的技能。
国际化篇 使用myeclipse开发最精简教程,适合有一定java基础人士
国际化篇 使用myeclipse开发 最精简教程,适合有一定java基础人士
实现以下技术: 增删查改 国际化 生成EXCEL报表等 由于项目过大,所以没有把jar包传上
Struts 2.0的标志(Tag)介绍; Struts 2.0的Action讲解; Struts 2.0的国际化; 转换器; 转换与校验; 拦截器; 在Struts 2中实现文件上传;等等.
struts2 标签 国际化 文件上传 校验 拦截器 struts2.0的标签库(简介) 用过struts1.x的人都知道,标签库有html、bean、logic、tiles, 而struts2.0里的标签却没有分类,只用在jsp头文件加上 ...
精简全面讲解Struts 2.0Struts2产生背景 工作原理 实现步骤 标签 ...国际化 输出 令牌机制 拦截器 表单校验(jQuery.validate验证框架) 上传 下载 OGNL(ActionContext StackContext ValueStack区别)
国际化配置-->globalMessages_zh.properties(中文) globalMessages_en.properties(英文) 页面美工 --> 页面按钮样式及CSS样式文件 生成的代码直接拷贝到MyEclipse里,做简单设置,发布后即可运行! 最多3分钟的...