學習 學習 再學習

          學習才是唯一的出路

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            31 隨筆 :: 1 文章 :: 8 評論 :: 0 Trackbacks

          摘要:
          通過下拉框里各個語言(中文,日本語,English)的選擇,切換jsp頁面文字。
          tyrone1979 發表于 2005-08-26 13:27:19
          作者:tyrone1979     來源:blog.csdn.net/tyrone1979
          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碼??梢允褂胣ative2ascii.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" divfix="bean"%>
          <%@ taglib uri="/tags/struts-html" divfix="html"%>
          <%@ taglib uri="/tags/struts-logic" divfix="logic"%>

          <html:html>
          <Title><bean:message key="index.jsp.welcome"/></Title>
          <body>
          <logic:divsent name="user">
                  <H3>Welcome <bean:write name="LoginForm" property="userID" />!</H3>
          </logic:divsent>
          <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默認語言為中文:



          J-CN工作室
          www.j-cn.org
          posted on 2009-11-01 12:37 鐵猴 閱讀(349) 評論(0)  編輯  收藏 所屬分類: JAVA隨記
          主站蜘蛛池模板: 滨海县| 大关县| 罗山县| 长岭县| 宣武区| 社会| 枞阳县| 驻马店市| 龙山县| 阿鲁科尔沁旗| 谢通门县| 乌鲁木齐县| 措勤县| 沙洋县| 鸡东县| 上犹县| 巴马| 上杭县| 和田市| 乐平市| 正宁县| 多伦县| 淄博市| 宜城市| 类乌齐县| 垦利县| 中山市| 始兴县| 永登县| 乌鲁木齐县| 长春市| 遂川县| 麻城市| 内黄县| 饶河县| 伊金霍洛旗| 栾城县| 玉门市| 秭归县| 顺昌县| 米脂县|