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; }
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.0中国际化(i18n)您的应用程序 + 本人拓展备注
转:http://www.blogjava.net/max/archive/2006/11/01/78536.html 国际化是商业系统中不可或缺的一部分,所以无论您学习的是什么Web框架,它都是必须 ...
- Struts 2 之资源国际化
首先在struts.properties文件中加入以下内容: struts.custom.i18n.resources=messageResource 或在struts.xml中加入 <con ...
- struts中如何实现国际化,涉及哪些文件?
struts中如何实现国际化,涉及哪些文件? 解答:“国际化”是指一个应用程序在运行时能够根据客户端请求所来自的国家/地区.语言的不同而显示不同的用户界面.Struts框架通过使用<bean:m ...
- 菜鸟学Struts——I18N对国际化的支持
大家肯定都喜欢玩游戏吧. 对于是一个游戏迷的话,肯定玩过不少很棒的经典单机游戏.比方说,国产的<古墓丽影>.<刺客信条>.<鬼泣>国产的仙剑.古剑等.在众多游戏系列 ...
- springboot 使用i18n进行国际化
1.i18n介绍 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬 ...
- I18n问题 国际化
http://www.cnblogs.com/guaniu/archive/2012/01/18/2325556.html java国际化 1.了解缺省Locale是由操作系统决定的,Locale是由 ...
随机推荐
- assertThat用法
一般匹配符1.assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) ); 注释: allOf匹配符表明如果接下来的所有条件必须 ...
- Scala 基础入门【翻译】
原文地址 本文只是带你进入 Scala 的世界,包括安装.不可变量 val.可变量 var.定义类.集合(包括列表(list).集(set).映射(map))以及集合遍历和集合库(能达到并行/并发效果 ...
- jackson 注脚学习参考
(1)初级我们从几个简单的使用场景开始:重命名属性,忽略属性,以及修改属性所使用的类型.注意:下面的例子仅仅显示了成员属性(field properties),注解同样也可以用在成员方法(getter ...
- 最近使用ajaxFileUpload和Jcrop来实现图片上传和截图,出现一个图片无法更换的问题
使用setImage 都无法更换 刷新图片 找了很久 什么方法都找过,最后发现...... 原来是 上传的图片的命名问题... 每次上传的图片 保存后都是同样的图片, 所以返回路径都是一样... jc ...
- Ubuntu server下搭建Maven私服Nexus
Ubuntu server下搭建Maven私服Nexus Maven私服Nexus的作用,主要是为了节省资源,在内部作为maven开发资源共享服务器来使用. 1.下载 通过root用户进去Ubuntu ...
- mysql 分表策略
mysql单表数据量巨大时,查询性能会很差,经常遇到的是存储日志相关的数据会每天产生大量的数据. 这里提供单表拆分成多表存储的三个思路: 一,固定N张表,ID取模存储 预先创建好N张表,记录按ID取模 ...
- android 14.04 64位 adb cannot run program adb
按照网上的说法: Failed to get the adb version: Cannot run program "adb": error=2, 没有那个文件或目录 64位系统 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- sitemesh2在tomcat和weblogic中同时使用的配置问题
(一)拦截*.do,装饰器中匹配do tomcat 可行 weblogic 不可行 web.xml ~~~ <filter> <filter-name>sitemesh< ...
- 【Web】Eclipse + Maven + Struts搭建服务器
一.环境 系统:Windows7 IDE:Eclipse-Kepler Service Release 2 使用插件:Maven(请预先在电脑上安装Maven) 二.搭建 在Eclipse中新建一个M ...