Struts 2的国际化(I18N)和本地化(i10n)或多语言的例子,来说明如何使用资源包来显示不同语言的消息。在这个例子中,您将创建一个简单的登录屏幕,通过Struts 2的UI组件显示来自资源包的消息, 并更改基于所选的语言选项的语言环境。
项目架构
资源束
Struts2使用资源束为Web应用程序的用户提供多种语言和区域的设置选项。你不必担心需要用不同的语言编写页面,你需要做的只是为每种你想要的语言创建一个资源束。资源束将包含用户语言中的标题,消息和其他文本,是包含应用程序默认语言的一对key/value的文件。资源文件最简单的命名格式是:
bundlename_language_country.properties
这里的
bundlename
可以是ActionClass,Interface,SuperClass,Model,Package,Global资源属性。
访问信息
有几种方法来访问信息资源,包括getText,text标签,UI标签的key属性和i18n标签。接下来让我们简要了解一下:
显示i18n文本,需在property标签或其他任何标签(如UI标签)中调用getText,如下所示:
<s:property value="getText('some.key')" />
text标签从默认资源束(即struts.properties)中检索信息:
<s:text name="some.key" />
i18n标签会将任意资源束推送到值栈,而i18n标签内的其他标签可以显示来自该资源束的信息:
<s:i18n name="some.package.bundle">
<s:text name="some.key" />
</s:i18n>
大多数UI标签的key属性可用于从资源束检索信息:
<s:textfield key="some.key" name="textfieldName"/>
本地化示例
现在,让我们用多种语言创建前一章中的login.jsp文件,如下所示:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Employee Form with Multilingual Support</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><s:text name="global.heading"/></h1>
<s:url var="indexEN" action="locale" namespace="/">
<s:param name="request_locale">en</s:param>
</s:url>
<s:url var="indexZH" action="locale" namespace="/">
<s:param name="request_locale">zh</s:param>
</s:url>
<s:url var="indexFR" action="locale" namespace="/">
<s:param name="request_locale">fr</s:param>
</s:url>
<s:a href="%{indexEN}">English</s:a>
<s:a href="%{indexZH}">中文</s:a>
<s:a href="%{indexFR}">France</s:a>
<s:form action="empinfo" method="POST" namespace="/">
<s:textarea name="name" key="global.name"/>
<s:textfield name="age" key="global.age"/>
<s:submit name="submit" key="global.submit"/>
</s:form>
</body>
</html>
现在创建success.jsp文件,这个文件在action返回SUCCESS结果的情况下会被调用。
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Success</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>
<s:property value="getText('global.success')"/>
</h1>
</body>
</html>
这里我们需要创建以下两个action。(a)第一个action处理区域设置并显示使用不同语言的同一个login.jsp文件;(b)另一个action是负责提交表单本身。这两个action都将返回SUCCESS,但我们将根据返回值采取不同的action,因为这两个action的目的是不同的:
区域设置Action
package com.ray.common.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by Ray on 2018/3/25 0025.
* 第一个action处理区域设置并显示使用不同语言的同一个login.jsp文件
**/
public class Locale extends ActionSupport {
@Override
public String execute() throws Exception {
return super.execute();
}
}
提交表单Action
package com.ray.common.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by Ray on 2018/3/25 0025.
* 第二个action是负责提交表单本身
**/
public class Employee extends ActionSupport {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
return super.execute();
}
}
现在,让我们创建以下三个global.properties文件
global_en.properties
global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success =Successfully authenticated
global_fr.properties
global.name = Nom d'utilisateur
global.age = l'âge
global.submit = Soumettre des
global.heading = Sé lectionnez Local
global.success =Authentifi\té avec succès
global_zh.properties
global.name = 姓名
global.age = 年龄
global.submit = 提交
global.heading = 选择一种语言
global.success = 成功
我们接着创建包含两个action的
struts.xml
文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.custom.i18n.resources" value="global"/>
<package name="helloworld" extends="struts-default" namespace="/">
<action name="empinfo" class="com.ray.common.action.Employee" method="execute">
<result name="input">/login.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="locale" class="com.ray.common.action.Locale" method="execute">
<result name="success">/login.jsp</result>
</action>
</package>
</struts>
以下是
web.xml
文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
现在启动Tomcat服务器并尝试访问URL:
http://localhost:8080/Struts2Demo/locale.action
ok!