struts2配置国际化全局资源文件 并 输出国际化信息

来源:互联网 发布:mysql root空密码登录 编辑:程序博客网 时间:2024/05/19 12:38

第一步:

在src目录下创建国际化信息文件,命名规则为:Name_zh_CN.properties (中国) ,Name_en_US.properties(美国),等等

其中Name:随意名字,中间红色区域,要根据不同的国家来写,中国就是zh_CN,美国就是en_US

第二步:

在Name_zh_CN.properties创建一个属性:welcome="欢迎来到中国"

在Name_en_US.properties创建一个属性:welcome="welcome to Americal"

第三步:

在struts,xml配置全局资源

<constant name="struts.custom.i18n.resources" value="itcast"/>

如果想在jsp中输出,那么加入标签:

<%@ taglib uri="/struts-tags" prefix="s"%>

使用:

<s:text name="welcome" /> 

来输出welcome的值

第四步:

如果想在action中得到welcome的值,那么让该Action继承ActionSupport这个类,然后使用this.getText("welcome");得到welcome的值

例如:

public class HelloWorldAction extends ActionSupport{public String execute(){ActionContext.getContext().put("message", this.getText("welcome"));return "success";}}
注意:浏览器默认语言为中文,所以得到的值是:欢饮来到中国。如果你们想让浏览器使用英文,那么在工---》internet选项--》语言--》选择美国

            那么,welcome的值就为:welcome to Americal



0 0
原创粉丝点击