struts2---资源国际化

来源:互联网 发布:网络语言一股清流 编辑:程序博客网 时间:2024/06/04 18:02

--Struts2---国际化
程序国际化:
程序可以根据计算机所在的国家,地区,来选择对应的语言来输出相关信息。
如输出你好:
在中国,使用中文"你好"来输出
在英国, 使用英文"hello" 来输出。
国际化步骤:
1.为不同的国家,语言环境提供资源包
如果国际化资源包中包含非西欧字符,需要使用native2ascii工具将其转换成unicode编码的字符

国际化资源文件命名规则
mess_zh_CN.properties (zh)
java国际化涉及到的核心类
ResourceBundle
Locale

/*--核心代码---*/

//根据Locale.getDefault获取的国家区域代码来加载对应的该国家区域的资源文件,"mess"为配置文件baseNameResourceBundle bundle = ResourceBundle.getBundle("mess", Locale.getDefault());//获取该资源文件中hi key对应的信息String str = bundle.getString("hi");System.out.println(str);


struts2 的资源国际化
1.支持模块话加载
2. struts2支持自动加载国际化资源


JSP页面的国际化:
1.需要手动加载国际化消息,使用<s:i18n ... />

<%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- 对jsp页面的资源国际化,需要将其放在页面开始部分,这样,整个Html标签体的内容都可以使用国际化资源。s:i18n标签,name指定的是国际化资源的baseName,可以包含国际化资源的目录名如下:resouce是资源存放的目录,login为login_zh_CN.properties或login_en_US.properties的base name--><s:i18n name="resource/login" ><html></html></s:i18n>


2. 在页面上使用<s:text ../>根据name来输出国际化消息,这里的name指定的值即为*.properties属性文件中的key
<title><s:text name="login"/></title>如果是表单标签,直接使用key属性来输出国际化消息,在表单中,struts2会根据key指定的值,从*.properties中获取相应的信息,并在标签中没有指定label属性时,会生成一个label标签。struts2最终会调用getText方法来获取*.properties中的内容。<s:form action="Login"><s:textfield name="username" key="name"/><br /><s:password name="password" key="pass"/><br /><s:submit key="submit"/></s:form>


示例:
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- 对jsp页面的资源国际化,需要将其放在页面开始部分,这样,整个Html标签体的内容都可以使用国际化资源。s:i18n标签,name指定的是国际化资源的baseName,可以包含国际化资源的目录名如下:resouce是资源存放的目录,login为login_zh_CN.properties或login_en_US.properties的base name--><s:i18n name="resource/login" ><html><head> <title><s:text name="login"/></title></head><body><!-- 使用struts2的form标签的好处:1:不用指定method方法,自动会为其添加post的method方法2: form生成的jsp页面自动支持2列对齐样式3:自动回显类型转换失败,标签校验等错误信息内容--><s:form action="Login"><s:textfield name="username" key="name"/><br /><s:password name="password" key="pass"/><br /><s:submit key="submit"/></s:form></body></html></s:i18n>

对应的资源文件放在resource目录下,basename为login


action国际化
1. 自动加载国际化消息,此时action的定义必须继承ActionSupport类
它除了实现了action接口外,还实现了自动校验和资源国际化接口,

可以为每个action配置国际化资源文件,baseName为action名;如LoginAction.java, 其资源国际化base name名为LoginAction

也可以为一个包中所有的action配置国际化资源文件,此国际化资源为这些action所共有,但是国际化资源的base name必须设

成package(注意不是包名,而是package), action类中通过getText()方法来加载国际化信息


示例:
action java代码:

public class LoginAction extends ActionSupport {private String username;private String password;private String tip;public String getTip() {return tip;}public void setTip(String tip) {this.tip = tip;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubsetTip(this.getText("succ", new String[]{this.username})); return SUCCESS;}}

action中通过getText()方法来从*.properties中获取相关内容,其中第一个参数为key,第二个参数是一个数组,

