Struts2入门(10):国际化(i18n)和页面切换语言实例

来源:互联网 发布:mac 系统重装 编辑:程序博客网 时间:2024/06/06 04:49

Struts2 国际化


Struts2 的国际化是基于Java国际化的,相比于JSP本身的国家化,Struts2 对国际化进行了进一步的封装,Struts 对于国际化(i18n)的支持是用过调用相应的预编写的资源包来实现的,主要使用于:UI标签、消息和错误,Action动作等区域;

国际化资源包

Struts2 对于JSP页面的国际化是借助<s:i18n>标签实现的,在 JSP 页面中加载相应的国际化 properties 资源包来实现的,这些资源包放置一系列的国际化语言版本,如对于一个登陆页面 login.jsp 使用以下示例资源包:
login.properties:规定默认的国际化文本的资源包;
login_zh_CN.properties :规定中文国际化文本的资源包
login_en_US.properties :规定英文环境的国际化文本的资源包
在记载这些资源时,会根据客户端的语言环境加载相应的资源包,当语言环境未知时,会加载默认资源包;

这些资源包的内容类似类似如下,一般会以 UTF-8 格式进行编码,同时使用 JDK 自带的 native2ascii 工具进行转换(尤其是携带非英文字符的资源包):
login_zh_CN.properties
使用 native2ascii 工具进行以上 properties 文件的转换时(如果不进行该步骤,会造成非英文文本输出的乱码),在命令行中输入(假如以上文件临时命名为"temp_login_zh_CN.properties"):
1
native2ascii -encoding UTF-8 temp_login_zh_CN.properties login_zh_CN.properties
转换后该文件内容如下:

资源包的放置位置和加载

1)用于JSP页面的资源包
这一系列的资源包,如果是用于JSP页面国际化,一般会放置在一个文件夹中,如“viewResources”,将该文件夹放置在 classes 目录下(如果是IDEA创建的Web应用,可以直接放置在src目录下,项目打包是会自动复制到 classes 目录下), 其加载需要在 stucts.xml 文件中使用如下示例进行加载(假设要加载的资源包位于classes/viewResources/login.properties):
1
<constant name="struts.custom.i18n.resources" value="viewResources.login" />
2)用于Action内部的资源包
如果是用于 Action 内部的(如验校信息的国际化文本等),需要放置在该Action所在的同级目录下,同时命名的格式为"actionName_lang_locale.properties",如 “LoginAction_zh_CN.properties”,在相应的 Action 运行时,会自动加载这些资源包,不用特别声明;

除了以上的方式外,常用的还有包的资源包、全局资源包,但是一般不推荐这2种方式,因为这样会将所有的配置信息包含在同一个文件中,造成该文件过于庞大后期难以维护;

国际化文本的获取

1)在JSP页面中获取
在JSP页面中获取国际化文本可以 通过 <s:property value="getText('some.key')"> 或 <s:text name="some.key">标签,示例如下:
2)在Action中获取
在Action中获取,只要将该Action继承ActionSupport,直接使用 getText("some.key"),即可获取,如下:
1
public class LoginAction extends ActionSupport{
2
    public String excute(){
3
        ....
4
        String tip = getText("successTip");
5
    }
6
}
3)在配置文件中获取
在xml配置文件中获取国际化文本时,可以使用key属性来获取,如在一个验校文件中获取国际化文本:


国际化实例

以下是一个可以自由切换页面显示语言的实例,该登录页面可以自由在中文,英文之间进行切换,同时数据验校验校消息也是对应的语言版本;

 该示例的文件目录结构如下:

 
以下为主要文件说明:
  • login.jsp 为登录页,result.jsp 为登录结果页;
  • LoginAction 为处理登录逻辑的Action,ChangeLocale 为处理 login.jsp 的语言切换的Action;
  • LoginAction-validation.xml 为处理 login.jsp 的数据验校文件;
  • viewResources 目录下为 login,jsp 的资源包文件,src/login目录下的数据包为 LoginAction 的处理过程和数据验校使用的数据包资源;

