Spring MVC(十六)--Spring MVC國際化實例

上一篇文章總結了一下Spring MVC中實現國際化所需的配置,本文繼上一文舉一個完整的例子,我選擇用XML的方式。個人場景是這樣的:javascript

訪問一個頁面時,這個頁面有個表格,對錶頭中的列名實現國際化。css

第一步:完成國際化配置html

主要是配置用來定位資源文件的消息源和用來解析國際化的國際化解析器,都要配置在Spring的配置文件中java

 1 <!--Spring MVC實現國際化 -->
 2     <!-- 第一步:配置消息源MessageSource用來加載對應的國際化屬性文件:bean的ID名稱只能是messageSource -->
 3     <!--第一種方式:配置ResourceBundleMessageSource:這種方式只能把資源文件放到指定的路徑下,不能熱加載,須要重啓系統才能加載它 -->
 4     <!-- <bean id="messageResource"
 5         class="org.springframework.context.support.ResourceBundleMessageSource">
 6         <property name="defaultEncoding" value="UTF-8" />
 7         <property name="basename" value="msg" />
 8     </bean> -->
 9 
10     <!--第二種方式:配置ReloadableResourceBundleMessageSource:這種方式只能把資源文件放到任何位置,不須要重啓就能加載文件,而且能夠設置刷新時間 -->
11     <bean id="messageSource"
12         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
13         <!-- 默認編碼方式 -->
14         <property name="defaultEncoding" value="UTF-8" />
15         <!-- 資源文件的前綴:多了一個classpath,若是資源文件在一個文件夾下,還要加上文件夾路徑,不然報錯 -->
16         <property name="basename" value="classpath:message/msg" />
17         <!-- 刷新時間 -->
18         <property name="cacheSeconds" value="3600" />
19     </bean>
20 
21     <!-- 第二步:配置國際化解析器,有cookie和session兩種方式,通常用session比較保險 -->
22     <!--第一種:配置cookie國際化解析器 -->
23     <!-- <bean id="localeResolver"
24         class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
25         cookie變量的名稱
26         <property name="cookieName" value="lang" />
27         cookie超時時間
28         <property name="cookieMaxAge" value="20" />
29         默認使用簡體中文
30         <property name="defaultLocale" value="zh_CN" />
31     </bean> -->
32 
33     <!--第一種:配置session國際化解析器 -->
34     <bean id="localResolver"
35         class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
36         由於session有其自身定義的超時時間和編碼,因此此處不須要設置
37         <property name="defaultLocale" value="zh_CN" />
38     </bean>

上面代碼中,我配置了兩種消息源和兩種國際化解析器,其中註釋掉的是不會熱加載的消息源和cookie國際化解析器,由於它們都有一些缺陷,上一篇中已經介紹過。jquery

第二步:配置攔截器web

攔截器是在Spring MVC的配置文件中配置:spring

1 <!-- 配置國際化攔截器 -->
2     <mvc:interceptors>
3         <mvc:interceptor>
4             <mvc:mapping path="/**" />
5             <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
6                 <property name="paramName" value="language" />
7             </bean>
8         </mvc:interceptor>
9     </mvc:interceptors>

 第三步:建立資源文件瀏覽器

我將這兩個文件都放在src/main/resource下的message文件夾下,因此上面配置消息源時basename的值爲classpath:message/msg,這一點須要注意。cookie

1⃣️中文資源文件msg_zh_CN.propertiessession

1 welcome=\u4F60\u597D \u4E16\u754C\uFF01
2 
3 param_param_id=\u53C2\u6570ID
4 param_param_name=\u53C2\u6570\u540D\u79F0
5 param_param_desc=\u53C2\u6570\u63CF\u8FF0
6 param_param_time=\u5165\u5E93\u65F6\u95F4

 

中文使用的是unicode編碼,這個不須要管,顯示時不會亂碼。

2⃣️英文資源文件msg_en_US.properties

1 welcome=hello world!
2 param_param_id=Param ID
3 param_param_name=Param Name
4 param_param_desc=Param Describer
5 param_param_time=Save Time

 第四步:建立頁面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 4 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 5 <%@ taglib prefix="mvc" uri="http://www.springframework.org/tags/form"%>
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <%
11     String root = request.getContextPath();
12     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
13             + root + "/";
14 %>
15 <script type="text/javascript"
16     src="<%=basePath%>jslib/jquery-1.8.3.min.js"></script>
17 <script type="text/javascript" src="<%=basePath%>jslib/jquery.form.js"></script>
18 <script type="text/javascript" src="<%=basePath%>js/param.js"></script>
19 <link href="<%=basePath%>css/param.css" type="text/css" rel="stylesheet">
20 <title>參數列表</title>
21 </head>
22 <body>
23     <div>
24         <table>
25             <thead>
26                 <tr>
27                     <td><spring:message code="param_param_id" /></td>
28                     <td><spring:message code="param_param_name" /></td>
29                     <td><spring:message code="param_param_desc" /></td>
30                     <td><spring:message code="param_param_time" /></td>
31                 </tr>
32             </thead>
33             <tbody>
34                 <c:forEach var="paramItem" items="${paramList }">
35                     <tr>
36                         <td>${paramItem.paramId}</td>
37                         <td>${paramItem.paramName }</td>
38                         <td>${paramItem.paramDesc }</td>
39                         <td>${paramItem.pramTime }</td>
40                     </tr>
41                 </c:forEach>
42             </tbody>
43         </table>
44     </div>
45 </body>
46 </html>

 上面代碼中加粗的紅色部分是重點內容:

  • 首先要導入spring標籤庫,由於jsp使用的是spring mvc國際化;
  • 使用<spring:message>標籤獲取國際化文件中的值;

第五步:建立控制器

 1 /*
 2  * 測試Spring MVC中傳遞參數的方式
 3  */
 4 
 5 @Controller
 6 @RequestMapping("/param")
 7 public class ParamController {
 8 
 9     @Autowired
10     @Qualifier("paramService")
11     ParamService paramService = null;
12 
13     @RequestMapping(value = "index", method = RequestMethod.GET)
14     public ModelAndView getParam() {
15         ModelAndView mView = new ModelAndView();
16         List<Param> paramList = paramService.getAllParams();
17         mView.addObject("paramList", paramList);
18         mView.setViewName("param/indexParam");
19         return mView;
20     }
21 }

 控制器中主要就是獲取列表並渲染到indexParam視圖中,這樣在頁面就能獲取到列表數據了,下面進行測試;

第六步:測試

在瀏覽器輸入URL:http://127.0.0.1:8080/mvc/param/index,結果以下:

由於模式是簡體中文,因此不須要輸入language參數就能獲取資源文件內容實現國際化,下面將URL改成這樣

http://127.0.0.1:8080/mvc/param/index?language=en_US,即加上參數language,並定義爲英文,結果以下:

表格表頭的列名變成英文,說明國際化成功。

以上就是完整的實例。

相關文章
相關標籤/搜索