Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6958
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-19 11:23
文章分类

全部博文(7)

文章存档

2015年(7)

我的朋友
最近访客

分类: Java

2015-01-27 21:37:30

struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化

 

首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入

 
其中value值自定义

资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties

 

1. jsp页面的国际化
通过使用标签输出国际化
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).

上面两个都为输出一个hello word的两种表示

 



显示一个文本框,文本框的标题进行国际化

 

(2). 使用标签指定从某个特定的资源文件中取数据

  

指定在从messageResource取资源

 

(3).

   callan

使用带参数的资源.可以替换label.hello=hello {0}中的{0}

 

2. Action的国际化
Action的国际化主要是通过getText(String key)方法实现的

 

 

  1. public String execute() throws Exception {   
  2.   
  3.            
  4.   
  5.         // getText(String) string为key   
  6.   
  7.          String str1 = getText("label.helloWorld");   
  8.   
  9.          System.out.println(str1);   
  10.   
  11.            
  12.   
  13.         // 带参数的   
  14.   
  15.          String str2 = getText("label.hello",new String[]{"fjf"});   
  16.   
  17.          System.out.println(str2);   
  18.   
  19.        
  20.   
  21.         // 与上一种实现一样   
  22.   
  23.          List l = new ArrayList();   
  24.   
  25.          l.add("callan");   
  26.   
  27.          String str3 = getText("label.hello",l);   
  28.   
  29.          System.out.println(str3);   
  30.   
  31.            
  32.   
  33.         return SUCCESS;   
  34.   
  35.      }  

 

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. 使用校验框价时,提示信息可以国际化
  
  
   
  

 


国际化资源文件分为三种级别
(1) 全局资源文件,可以被整个应该程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包级资源文件,每个包的根目录下可以新建资源文件,仅被当前包中的类访问.文件名格式为:package_语言代码_国家代码.
(3) Action级资源文件,仅被当前Action引用,名称为action名_语言代码_国家代码
查找顺序为从小范围到大范围, Action级优先级最大


   struts2
   org.apache.struts2.dispatcher.FilterDispatcher
  
            struts.custom.i18n.resources
            globalMessages
       




   struts2
   /*

阅读(606) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~