每种框价都会有国际化的支持,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 {
// 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级优先级最大
相关推荐
NULL 博文链接:https://kencheng.iteye.com/blog/602497
实现struts2国际化支持,下载后直接可以部署运行,案例通俗易懂!我们公司项目里面就是按照我的案例来实现国际化的
struts2 语言国际化配置和使用、页面按钮实现改变显示语言
Struts对国际化支持,国际化概念及具体操作详细讲解,属于专题报告类
Struts2的国际化支持及JDK对国际化的支持深入详解,是一段视频,讲解细致
一个Web应用程序在运行时能够根据客户端请求所来自的国家和语言显示不同的用户界面。这样 ,当需要在应用程序中添加对一种新的语言的支持时,无需修改应用程序的代码。
Struts2(国际化支持)公司实战开发项目
整个工程的源码,拷过去即可运行,例子简单明了。 自己经过几日的调查开发实现的,希望对大家有帮助。
本文档包含了struts2的JSP页面、Action类以及校验框架xml的国际化支持
Java国际化支持实例 博文链接:https://zmx.iteye.com/blog/461474
国际化与本地化的概念,Web应用的中文本地化,Struts框架对国际化的支持
通过定制jsp标签库,实现如下功能: 1.支持语言国际化(类似struts text标签) 2.支持图片文件、JS文件国际化
第2章 Struts 2下的HelloWorld,第3章 Struts 2基础,第4章 深入Struts 2,第5章 Struts 2的类型转换,第6章 文件的上传和下载.,第7章 Struts 2的拦截器,第8章 Struts 2的输入校验,9.2 Struts 2的国际化入门,...
包括Struts 2中配置文件的详细讲解、国际化与异常处理、数据类型的转换、数据的输入校验、Struts 2强大的标签库、Struts 2中文件的上传与下载、Struts 2对Ajax的支持、Struts 2与流行框架的整合,最后以3个实例由...
书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。...
书中介绍了如何利用Struts2来解决Web应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts2应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax的支持,等等。书中概念清晰...
在struts2中,前端页面表示国际化的实现更加简单。简单的应用struts2提供的支持国际化的表达式即可快速方便的进行页面的国际化的实现。如何做呢?本文以英文和中文为例进行说明。
第11章 理解国际化 240 11.1 Struts 2框架和Java i18n 241 11.1.1 使用ResourceBundle和Locale取得本地化文本 241 11.1.2 Struts 2如何解决本地Java对i18n支持的问题 243 11.2 Struts 2 i18n示例 244 11.2.1 Struts ...
该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开. struts.diSPAtcher.parametersWorkaround 对于某些Java EE服务器,不支持HttpServlet Request...