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)方法实现的


 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 }
36

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>

时间: 2024-08-10 19:42:15

struts.custom.i18n.resources的相关文章

struts入门实例

创建maven web项目 配置pom.xml 加入struts的依赖包 <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.13</version> </dependency> 下面是pom.xml的全部配置 <project xmlns="ht

Struts.xml讲解

解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/dtds/struts-2.0.dtd”是一个网络地址,如果上网的话,IDE会自动帮我们下载此文件,如果断网就没有办法了,但是我们还是有解决方法的.首先在源码包里找到struts-2.0.dtd这个文件在MyEclipse中菜单栏中选择:windowPreferencesMyEclipseFiles

浅谈struts2的国际化----i18n

可能大家在使用struts框架的时候,偶尔会看到这个词: i18n.也就是 Internationalization    i 开头,n 结尾, 总共18个字母,今天的主要内容就是围绕这 四个字母. struts2国际化是建立在 java 国际化的基础上的,通过提供不同国家.语言环境的消息资源,然后通过 ResourceBundle 加载指定Locale 对应的资源文件,取得文件中指定的 key 对应的信息. Locale 对象表示了特定的地理.政治和文化地区.需要 Locale 来执行其任务的

spring 4.x + struts 2.x + mybatis 3.x

寫 web 程式時,使用 spring + struts + mybatis 是很流行的架構,這麼多東西怎麼整合呢? 這裡提供一個參考,先看一下寫好的程式裡面有那些檔案… 這不是什麼標準,只是我個人的習慣,僅供參考,接下來逐步說明.這個程式只有兩個功能,一個是顯示所有股票資料,一個是可以新增股票資料,網頁的程式流程如下: 總共有五個網頁,如下: index.jsp: 首頁,可選擇要「查詢即時資訊」或「維護股票」. realtimeInfo.jsp: 顯示股票即時資訊. manageStock.j

Struts2对于i18n的支持

1 2 struts.xml中可以配置 3 <constant name="struts.custom.i18n.resources" value="itcast"> 指定全局资源文件 4 全局资源文件/包范围资源文件/动作类的资源文件 5 全局资源文件:src下放到WEB-INF/classes目录下 6 包范围资源文件:服务于Java类中的包下的动作类的. java类访问资源文件 7 取名:package_语言_国家.properties 8 9 动

struts2的返回类型、struts的标签库、struts2中完成国际化

struts2的返回类型:在jsp-servlet中, 返回的类型主要有 转发, 重定向: 转发主要是RequestDispatcher的forward方法来进行操作的req.getRequestDispatcher("goods/queryGoodFenYe.jsp").forward(req, resp);重定向主要使用response对象的一些方法来操作:1.response.sendRedirect("index.jsp");2.response.addHe

Struts(四)

==========================================OGNL=================================OGNL:Object Graph Navigation Language    1.取代页面中Java脚本,简化数据访问    2.和EL一样是表达式语言,功能更强大    功能和特点:    1.能存取对象的属性,也能调用对象的方法    2.能访问静态方法.静态属性    3.能访问值栈及StackContext    4.能操作集合

struts登录案例和自定义拦截器

struts登录案例:struts.xml<struts> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="messages"></constant> <package name="basic" ext

Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)

搭建的环境:eclispe下面的maven web项目 Struts:    2.5.10 Spring:    4.3.8 Hibernate:   5.1.7 .Final MySQL:   5.1.30 先来详细的讲解一下SSH框架的整合,在这里是将struts2.0的Action的创建工作由Spring进行统一管理,主要是利用了Spring 控制反转和依赖注入的功能. 而将hibernate.cfg.xml整合Spring的配置文件中.而且利用Spring的面向切向功能对Hibernat