struts.custom.i18n.resources
每种框价都会有国际化的支持,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级优先级最大
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.custom.i18n.resources</param-name>
<param-value>globalMessages</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
相关推荐
整理struts2国际化例子源码整理struts2国际化例子源码整理struts2国际化例子源码
Struts2国际化的实现原理;简单的struts2国际化实现过程的讲解
Struts2国际化
struts2国际化 标签 页面 处理类 实例,特殊注意点说明
struts2 国际化
导入相关的jar包,直接发布便可以运行 struts2国际化+简单的标签+用户注册和登录(不实用数据库)
struts2国际化MyEclipse开发struts2国际化MyEclipse开发童叟无欺
struts2国际化ppt和代码,这是上课老师给的课件和案例。可以参考一下适合初学者
struts2 国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化。程序在不修改内部代码的情况下,能根据不同语言及地区显示...
http://blog.csdn.net/liuhongjavaen/article/details/7655385 具体操作事项请查阅博客 struts2国际化源码 eclipse国际化插件
struts2国际化测试小demo
Struts1 和 Struts 2 国际化全局资源,有文档说明、有图形加以说明,还有源代码。
struts国际化的描述,其中包括在配置文件中配置实现,以及在后台中或者是前台中处理实现国际化的过程。
另外,关于struts2国际化乱码问题: 解决方法: 找到jdk的安装目录--C:\Program Files\Java\jdk1.6.0_10\bin(我的安装目录) 在bin里面找到native2ascii.exe并运行 在命令窗口输入 native2ascii -encoding UTF-8 ...
相关的国际化的知识点,写了一个小demo
自己做的一个简单的国际化例子,Struts2框架,myEclipse.
励智教育,struts2国际化处理教程案例及幻灯片,网址:http://www.lizhi.cc
文件的国际化字符串中使用OGNL,格式为${表达式},例如: validation.required=${getText(fileName)} is required 2. 使用java.text.MessageFormat中的字符串格式,格式为{ 参数序号(从0开始), 格式类形(number |...
struts2 国际化 i18n struts2 国际化 i18n struts2 国际化 i18n