到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/43/43663_Struts.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
Struts实现多国语言切换
作者:tyrone1979 来源:www.matrix.org.cn  发布时间:2006-02-22 17:51:34.453

1 准备资源文件。

资源文件命名格式:filename_language_country.properties.
中文文件名为index_zh_CN.properties。
日文文件名为 index_ja_JP.properties。
英文文件名为 index_en.properties。

英文文件内容:

index.jsp.welcome=Colimas Library Management System
index.jsp.name=Name
index.jsp.userid=User ID
index.jsp.pass=Password


中文文件内容:
index.jsp.welcome=\u4f60\u597d
index.jsp.name=\u59d3\u540d
index.jsp.userid=\u7528\u6237\u540d
index.jsp.pass=\u5bc6\u7801


日文文件内容:
index.jsp.welcome=\u3044\u3089\u3063\u3057\u3083\u3044\u307e\u305b
index.jsp.name=\u59d3\u540d
index.jsp.userid=\u30e6\u30fc\u30b6\u30fcID
index.jsp.pass=\u30d1\u30b9\u30ef\u30fc\u30c9

\uxxxx是中文被转换后的ASCII码。可以使用native2ascii.exe工具转换。

2 struts-config.xml里配置资源文件
    
<message-resources parameter="resources.config.index" />

resources.config.index是classes目录下的resources/config子目录的index__xx_xx.properties文件.
struts根据你的浏览器的语言设置调用不同语言的资源文件。
例如:如果你的IE默认语言为中文则。Struts将使用index_zh_CN.properties。而在struts-config.xml里只需写出“index”即可

ActionMapping
  
<form-beans>
                <!--1 Multi-Lanuage support formbean-->
        <form-bean
            name="SelectLanguageForm"
            type="com.nova.colimas.web.form.SelectLanguageForm"/>
   </form-beans>
<!-- =========================================== Global Forward Definitions -->

    <global-forwards>
        <!-- Default forward to "Welcome" action -->
        <!-- Demonstrates using index.jsp to forward -->
        <forward
            name="index"
            path="/pages/index.jsp"/>  
    </global-forwards>


<!-- =========================================== Action Mapping Definitions -->

    <action-mappings>      
        <!-- 1 select language action -->          
            <action    path="/SelectLanguageAction"
              type="com.nova.colimas.web.action.SelectLanguageAction"
              name="SelectLanguageForm"
              scope="request">
            </action>
                …
    </action-mappings>
      


3 jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>

<html:html>
<Title><bean:message key="index.jsp.welcome"/></Title>
<body>
<logic:present name="user">
        <H3>Welcome <bean:write name="LoginForm" property="userID" />!</H3>
</logic:present>
<logic:notPresent scope="session" name="user">
        <H3>Welcome Colimas!</H3>
</logic:notPresent>
<html:errors />
<html:form action="/SelectLanguageAction.do">
       <html:select property="language">
                <html:option value="0">中文</html:option>
                <html:option value="1">日本語</html:option>
                <html:option value="2">English</html:option>              
       </html:select>
        <html:submit>Select</html:submit>
</html:form>


<html:form action="/LoginAction.do">
        <p><bean:message key="index.jsp.userid"/><input type="text" name="userID" value="tyrone1979" /><br>
        <bean:message key="index.jsp.pass"/><input type="password" name="password" value="197913"/><br>
        <html:submit><bean:message key="index.jsp.login"/></html:submit>
        </p>
</html:form>

</body>
</html:html>


<bean:message key="index.jsp.welcome"/>引用资源文件的index.jsp.welcome属性
SelectLanguageAction.do调用Action实现语言转换。

4 Action
package com.nova.colimas.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
//import org.apache.struts.upload.FormFile;
import com.nova.colimas.web.form.SelectLanguageForm;
import org.apache.struts.Globals;
import java.util.Locale;

public class SelectLanguageAction extends Action {
        public ActionForward execute(ActionMapping mapping,
                         ActionForm form,
                         HttpServletRequest request,
                         HttpServletResponse response)
        throws Exception{
                SelectLanguageForm myform=(SelectLanguageForm)form;
                String lan=myform.getLanguage();
                switch((new Integer(lan)).intValue()){
                case 0 :
                        request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
                        break;
                case 1:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.JAPAN);
                        break;
                case 2:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
                        break;
                default:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
                        break;
                }
                        return mapping.findForward("index");
        }
}
Form
/*
* Created on 2005/06/18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.nova.colimas.web.form;

import org.apache.struts.action.ActionForm;

/**
* @author tyrone
**/
public class SelectLanguageForm extends ActionForm {

        private String language;

        public void Reset() {
                this.language="";
                return;
        }

        /**
         * @return Returns the Language.
         */
        public String getLanguage() {
                return language;
        }
        /**
         * @param language The Language to set.
         */
        public void setLanguage(String property1) {
                this.language = property1;
        }
}


结果
1 IE默认语言为中文:
启动画面
image

切换为日语
image

切换为英文
image


 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读