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 {
- // 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;
- }
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>
struts.custom.i18n.resources国际化详解(一)的更多相关文章
- struts.custom.i18n.resources国际化
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom ...
- Java——Struts2 之国际化 struts.custom.i18n.resources=globalMessages
1.在src下 建立 struts.properties 文件,内容为:struts.custom.i18n.resources=globalMessages struts.custom.i18n.r ...
- struts.custom.i18n.resources 如何配置多个资源文件?
struts.custom.i18n.resources = resources1,resources2,resources3 配置properties文件
- Struts 2中的constant详解
通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. 1.<constant ...
- Struts 2中的constant详解【转载】
通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. struts.xml 1.&l ...
- iOS开发——高级技术&本地化与国际化详解
本地化与国际化详解 效果如下: 英语: 中文: 具体实现如下: ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- java——国际化详解
深入理解Java国际化 假设我们正在开发一个支持多国语言的Web应用程序,要求系统能够根据客户端的系统的语言类型返回对应的界面:英文的操作系统返回英文界面,而中文的操作系统则返回中文界面--这便是典型 ...
- ssh框架中struts.xml 的配置参数详解
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
随机推荐
- DEDE数据库修改
dede数据库配置文件所在路径为:/data/common.inc.php 修改方法: 下面是该dede数据库配置文件的内容: <?php //数据库连接信息 $cfg_dbhost = 'lo ...
- HTML&CSS基础学习笔记1.13—无序列表
无序列表 有时我们的工作繁忙,杂事很多,怕忘记,就会把事情一件件列出来,防止忘记. 它们的排列顺序对于我们来说并不重要,可以随意调换,我们将它称为无序列表,HTML里用<ul>标签来表示无 ...
- http://www.cnblogs.com/zhuli19901106/p/4356419.html
http://www.cnblogs.com/zhuli19901106/p/4356419.html
- C程序设计语言练习题1-2
练习1-2 做个实验,当printf函数的参数字符串中包含\c(其中c是上面的转义字符串序列中未曾列出的某一个字符)时,观察一下会出现什么情况. 代码如下: #include <stdio.h& ...
- soapUI通过groovy脚本设置超时时间
import com.eviware.soapui.SoapUI import com.eviware.soapui.settings.HttpSettings import com.eviware. ...
- 拍照-----------android系统 至关重要的功能
一 在Android 中,拍照对应的Action 是android.provider.MediaStore.ACTION_IMAGE_CAPTURE. 用于拍照的Activity 需要返回照片图像数据 ...
- document.readystate
http://www.cnblogs.com/lhb25/archive/2009/07/30/1535420.html http://www.cnblogs.com/haogj/archive/20 ...
- 原型模式 - OK
原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单说来原型模式就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节. 原型模式UML ...
- 为MVC 添加下载权限
今天碰到一个错误,极其郁闷,本地开发和本地部署测试没有问题,但是放到阿里云上,出现了权限问题. 报错:ASP.NET 无权访问所请求的资源.请考虑对 ASP.NET 请求标识授予. 参考网上很多资料, ...
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...