基於Session的國際化實現,Session國際化實現

來源:互聯網
上載者:User

基於Session的國際化實現,Session國際化實現

其他連結:http://blog.csdn.net/jinwufeiyang/article/details/52338268

如何將我們網站的其它內容(如菜單、標題等)做國際化處理呢?這就是本篇要將的內容—>國際化。

在項目的spring.xml檔案添加的內容如下

 

[html] view plain copy 
  1. <mvc:interceptors>    
  2. <span style="white-space:pre">    </span><!-- 國際化操作攔截器 如果採用基於(請求/Session/Cookie)則必需配置 -->   
  3.     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />    
  4. </mvc:interceptors>  

 

在項目中的源檔案夾resources中添加myproperties.properties、myproperties_zh_.properties、myproperties_en_.properties三個檔案

 

下面是jsp頁面的一些簡單資訊如下,僅僅是示範沒考慮其他的:

 

[html] view plain copy 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4.   
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <%  
  11.     Locale name = (Locale) session.getAttribute("i18nlanguage");  
  12.     ResourceBundle myResourcesBundle = ResourceBundle.getBundle("myproperties",name);  
  13. %>  
  14. <body>  
  15.     <a href="${pageContext.request.contextPath}/index/findex.do?langType=en&page=Home">ENG</a> |  
  16.  <a href="${pageContext.request.contextPath}/index/findex.do?langType=zh&page=Home"><%=myResourcesBundle.getString("simplified")%></a>  
  17. </body>  
  18. </html>  


後台Action層代碼如下:

 

 

[html] view plain copy  print?
  1. package com.zhidao.oms.index;  
  2.   
  3. import java.util.Locale;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RequestParam;  
  10.   
  11. @Controller  
  12. @RequestMapping("/index")  
  13. public class IndexAction {  
  14.       
  15.       
  16.     @RequestMapping("/findex")  
  17.     public String Findex(HttpServletRequest request,@RequestParam String langType,String page){  
  18.   
  19.     if(langType.equals("zh")){  
  20.             Locale locale = new Locale("zh", "CN");   
  21.             request.getSession().setAttribute("i18nlanguage",locale);   
  22.         }  
  23.         else if(langType.equals("en")){  
  24.             Locale locale = new Locale("en", "US");   
  25.             request.getSession().setAttribute("i18nlanguage",locale);  
  26.         }else{  
  27.             request.getSession().setAttribute("i18nlanguage",Locale.getDefault());  
  28.         }  
  29.         return "/front/"+page+".jsp";  
  30.     }  
  31.       
  32. }  


有關的展示大家測試一下就好了!寫的不好的地方希望大家批評指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.