选择显示字体大小

struts实现多国语言切换

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=&quot;resources.config.index&quot; />

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

actionmapping
  
<form-beans>
                <!--1 multi-lanuage support formbean-->
        <form-bean
            name=&quot;selectlanguageform&quot;
            type=&quot;com.nova.colimas.web.form.selectlanguageform&quot;/>
   </form-beans>
<!-- =========================================== global forward definitions -->

    <global-forwards>
        <!-- default forward to &quot;welcome&quot; action -->
        <!-- demonstrates using index.jsp to forward -->
        <forward
            name=&quot;index&quot;
            path=&quot;/pages/index.jsp&quot;/>  
    </global-forwards>


<!-- =========================================== action mapping definitions -->

    <action-mappings>      
        <!-- 1 select language action -->          
            <action    path=&quot;/selectlanguageaction&quot;
              type=&quot;com.nova.colimas.web.action.selectlanguageaction&quot;
              name=&quot;selectlanguageform&quot;
              scope=&quot;request&quot;>
            </action>
                &hellip;
    </action-mappings>
      


3 jsp
<%@ page contenttype=&quot;text/html; charset=utf-8&quot;%>
<%@ taglib uri=&quot;/tags/struts-bean&quot; prefix=&quot;bean&quot;%>
<%@ taglib uri=&quot;/tags/struts-html&quot; prefix=&quot;html&quot;%>
<%@ taglib uri=&quot;/tags/struts-logic&quot; prefix=&quot;logic&quot;%>

<html:html>
<title><bean:message key=&quot;index.jsp.welcome&quot;/></title>
<body>
<logic:present name=&quot;user&quot;>
        <h3>welcome <bean:write name=&quot;loginform&quot; property=&quot;userid&quot; />!</h3>
</logic:present>
<logic:notpresent scope=&quot;session&quot; name=&quot;user&quot;>
        <h3>welcome colimas!</h3>
</logic:notpresent>
<html:errors />
<html:form action=&quot;/selectlanguageaction.do&quot;>
       <html:select property=&quot;language&quot;>
                <html:option value=&quot;0&quot;>中文</html:option>
                <html:option value=&quot;1&quot;>日本語</html:option>
                <html:option value=&quot;2&quot;>english</html:option>              
       </html:select>
        <html:submit>select</html:submit>
</html:form>


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

</body>
</html:html>


<bean:message key=&quot;index.jsp.welcome&quot;/>引用资源文件的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(&quot;index&quot;);
        }
}
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=&quot;&quot;;
                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默认语言为中文:
启动画面


切换为日语


切换为英文




 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons