准备资源文件,资源文件的命名格式如下:
baseName_language_country.properties
baseName_language.properties
baseName.properties
其中baseName是资源文件的基本名,我们可以自定义,但language和country必须是java支持的语言和国家。如:
中国大陆: baseName_zh_CN.properties
美国: baseName_en_US.properties
现在为应用添加两个资源文件:
第一个存放中文:csdn_zh_CN.properties
内容为:welcome=欢迎来到传智播客 (myEclipse会自动将中文转换为unicode编码)
第二个存放英语(美国): csdn_en_US.properties
内容为: welcome=welcome to itcast
当准备好资源文件之后,我们可以在struts.xml中通过struts.custom.i18n.resources常量把资源文件定义为全局资源文件,如下:
<constant name="struts.custom.i18n.resources" value=“csdn" />
csdn为资源文件的基本名。
在页面中访问:
想要使用<s:text name=“”/>标签在JSP页面中输出国际化信息,首先要进行如下配置:
<%@ taglib uri="/struts-tags" prefix="s" %>
在body中输出:
<s:text name="welcome"/>
注:name为资源文件中的key
在action中访问:
在Action类中,可以继承ActionSupport,使用getText()方法得到国际化信息,该方法的第一个参数用于指定资源文件中的key。
在struts.xml 文件中配置
<package name="itcast" namespace="/test" extends="struts-default">
<action name="manage" class="cn.csdn.action.ManageAction">
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
Action中的方法:
public class ManageAction extends ActionSupport{
@Override
public String execute() throws Exception {
ActionContext.getContext().put("message", this.getText("welcome")) ;
return "message";
}
}
复习struts2之配置国际化全局资源文件、输出国际化信息
最新推荐文章于 2019-09-10 19:06:44 发布