struts2-19配置国际化全局资源文件、输出国际化信息
来源:互联网 发布:软件开发职位 编辑:程序博客网 时间:2024/06/05 08:31
国际化:
baseName_language_country.properties[一般跟struts.xml文件放在一起]
baseName_language.properties
baseName.properties
其中baseName时资源文件的基本名,可以自定义,蛋language和country必须时java支持的语音和国家。如:
中国大陆:baseName_zh_CN.properties
美国:baseName_en_US.properties
现在为应用添加两个资源文件:
第一个存放中文:test_zh_CN.properties
内容为:welcome=欢迎来到java。
第二个存放英语(美国):test_en_US.properties
内容为:welcome=welcome to java
对于中文的属性文件,编写好后,应该使用jdk提供的native2asii命令把文件转换为Unicode编码的文件。命令的使用方式如下:
native2asii 源文件.properties 目标文件.properties
配置全局资源与输出国际化信息
准备好资源文件之后,可以在struts.xml中通过struts.custom.i18n.resources常量把资源文件定义为全局资源文件,如下:
<constant name = "struts.custom.i18n.resources" value = "test" />
后面就可以在页面或在action中访问国际化信息:
- 在JSP页面中使用标签输出国际化信息:,name为资源文件中的key
- 在action类中,可以继承ActionSupport,使用getText()方法得到国际化信息,该方法的第一个参数用于指定资源文件中的key。
在表单标签中,通过key属性指定资源文件中的key,如:
例子:【全局范围的国际化】
test_zh_CN.properties中:[跟struts.xml放同一个目录]
welcome=欢迎来到java。
test_en_US.properties中:
welcome=welcome to java
struts.xml:
<struts> <constent name = "struts.custom.i18n.resources" value = "test" /> <package name = "person" namespace = "/person" extends = "struts-default"> <action name = "manage" class = "cn.gz.action.PersonManageAction"> <result name = "message"> /WEB-INF/page/message.jsp </result> </action> </package></struts>
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>国际化</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:text name = "welcome" /> </body></html>
URL:http://localhost:8080/native/index.jsp
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{ public String execute() throws Exception{ ActionContext.getContext().put("message",this.getText("welcome"); return "message"; }}
message.jsp:
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>结果信息</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> ${message} </body></html>
URL:http://localhost:8080/native/person/manage.action
国际化-输出带占位符的国际化信息
资源文件中的内容如下:
welcome={0},欢迎来到国际化{1}
在jsp页面中输出带占位符的国际化信息
<s:text name = "welcome"> <s:param><s:property value = "realname"/></s:param> <s:param>学习</s:param></s:text>
在action类中获取带占位符的国际化信息,可以使用getText(String key,String[] args)或getText(String aTextName,List args)方法。
例子:
test_zh_CN.properties中:[跟struts.xml放同一个目录]
welcome={0},欢迎来到java{1}
test_en_US.properties中:
welcome={0},welcome to java{1}
struts.xml:
<struts> <constent name = "struts.custom.i18n.resources" value = "test" /> <package name = "person" namespace = "/person" extends = "struts-default"> <action name = "manage" class = "cn.gz.action.PersonManageAction"> <result name = "message"> /WEB-INF/page/message.jsp </result> </action> </package></struts>
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>国际化</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:text name = "welcome"> <s:param>xiaohong</s:param> <s:param>study</s:param> </s:text> </body></html>
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{ public String execute() throws Exception{ ActionContext.getContext().put("message",this.getText("welcome"); return "message"; }}
url:http://localhost:8080/native/index.jsp
PersonManageAction.java:
public class PersonManageAction extends ActionSupport{ public String execute() throws Exception{ ActionContext.getContext().put("message",this.getText("welcome",new String[]("xiaohong","study")); return "message"; }}
URL:http://localhost:8080/native/person/manage.action
配置包范围的国际化资源
国际化-包范围资源文件
在一个大型应用中,整个应用有大量的内容需要实现国际化,如果我们把国际化的内容都放置在全局资源属性文件中,显然会导致资源文件变得过于庞大,臃肿,不便于维护,这个时候我们可以针对不同模块,使用包范围类组织国际化文件。
方法如下:
在java的包下放置package_language_country.properties资源文件,package为固定写法,处于该包及子包下的action都可以访问该资源。当查找指定key的消息时,系统会先从package资源文件查找,当找不到对应的key时,才会从常量struts.custom.i18n.resources指定的资源文件中寻找。
例子:
package_zh_CN.properties中:[跟action放同一个目录]
welcome={0},欢迎来到java{1}
package_en_US.properties中:
welcome={0},welcome to java{1}
国际化-action范围资源文件
可以为某个action单独指定资源文件,方法如下:
在action类所在的路径,放置ActionClassName_language_country.properties资源文件,ActionClassName为action类的简单名称。
当查找指定key的消息时,系统会先从ActionClassName_language_country.properties资源文件查找,如果没有找到对应的key,然后沿着当前包往上查找基本名为package的资源文件,一直找到最顶层包。如果还没有找到对应的key,最后会从常量struts.custom.i18n.resources指定的资源文件中寻找。
例子:
PersonManageAction_zh_CN.properties中:[跟action放同一个目录]
welcome={0},欢迎来到java{1}
PersonManageAction_en_US.properties中:
welcome={0},welcome to java{1}
国际化-jsp中直接访问某个资源文件
struts2提供了标签,使用标签可以在类路径下直接从某个资源文件中获取国际化数据,而无需任何配置:
<s:i18n name = "test"> <s:text name = "welcome"/></s:i18n>
test为类路径下资源文件的基本名。
如果要访问的资源文件在类路径的某个包下,可以这样访问:
<s:i18n name = "cn/gz/action/*package*"> <s:text name = "welcome"/> <s:param>小红</s:param></s:i18n>
上面访问cn.gz.action包下基本名为package的资源文件。
index.jsp:
访问的是PersonManageAction_en_US.properties资源文件:
<s:i18n name = "cn/gz/action/PersonManageAction"> <s:text name = "welcome"/> <s:param>小红</s:param></s:i18n>
- struts2-19配置国际化全局资源文件、输出国际化信息
- struts2配置国际化全局资源文件 并 输出国际化信息
- struts2国际化---配置国际化全局资源文件并输出国际化资源信息
- 配置国际化全局资源文件、输出国际化信息
- 国际化全局资源文件输出国际化信息
- 复习struts2之配置国际化全局资源文件、输出国际化信息
- Struts2的国际化、配置全局资源与输出国际化信息、国际化—JSP中直接访问某个资源文件
- 第二十四章 配置国际化全局资源文件、输出国际化信息
- Struts2_国际化、配置全局资源与输入国际化信息
- struts2 资源文件:国际化
- Struts2配置国际化信息
- struts2国际化---输出带有占位符的国际化信息以及包范围、Action范围资源文件访问
- struts2国际化文件配置
- struts2国际化资源的配置
- struts2引用国际化资源文件
- struts2 国际化资源文件测试
- Struts2定义国际化资源文件
- Struts2简单配置国际化资源文件及用法
- 代理模式--静态代理
- 计算机科学与Python编程导论_MIT 学习笔记(六)
- No compiler is provided in this environment
- 统计N!末位0的数量的一个思路
- python2.7学习笔记(一)数据类型及基础
- struts2-19配置国际化全局资源文件、输出国际化信息
- 《精要主义》读书笔记(一)
- 使用Jenkins自动部署博客
- AJ故事---准备
- 最优化问题
- iOS应用剖析 ---- 对plist文件进行处理
- 华硕笔记本电脑开机蓝屏怎么解决
- java使用IO打印流输出到文件
- 周末训练笔记+Codeforces Round #451 (Div. 2)