资源包文件
login.properties,login_en_US.properties : 英文数据包
1
loginPage = Login Page
2
username = username
3
password = password
4
login = login
5
resultPage = Log In Page
login_zh_CN.properties :中文数据包(示例为 native2ascii 处理前的文本,方便示例,项目中为处理后的文本);

LoginAction.properties, LoginAction_en_US.properties : 英文数据包
1
#在Action中使用的国际化消息
2
successTip = Welcome,${username},you has logged in !
3
errorTip = Sorry,${username},you can't log in !
4
5
#在Action验校文件中使用的国际化消息
6
username.required = username is required !
7
username.stringlength = the length of string must between 1 and 15 !
LoginAction_zh_CN.properties:中文数据包(示例为 native2ascii 处理前的文本,方便示例,项目中为处理后的文本)

Action文件
LoginAction
1
package login;
2
//处理登录动作Action
3
public class LoginAction extends ActionSupport{
4
    private String username;
5
    private String password;
6
    
7
    public String execute() throws Exception{
8
        ActionContext actionContext = ActionContext.getContext();
9
        HttpServletRequest request = ServletActionContext.getRequest();
10
        //进行用户名和密码的查询,一般这个过程通过数据库进行,这里为了方便演示,直接匹配一个用户名和密码
11
        if(request.getParameter("username").equals("assad") && request.getParameter("password").equals("123") ){
12
            actionContext.put("tip",getText("successTip"));   //向Stack context中压入登录消息,也可以将其压入ValueStack
13
        }else{
14
            actionContext.put("tip",getText("errorTip"));
15
        }
16
        return "success";
17
    }
18
    //省略get,set方法
19
20
}
ChangeLocale

JSP视图文件
login.jsp
1
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2
<%@ taglib prefix="s" uri="/struts-tags" %>
3
<html>
4
<head><title><s:text name="loginPage" /></title></head>
5
<body>
6
<!--切换语言版本的超链接-->
7
<s:url var="loginCN" action="changeLocale" namespace="/login">
8
    <s:param name="language">cn</s:param>
9
</s:url>
10
<s:url var="loginEN" action="changeLocale" namespace="/login">
11
    <s:param name="language">en</s:param>
12
</s:url>
13
<a href="<s:property value="#loginCN" />">中文</a>
14
<a href="<s:property value="#loginEN" /> ">English</a>
15
16
<!--表单内容-->
17
<form action="login/login" method="post">
18
    <label><s:text name="username" /></label>
19
    <input type="text" name="username"/><br/>
20
    <label><s:text name="password" /></label>
21
    <input type="text" name="password" /><br/>
22
    <input type="submit" value="<s:text name="login" />"/>
23
    <!--输出验校信息-->
24
    <p><s:fielderror fieldName="username" /></p>
25
</form>
26
    
27
</body>
28
</html>
29
result.jsp

数据验校文件
LoginAction-validation.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE validators PUBLIC
3
        "-//Apache Struts//XWork Validator 1.0.3//EN"
4
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd" >
5
<validators>
6
    <field name="username">
7
        <field-validator type="requiredstring">
8
            <param name="trim">true</param>
9
            <message key="username.required" />
10
        </field-validator>
11
        <field-validator type="stringlength">
12
            <param name="trim">true</param>
13
            <param name="minLength">1</param>
14
            <param name="maxLength">15</param>
15
            <message key="username.stringlength" />
16
        </field-validator>
17
    </field>
18
</validators>

配置文件
struts.xml
web.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5
         version="3.1">
6
    <welcome-file-list>
7
        <welcome-file>login/login.jsp</welcome-file>
8
    </welcome-file-list>
9
    <filter>
10
        <filter-name>struts2</filter-name>
11
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
12
    </filter>
13
    <filter-mapping>
14
        <filter-name>struts2</filter-name>
15
        <url-pattern>/*</url-pattern>
16
    </filter-mapping>
17
18
</web-app>