Spring MVC與easyui國際化

來源:互聯網
上載者:User

1.建立資源檔

         在webapp下建立檔案夾language,在其中再添加file,命名分別為language.properties,language_en.properties,language_zh_CN.properties。其中language.properties為預設資源檔。

         在其中新增內容,格式如下:

         language.properties

                   welcome=Welcome

         language_en.properties

                   welcome=Welcome

         language_zh_CN.properties

                   welcome=\u6b22\u8fce

         其中welcome為key,在jsp中調用使用的就是這個,漢語的需要使用unicode編碼(在eclipse的properties檔案中輸入漢語會自動轉換成unicode。\u6b22\u8fce-歡迎)

2.配置springMVC

         在demo-servlet.xml中添加如下

<mvc:interceptors>        <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /></mvc:interceptors><!-- Saves a locale change using a session--><bean id="localeResolver"    class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /><!-- 國際化檔案 --><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource">    <property name="basename" value="/language/language" />    <property name="defaultEncoding" value="UTF-8"/></bean>

         其中<property name="basename" value="/language/language" />value即為資源檔所在,不需要尾碼名.properties。

3.調用國際化3.1 在web中調用國際化

在jsp檔案頭添加

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

在jsp HTML顯示內容處將內容更改,如

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    Welcome</a>

更改為

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    <spring:message code="welcome"/></a>

<input type="text" value="welcome" />

更改為

<input type="text" value='<spring:message code="welcome"/>' />
3.2 在javascript中調用國際化

在jsp檔案頭中添加

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

$.messager.alert('info');

更改為

$.messager.alert('<spring:message code="message.info"/>');

注意:1.<spring>標籤不可以這樣寫"<spring:message code=\"message.info\"/>"這樣會出錯。

    2.在_en檔案中key的值不可以使用\或'符號。

3.3 在java中調用國際化

在controller中使用

if(!model.containsAttribute("contentModel")){    RequestContext requestContext = new RequestContext(request);    String welcome = requestContext.getMessage("welcome");}

其中request為HttpServletRequest,model為Model。

4 解決亂碼4.1 中文輸入亂碼

         完成了上面的步驟之後,會發現XCenter介面中輸入漢語,點擊確定,往資料庫中寫入漢語時,寫入的是亂碼。

解決辦法如下:

         在applicationContext.xml中的

<property name="url" value="${jdbc.url}"/>

更改為 

<property name="url" value="${jdbc.url}?useUnicode=true&amp;characterEncoding=UTF-8"/>

    注意:不要在jdbc.properties中的jdbc.url=jdbc:mysql://localhost:3306/demo之後添加?useUnicode=true&amp;characterEncoding=UTF-8,這樣是不正確的。

4.2 部分介面顯示中文亂碼

         接下來發現在大部分介面因為添加了這句

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

可以正常顯示中文了,但是XCenter部分介面中文顯示為???,經過檢查發現是因為在controller中傳回值為String,而且標記為@ResponseBody,查資料發現是因為Spring MVC中@ResponseBody預設編碼是ISO-8859-1(其他的都是UTF-8,誰知道為什麼同一個架構中這裡要用另外一種編碼)。

         解決辦法:

         在

@RequestMapping(value = "welcome", method = RequestMethod.POST)

中添加produces = "application/json; charset=utf-8"

變成

@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")

         然後會發現在部分的tree中中文仍然顯示為亂碼,發現是因為傳回值轉換成JSON格式的原因。

         解決辦法:

         tree的傳回值格式化將

JSON.toJSONStringWithDateFormat(rs.getDefaultModel(), "yyyy-MM-dd HH:mm:ss");

  更改為

JSON.toJSONString(rs.getDefaultModel());
5 中英文切換5.1 資源檔的中英文切換

         這個比較簡單,使用如下代碼就行

<a href="?locale=en" onclick="changeLanguage('en')">English</a><a href="?locale=zh" onclick="changeLanguage('zh_CN')">簡體中文</a>
5.2 easyui架構的中英文切換

         easyui中有些架構(如datagrid)有些內容是架構內建的,需要在<head>中加入

<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>

那如何切換中英文呢

首先不要在<head>中添加上面這句,然後在主介面<head>中加入如下js語句

<script type="text/javascript">    var language=window.navigator.language;    var userLanguage="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}";    if(null != userLanguage&&userLanguage!=""){//not login        language = userLanguage;    }    $(function(){        var src = 'js/locale' + '/easyui-lang-'+language.replace("-","_")+'.js';// when login in China the language=zh-CN             $.getScript(src);    });</script>

這是使用jquery載入js檔案。

 

接下來將項目中需要替換的內容全部用步驟3中的方法替換就行了,這樣國際化就完成了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.