鍍金池/ 教程/ Java/ Struts2 國際化手動切換版
Spring 事務(wù)管理
Struts2 上傳文件
Hibernate 對象的三種狀態(tài)
Struts2 攔截器
Hibernate與Spring 配合生成表結(jié)構(gòu)
Struts2 內(nèi)部是如何工作的
Spring 容器IOC解析及簡單實(shí)現(xiàn)
Hibernate動態(tài)模型+JRebel實(shí)現(xiàn)動態(tài)創(chuàng)建表
Hibernate 核心接口
Hibernate——Session之save()方法
提高用戶體驗(yàn)之404處理
Struts2 國際化自動檢測瀏覽器語言版
Struts2 國際化手動切換版
Spring jar包詳解
Spring 容器AOP的實(shí)現(xiàn)原理——動態(tài)代理
Struts實(shí)現(xiàn)簡單登錄(附源碼)
Hibernate之SchemaExport+配置文件生成表結(jié)構(gòu)
基于注解的SSH將配置精簡到極致
簡單模擬Hibernate實(shí)現(xiàn)原理

Struts2 國際化手動切換版

國際化(internationalization,i18n)和本地化(localization,l10n)指讓產(chǎn)品(出版物,軟件,硬件等)能夠適應(yīng)非本地環(huán)境,特別是其他的語言和文化。程序在不修改內(nèi)部代碼的情況下,能根據(jù)不同語言及地區(qū)顯示相應(yīng)的界面。

國際化原理:

國際化資源文件:用不同國家的語言描述相同的信息,并放在各自對應(yīng)的.properties屬性文件中,程序根據(jù)運(yùn)行時環(huán)境決定加載哪個文件。 國際化主要通過以下類完成: java.util.Locale:對應(yīng)一個特定的國家/區(qū)域、語言環(huán)境。 java.util.ResourceBundle:用于加載一個資源包。 I18nInterceptor:struts2所提供的國際化攔截器,負(fù)責(zé)處理Locale相關(guān)信息。 國際化流程:程序得到當(dāng)前運(yùn)行環(huán)境的國家/區(qū)域、語言環(huán)境并存放于Locale,ResourceBundle根據(jù)Locale中信息自動搜索對應(yīng)的國際化資源文件并加載。當(dāng)某個Action被執(zhí)行前,I18nInterceptor負(fù)責(zé)檢測Locale相關(guān)信息來尋找對應(yīng)的國際化資源

先來看一下實(shí)例的效果圖:

默認(rèn)中文:

http://wiki.jikexueyuan.com/project/ssh-noob-learning/images/2.1.jpeg" alt="" />

點(diǎn)擊英文,頁面變成英文顯示:

http://wiki.jikexueyuan.com/project/ssh-noob-learning/images/2.2.jpeg" alt="" />

接下來看具體的實(shí)現(xiàn):

LoginAction

package com.lsj.action;  

import java.util.Locale;  
import org.apache.struts2.ServletActionContext;  
import com.opensymphony.xwork2.ActionContext;  
import com.opensymphony.xwork2.ActionSupport;  

public class LoginAction extends ActionSupport {  

    private static final long serialVersionUID = 1L;  

    private String flag;  
    public String getFlag() {  
        return flag;  
    }  
    public void setFlag(String flag) {  
        this.flag = flag;  
    }  

    public String ha() throws Exception {  
        this.flag=ServletActionContext.getRequest().getParameter("flag");  
                Locale l = Locale.getDefault();     
                if(this.flag==null){     
                   l = new Locale("zh", "CN");     
                }else if (this.flag.equals("2")) {     
                   l = new Locale("zh", "CN");     
                } else if (this.flag.equals("1")) {     
                   l = new Locale("en", "US");     
                }      
              ActionContext.getContext().setLocale(l);     
              return "success";     

    }     

}  

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
     <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>  
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</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>  

    <!-- 自動發(fā)布 -->  
    <constant name="struts.devMode" value="true" />  
    <!-- 過濾器 -->  
    <constant name="struts.i18n.encoding" value="GBK"/>  
    <!-- 配置i18n -->  
    <constant name="struts.custom.i18n.resources" value="message"></constant>  

   <!-- i18n控制 -->  
   <package name="i18n" namespace="/"  extends="struts-default">  
        <action name="i18n" class="com.lsj.action.LoginAction" method="ha">  
            <result name="success">  
               /i18nlogin.jsp  
            </result>  
            <result name="input">  
               /i18nlogin.jsp  
            </result>  
        </action>  
    </package>      

</struts>  

login.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<%@ 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+"/";  
%>  

<html>  
  <head>  
    <base href="<%=basePath%>">      
    <title>My JSP 'i18nlogin.jsp' starting page</title>      
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">   
  </head>  

  <body>  
  <s:i18n name="local.message">  
    <s:form action="/i18n.action" method="post">  
      <s:textfield name="user.userName" label="%{getText('login.name')}"/>  
      <s:password name="user.userPassword" label="%{getText('login.pass')}"/>  
      <s:submit value="%{getText('login.submit')}"></s:submit>  
       <a href="i18n.action?flag=1">英文</a>  
       <a href="i18n.action?flag=2">中文</a>  
    </s:form></s:i18n>  
  </body>  
</html>  

需要引入的jar包:

http://wiki.jikexueyuan.com/project/ssh-noob-learning/images/2.3.jpeg" alt="" />

OK到這里就實(shí)現(xiàn)了一個簡單的國際化的小實(shí)例,有的說弄國際化沒多大必要,因?yàn)樵蹅冏龅臇|西幾乎都只是用于國內(nèi)。我想說的是:為什么就覺得我們做的東西只會給我們自己用?為什么就不會覺得隨著我們不斷的努力,我們做的東西會越來越好,然后我們的東西會逐漸打入國際市場,讓老外用我們做的東西呢?你們有信心嗎?