0
点赞
收藏
分享

微信扫一扫

Struts2_配置包范围国际化资源文件


国际化-包范围资源文件:

在一个大型应用中,整个应用有大量的内容需要实现国际化,如果我们把国际化
的内容都放置在全局资源属性文件中,显然会导致资源文件变的过于庞大、臃肿,
不便于维护,这个时候我们可以针对不同模块,使用包范围来组织国际化文件。

方法如下:
在java的包下放置package_language_county.properties资源文件,package为
固定写法,处于该包及子包下的action都可以访问该资源。当查找到key的消息
,系统会先从package资源文件查找,当找不到对应的key时,才会从常量
struts.custom.i18n.resources指定的资源文件中寻找

两个.properties文件

package_en_US.properties

welcome=package:{0},welcom to bdqn{1}

package_zh_CN.properties


welcome=package:{0},欢迎来到北大青鸟{1}


两个struts.xml文件

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

<struts>

<!-- 引用外部struts.xml文件 -->
<include file="struts_native.xml" />
</struts>


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

<struts>
<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输入 -->
<constant name="struts.custom.i18n.resources" value="itcast" />

<package name="besa" namespace="/demo" extends="struts-default">
<action name="manage" class="cn.itcast.action.PersonManageAction" method="execute">
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>


package cn.itcast.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PersonManageAction extends ActionSupport {

@Override
public String execute() throws Exception {
// List<String> s = new ArrayList<String>();
// s.add("小明");
// s.add("你好");
// ActionContext.getContext().put("message", this.getText("welcome", s));
ActionContext.getContext().put("message", this.getText("welcome", new String[] { "小明", "学习" }));
return "message";
}
}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<title>结果</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">


</head>

<body>
${message }

<h1>中国万岁</h1>
</body>
</html>



举报

相关推荐

0 条评论