xwdengjie 发表于 2013-2-6 08:47:37

struts2.0 国际化

每种框架都会有国际化的支持,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)方法实现的
<div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <img alt="复制代码" />
页: [1]
查看完整版本: struts2.0 国际化