它将填充*.properties中对应key值中的{0},{1}相关内容。此action对应的properites文件名为:
LoginAction_zh_CN.properties
LoginAction_en_US.properties
或者为:
package_zh_CN.properties
package_en_US.properties


回显jsp页面:
${tip}


全局国际化资源文件
1. 所有jsp页面或者action等都能够从全局国际化资源文件中获取信息,使用truts.custom.i18n.resources来加载全局国际化资源文件
它可以在struts.xml中通过指定常量,来指定国际化资源文件,如下:
<constant name="struts.custom.i18n.resources" value="resource/login,resource/error,resource/success" />
它可以指定多个文件,每个文件用","分开
2.全局国际化资源尽量少用,不利于项目的维护和升级


struts2资源国际化高级应用
浏览器的资源国际化,默认是通过浏览器语言设置来选择不同区域的语言的。struts2还提供了一个机制,可以允许用户来选择不同国家的语言环境,

可以增加一个
request_locale参数:
URL: http://localhost:8080/Struts2_002/index.jsp?request_locale=zh_CN
An interceptor that handles setting the locale specified in a session as the locale for the current action request.
In addition, this interceptor will look for a specific HTTP request parameter and set the locale to whatever value is
provided. This means that this interceptor can be used to allow for your application to dynamically change the locale
for the user's session or, alternatively, only for the current request (since XWork 2.1.3). This is very useful for
applications that require multi-lingual support and want the user to be able to set his or her language preference at
any point. The locale parameter is removed during the execution of this interceptor, ensuring that properties aren't
set on an action (such as request_locale) that have no typical corresponding setter in your action.
For example, using the default parameter name, a request to foo.action?request_locale=en_US, then the locale for
US English is saved in the user's session and will be used for all future requests.there is no locale set(for example with 

the first visit), the interceptor uses the browser locale.


Parameters
parameterName (optional) - the name of the HTTP request parameter that dictates the locale to switch to and save in
the session. By default this is request_locale,会存入到session中,key值为WW_TRANS_I18N_LOCALE
requestOnlyParameterName (optional) - the name of the HTTP request parameter that dictates the locale to switch to
for the current request only, without saving it in the session. By default this is request_only_locale,此方式
设置定locale不会存放到session中去


attributeName (optional) - the name of the session key to store the selected locale.By default this is WW_TRANS_I18N_LOCALE
可以在jsp页面上通过${WW_TRANS_I18N_LOCALE }来获取该session存放的locale内容

示例(从页面上通过选择语言来显示整个页面的对应语言的内容)

<%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- 对jsp页面的资源国际化,需要将其放在页面开始部分,这样,整个Html标签体的内容都可以使用国际化资源。s:i18n标签,name指定的是国际化资源的baseName,可以包含国际化资源的目录名如下:resouce是资源存放的目录,login为login_zh_CN.properties或login_en_US.properties的base name--><s:i18n name="resource/login" ><html><head> <title><s:text name="login"/></title></head><body><s:bean var="languages" name="cn.itcast.action.SelectLanuageAction" /><s:form action="SelectLanuage"><s:select id="lanuage" list="#languages.locales" listKey="value" listValue="key" key="SelectLang"name="request_locale"onchange="this.form.submit()"/></s:form><script type="text/javascript">document.getElementById("lanuage").value="${param.request_locale}";</script></body></html></s:i18n>


相关的action
public class SelectLanuageAction extends ActionSupport {private String request_locale;private Map<String, Locale> locales = new HashMap<String, Locale>();{locales.put(Locale.CHINA.getDisplayLanguage(), Locale.CHINA);locales.put(Locale.ENGLISH.getDisplayLanguage(), Locale.ENGLISH);}public String getRequest_locale() {return request_locale;}public void setRequest_locale(String request_locale) {this.request_locale = request_locale;}public Map<String, Locale> getLocales(){return locales;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn SUCCESS;}}


0 0
原创粉丝点击