struts框架实现国际化(实现登录页面中英互换)
新建一个Web工程,并且搭好框架
环境要求:Struts2框架环境搭建成功
上次已经介绍了Struts2框架的搭建,在这里就不在介绍了。下面是实例化的一个例子,有需要的可以参考一下。
首先,先写jsp页面,在这个jsp上测试中英文切换效果,代码如下:
登录页面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
<title>登录页面</title>
</head>
<body>
<!-- 局部定义使用哪一种国际化语音 -->
<s:i18n name="messages">
<s:text name ="check" ></s:text>
<a href="check.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>
<a href="check.action?request_locale=en_US"><s:text name="english"></s:text></a>
<br/>
<form action="<%=basePath%>hellow.action" method="post">
<table>
<tr>
<td><s:text name="login.username"/></td>
<td><input type="text" name="user.userName"/></td>
</tr>
<tr>
<td><s:text name="login.password"/></td>
<td><input type="text" name="user.password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="<s:text name="login.btn"/>"/></td>
</tr>
</table>
</form>
</s:i18n>
</body>
</html>
欢迎页面:welcome.jsp
<%@ 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>欢迎页面</title>
</head>
<body>
欢迎你!
</body>
</html>
为了实现程序的国际化,必须先提供程序所需要的资源文件。资源文件的内容基本是key-value对,其中key是程序使用的部分,而value是程序的显示部分。
资源文件的命名可以是如下3种形式:
baseName_language_country.properties
baseName_language.properties
baseName.properties
其中baseName是资源文件的基本名称,用户可*定义,而language和country是不变的,必须是Java所支持的语言和国家。(用到哪个国家语言资源可查官方文档)
Java不可能支持所有的国家和语言,可以通过Locale类的getAvailableLocale方法获取支持的,该方法返回一个Locale数组,该数组中包含了所有支持的国家和语言。
创建资源文件:
资源文件在src下创建,右击src新建一个File文件,文件名写下面的messages_zh_CN.properties和messages_en_US.properties就可以了。
下面演示了如何去建这个资源文件:
messages_zh_CN.properties(中文)
messages_en_US.properties(英文)
编写action:
ChangeLanguageAction.java
package com.hnpi.blue;
public class ChangeLanguageAction {
public String execute(){
return "success";
}
}
RegisterAction.java
package com.hnpi.blue;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private String username;
private String password;
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;
}
public String t1(){
return "success";
}
}
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在struts.xml中配置拦截器和国际化的资源:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name ="struts.costom.i18n.resources" value="message"></constant>
<package name="v1" namespace="/" extends="struts-default">
<action name="check" class="com.hnpi.blue.ChangeLanguageAction">
<result name="success">/index.jsp</result>
</action>
<action name="hellow" class="com.hnpi.blue.RegisterAction" method="execute">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>