struts2 i18n

国际化资源文件:放在各自对应的.properties属性文件中,可以根据运行时环境决定加载那个文件。


注意:
(1)i18n拦截器对 “.action”之外的请求失败,因为struts2中的拦截器只能拦截“.action”结尾的请求
(2)struts2没有提供中国资源文件,需要自己添加全局国际化资源文件


资源文件的命名可以有以下三种形式:
(1)baseName_language_country.properties
(2)baseName_language.properties
(3)baseName.properties

国际化资源文件分为三种级别
(1)Action级资源文件,自被当前Ation引用(级别最大)。文件名格式为:XxxAction_语言代码_国际代码.。文件放在与action同一目录下
(2)package级资源文件,每个包的跟目录下可以新建资源文件,仅被当前包中的类访问。文件名格式为:package_语言代码_国际代码.

(3)全局级资源文件,可以被整个工程引用。引用时在struts.xml中加入<constant name="struts.custom.i18n.resources" value="资源文件的基本名"/>

    资源文件放在src目录下





全局资源例子:

语言Action
public class LangAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

}

资源文件
test_en_US.properties

login.username=username:
login.password=password:
login.login=login
welcome.msg=welcome\:{0}



test_zh_CN.properties

login.username=\u7528\u6237\u540D\uFF1A
login.password=\u5BC6  \u7801\uFF1A
login.login=\u767B\u5F55
welcome.msg=\u6B22\u8FCE\u4F60\uFF1A{0}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

	<constant name="struts.custom.i18n.resources" value="test"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<action name="lang" class="com.lzb.action.LangAction">
			<result>/login.jsp</result>
		</action>
	</package>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
   <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>


login.jsp

<form action="" method="post">
  	<s:property value="getText('login.username')"/> <input name="username" /><br/>
  	<s:property value="getText('login.password')"/><input name="password" type="password" />
  	<input type="submit" value="<s:property value="getText('login.login')"/>" /> 
  </form>
  <s:debug></s:debug>
  <a href="lang?request_locale=en_US">en</a>
  <a href="lang?request_locale=zh_CN">cn</a>


 
 
 
 

 
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值