`
wanxiaotao12
  • 浏览: 456002 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2的国际化 Action类中的国际化

 
阅读更多

转:http://callan.iteye.com/blog/186014

每种框价都会有国际化的支持,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)方法实现的

getText(String aTextName, String defaultValue), 也可以带参数, 参数的形式可以是String, List, String[]

Java代码 复制代码 收藏代码
  1. public String execute() throws Exception {
  2. // getText(String) string为key
  3. String str1 = getText("label.helloWorld");
  4. System.out.println(str1);
  5. // 带参数的
  6. String str2 = getText("label.hello",new String[]{"fjf"});
  7. System.out.println(str2);
  8. // 与上一种实现一样
  9. List l = new ArrayList();
  10. l.add("callan");
  11. String str3 = getText("label.hello",l);
  12. System.out.println(str3);
  13. return SUCCESS;
  14. }
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. 参数化国际化(带参数的), 在properties文件中
在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级优先级最大

分享到:
评论

相关推荐

    Struts action 国际化 in18

    Struts action 国际化 in18

    Struts2 国际化字符串 拦截器

    文件的国际化字符串中使用OGNL,格式为${表达式},例如: validation.required=${getText(fileName)} is required 2. 使用java.text.MessageFormat中的字符串格式,格式为{ 参数序号(从0开始), 格式类形(number |...

    struts2国际化

    struts2中的国际化分为3类 Action级别:只能为一个Action服务 资源文件的前缀名和相应的Action同名,而且在同一个包中 package级别:为同包的Action服务 资源文件的前缀必须以package开头,建立在某个包下,表示该包中的...

    Struts2 in action中文版

    第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 ...

    struts2帮助文档

    struts2中的国际化 struts2转化器 struts2实现表单数据校验 struts2的基石-拦截器 struts2中实现IOC struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2...

    畅所欲言struts2的国际化

    本文档包含了struts2的JSP页面、Action类以及校验框架xml的国际化支持

    Struts2全解Struts2全解

    5struts2国际化 ......... 6 struts2输入校验 ......... 7 struts2 OGNL(对象图形化导航语言) ........ 8 struts2拦截器 ......... 9 struts2类型转换 ........ 10struts2标签库 ........ 11、访问数据库 ........

    Struts2 如何使Action取得属性范围 通配符 国际化等内容

    详细介绍Struts2 如何使Action取得属性范围 通配符 国际化等内容,并使用事例说明

    struts2 标签换行

    如下代码:&lt;s:form action="login2"&gt; 用户名" name="username"/&gt; 密码" name="password"/&gt; 提交"/&gt;我们看着以上的代码跟HTML的差不了多少,但是因为struts2表单默认将表单内的每一个元素都分在单独的一行

    struts2入门教程

    ·Struts2国际化(i18n)您的应用程序 ·Struts2.0转换器(Converter) ·Struts 2.0中实现表单数据校验(Validation) ·拦截器(Interceptor) ·Struts 2中实现IoC ·Struts 2中实现文件上传 ·Struts 2中的...

    Struts2框架 jar JAVA开发 Struts2.jar架包 Struts2开发实例

    Struts2 概述 如何应用Struts2 Struts2 配置文件 如何编写Action 拦截器 Struts2的标签 OGNL 数据校验 国际化

    struts2 详解文档

    介绍Struts 2及Struts 2开发环境的搭建 第一个Struts 2应用开发 ...配置Action范围国际化资源文件 ognl表达式 Struts 2常用标签解说 使用标签防止表单重复提交 Struts 2+Spring 2.5+Hibernate 3.3整合开发

    struts2讲义_吴峻申

    9.2.2 Struts2Action范围属性文件国际化应用 187 9.2.3 Struts2临时范围属性文件国际化应用 188 9.3 用户主动选择国际化应用介绍 191 第10章 Struts2页面布局实现 194 10.1 sitemesh基本使用方法 194 10.2 sitemesh...

    Struts2 学习笔记

    三、 Struts2国际化 35 1、 Action级别 35 2、 Package级别 36 3、 Application级别 36 四、 资源文件中的参数处理 37 五、 国际化-动态语言切换 37 09 自定义拦截器 38 10 类型转换 38 Struts2总结 39

    Struts简介 什么是Struts Struts基本运作流程

    Struts简介 什么是Struts Struts基本运作流程 ActionMapping类 Action类 ActionForm类 ActionError与ActionMessage 协同开发 模块化程序 Struts异常处理 Struts国际化支持 PlugIn接口 等等

    Struts2入门教程(全新完整版)

    1.概述strust2中的拦截器 28 2.自定义拦截器 28 方式一,实现Interceptor接口。 28 方式二、继承AbstractInterceptor抽象类 29 方式三、继承MethodFilterInteceptor类 30 3.使用来MethodFilterInterceptor灵活拦截 ...

    Struts2属性文件详解

    该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开. struts.diSPAtcher.parametersWorkaround 对于某些Java EE服务器,不支持HttpServlet Request...

    struts2.1.8学习

    、Struts2 主要几句话: 1、Struts2 是由webwork2 发展来的而非Struts1,相比Struts1,Struts2 编码规范跟类似与webwork2 ...8、Struts2 提供了全局范围、包范围和Action 范围的国际化资源文件管理实现

    Struts中文手册[文字版][中文]

    2.1.4. 消息标记和国际化.15 2.2. 逻辑标记.16 2.2.1. 条件逻辑.17 2.2.2. 重复标记.18 2.2.3. 转发和重定向标记.19 2.3. HTML 标记20 2.3.1. 显示表单元素和输入控件.20 a) 表单标记.21 b) 按钮和取消标记.22 c) ...

Global site tag (gtag.js) - Google Analytics