网站如果需要跨国使用的话,最大的问题莫过于语言的交流。那么怎么建立一个较好网站满足多个国家的需求呢?在Struts1框架就定义一个比较完整的国际化实现。首先struts1支持静态的国际化(即:根据你使用的浏览器的默认语言或者系统语言)和自动切换的国际化(根据你的选择语言的不同)
我们先来了解下怎么实现一个静态的国际化,首先我们来看看struts-config.xml配置信息,这个配置信息里面其他的都可以不看,主要看这个配置信息,其中parameter对应的属性值是国际化文件的Basename,其定义国籍化配置文件需要用到。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- 配置所有的formbean,每个formbean都可以用一个<form-bean>类指定-->
<form-beans>
<form-bean name="UserBean" type="com.xingyao.model.UserBean"></form-bean>
</form-beans>
<!-- 配置具体接受action请求的信息,每一个action请求都可以用一个<action>来指定 -->
<action-mappings>
<!-- 配置action信息,其中
path表示用户form请求路径;
type表示服务器组件action的类;
name表示formBean组件;
scope表示将FormBean封装到那个域中
validate表示是否要验证该formBean
-->
<action
path="/user/test"
type="com.xingyao.action.LoginAction"
name="UserBean"
scope="request"
parameter="command"
>
<!-- 具体请求的时候,得到返回结果,需要跳转(转发)的页面 -->
<forward name="success" path="/WEB-INF/JSP/success.jsp"></forward>
<forward name="error" path="/WEB-INF/JSP/error.jsp"></forward>
</action>
</action-mappings>
<!-- 配置国际化组件 -->
<message-resources parameter="MessageResources"></message-resources>
</struts-config>
现在我们来看看,怎么配置一个国际化文件,首先需要明白国际化文件就是一个properties文件,命名规则就是BaseName_local.properties,如中文的国际化资源文件:MessageResources_zh_CN.properties,但是需要注意的是,struts1的国际化不能直接支持中文,需要将中文转换为二进制,然后才能正确的进行国际化转换(幸好MyEclipse的高版本编写properties文件是会自动转换,如果不能自动转换请自动百度),现在我们来看看这些资源文件
第一个截图为中文的国际化文件,第二个文件为英文版的国际化文件
好了,我们来看看jsp页面怎么配置,请注意,使用国际化配置,需要导入一个<%@ taglib prefix=”bean” uri=”http://struts.apache.org/tags-bean” %>标签,使用方式就是,具体的显示页面请看下图
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%
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%>">
</head>
<body>
<h1><bean:message key="user.title"/></h1>
<hr>
<form action="user/test.do?command=add">
<bean:message key="user.username"/> <input type="text" name="username"><br>
<bean:message key="user.password"/> <input type="password" name="passwrod"><br>
<input type="submit" value='<bean:message key="user.button.login"/>'>
</form>
</body>
</html>
这样就能实现页面国际化的显示了,我们知道了怎么配置一个国际化的网站,但是具体里面的实现我们还不懂,现在我们来讲讲源码的实现
首先我们还是看看process这个方法,因为action功能的主要功能实现都在这个方法里面,我们主要关注的是 processLocale(request, response);这个方法,这方法就是和国际化功能相关
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String path = processPath(request, response);
processLocale(request, response);------------------------------------------------具体请看详细讲解1
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
}
}
————————————————————详细讲解1———————————————————–
这个方法并没有做太多的事情,
1、先通过查询session域里面是否含有Globals.LOCAL_KEY的属性值,如果有则退出
2、如果没有则通过request域里面得到Locale对象,然后往session里面设置key为Globals.LOCALE_KEY,值为Locale对象的引用
protected void processLocale(HttpServletRequest request,
HttpServletResponse response) {
// Has a Locale already been selected?
HttpSession session = request.getSession();
if (session.getAttribute(Globals.LOCALE_KEY) != null) {
return;
}
// Use the Locale returned by the servlet container (if any)
Locale locale = request.getLocale();
if (locale != null) {
if (log.isDebugEnabled()) {
log.debug(" Setting user locale '" + locale + "'");
}
session.setAttribute(Globals.LOCALE_KEY, locale);
}
}
到这一步我们就完成了怎么配置一个国际化程序,但是我们只是配置了!在jsp的这个标签又是怎么实现通过国际化文件显示页面信息的呢?我们来看看这个简单的实现
public class MessageTag extends TagSupport {
protected String key = null;
public String getKey() {
return (this.key);
}
public void setKey(String key) {
this.key = key;
}
//通过这个key得到session厘面的local信息
protected String localeKey = Globals.LOCALE_KEY;
public String getLocale() {
return (this.localeKey);
}
public void setLocale(String localeKey) {
this.localeKey = localeKey;
}
//这个是得到MessageResources资源,它封装在一个LocalStrings.properties文件里面
protected static MessageResources messages =
MessageResources.getMessageResources(
"org.apache.struts.taglib.bean.LocalStrings");
//这一步是通过你配置信息<bean:message key="user.title"/>的key,得到里面的属性值:user.title,并赋值为key
public int doStartTag() throws JspException {
String key = this.key;
if (key == null) {
Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
if (value != null && !(value instanceof String)) {
JspException e =
new JspException(messages.getMessage("message.property", key));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
key = (String) value;
}
通过你得到的key:user.title的信息,查询你配置的国际化信息,得到其对应的属性值,在本例中为
String message =
TagUtils.getInstance().message(
pageContext,
this.bundle,
this.localeKey,
key,
args);
//将你得到的信息封装到pageContext域中显示给界面
TagUtils.getInstance().write(pageContext, message);----------------------------看详细介绍2
return (SKIP_BODY);
}
}
————————————————————–详细介绍2———————————————————
//在这里我们可以看出,系统得到page域的输出对象,然后将message信息写到界面上去
public void write(PageContext pageContext, String text)
throws JspException {
JspWriter writer = pageContext.getOut();
try {
writer.print(text);
} catch (IOException e) {
TagUtils.getInstance().saveException(pageContext, e);
throw new JspException
(messages.getMessage("write.io", e.toString()));
}
}
在这里我们讲完了怎么配置一个国际化网站和系统怎么执行一个的标签,从而得到国际化信息,但是这个有问题就是,国际化的信息都是固定不变的,怎么实现一个“XXX用户不存在”这个信息的国际化呢,这里不做介绍,具体的请看源码三
转载请标明出处
感谢苏波老师的指导
个人见解、错误请指出!请莫怪