一個軟件,一個產(chǎn)品,都是一點點開發(fā)并完善起來的,功能越來越多,性能越來越強,用戶體驗越來越好……這每個指標的提高都需要切切實實的做點東西出來,好比,你的這個產(chǎn)品做大了,用的人多了,不僅僅再是上海人用,北京人用,還有印度人用,法國人用等等,可以說這個產(chǎn)品已經(jīng)走上了國際化的大舞臺。當印度的哥們輸入url訪問產(chǎn)品時,界面上彈出“歡迎您,三哥”,估計哥們當場就蒙圈了。而這個時候,國際化就應運而生了。
要做國際化這道菜,真的沒有想象中的那么復雜,反而很簡單,不信你看——
1. 注入resourcebundlemessagesource
在springmvc.xml添加用于國際化處理的beanresourcebundlmessagesource
1 | <bean id= "messagesource" class = "org.springframework.context.support.resourcebundlemessagesource" > |
3 | <property name= "basename" value= "i18n" ></property> |
這里property中的name是與注入類中的屬性名一直的,這里的value決定了后面國際化文件的名稱,記得是i18n,馬上你就會看到它的用法。
2. 創(chuàng)建國際化文件
總共需要創(chuàng)建三個國際化屬性文件

i18n.properties-默認的國際化文件
i18n_en_us.properties-用于英文環(huán)境的國際化文件
i18n_zh_cn.properties-用于中文環(huán)境的國際化文件
注意:這里為什么文件的名稱都是i18n開頭,因為在第一點的springmvc.xml配置文件中,配置的value值就是i18n
對于i18n.properties和i18n_en_us.properties文件的內(nèi)容相同,如下
i18n_zh_cn.properties
1 | i18n.username=\u7528\u6237\u540d |
3 | i18n.password=\u5bc6\u7801 |
3.新建頁面
分別新建兩個頁面,一個是i18n.jsp,顯示用戶名,同時有跳轉到i18n2.jsp的超鏈接,另一個是i18n2.jsp,顯示密碼,同時有跳轉到i18n.jsp的超鏈接。
i18n.jsp
01 | <%@ page language= "java" contenttype= "text/html; charset=utf-8" |
03 | pageencoding= "utf-8" %> |
13 | <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > |
15 | <title>insert title here</title> |
21 | <fmt:message key= "i18n.username" ></fmt:message><br><br> |
25 | <a href= "i18n2" >i18n2</a> |
i18n2.jsp
01 | <%@ page language= "java" contenttype= "text/html; charset=utf-8" |
03 | pageencoding= "utf-8" %> |
13 | <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > |
15 | <title>insert title here</title> |
21 | <fmt:message key= "i18n.password" ></fmt:message><br><br> |
25 | <a href= "i18n" >i18n</a> |
同時,顯然我們需要在index.jsp中添加一個入口,鏈接到i18n.jsp頁面,如下
為了能夠直接點擊就鏈接過去,而不需要通過handler處理并跳轉到視圖的套路,我們需要在springmvc.xml中添加標簽
1 | <mvc:view-controller path= "/i18n" view-name= "i18n" /> |
3 | <mvc:view-controller path= "/i18n2" view-name= "i18n2" /> |
這樣就能實現(xiàn)直接在地址欄中直接訪問到i18n.jsp和i18n2.jsp頁面了。
小坑:如果i18n.jsp和i18n2.jsp中的編碼方式采用默認“iso-8859-1”,就會出現(xiàn)頁面顯示亂碼

當把編碼改為“utf-8”后,就能夠正常顯示

以上是國際化這道菜的基礎做法,那么如果我還想做一道不用直接訪問i18n.jsp,而是經(jīng)過handler處理后呈現(xiàn)的i18n.jsp,或者我還想做一道不用那么麻煩還要切換語言的國際化菜,有沒有可能,當然,接著看——
1. 注釋之前在springmvc.xml添加對于i18n.jsp直接訪問的標簽
3 | <mvc:view-controller path= "/i18n" view-name= "i18n" /> |
2. 在hanlder處理類springmvctest中添加處理接口
03 | private resourcebundlemessagesource messagesource; |
05 | @requestmapping ( "/i18n" ) |
07 | public string testi18n(locale locale){ |
09 | string val = messagesource.getmessage( "i18n.username" , null , locale); |
11 | system.out.println(val); |
注意這里注入了國際化處理類resourcebundlemessagesource,并使用其getmessage方法獲取國際化后的屬性值。
啟動tomcat服務可以看到

那么如果根據(jù)自己的設定,在不同的語言環(huán)境中顯示相應語言的信息呢
1. 配置sessionlocaleresolver和localechangeinterceptor
01 | <!-- 配置sessionlocaleresolver --> |
03 | <bean id= "localeresolver" class = "org.springframework.web.servlet.i18n.sessionlocaleresolver" ></bean> |
07 | <!-- 配置localechangeinterceptor --> |
11 | <bean class = "org.springframework.web.servlet.i18n.localechangeinterceptor" ></bean> |
這里的localechangeinterceptor主要用于將帶有l(wèi)ocale信息的請求解析為一個locale對象,并得到一個localeresolver對象
之后這里的sessionlocalresolver就會將上面的localresolver對象轉化為session的屬性,并從中取出這個屬性,也就是locale對象,返回給應用程序。
2. 在index.jsp中添加超鏈接
1 | <a href= "i18n?locale=zh_cn" >中文</a> |
5 | <a href= "i18n?locale=en_us" >英文</a> |
這樣,我們就可以看到結果

說完國際化,再來說說springmvc對于json的支持。
在傳統(tǒng)的開發(fā)過程中,我們的handler即controller層通常遵循需要轉向一個jsp視圖的套路;但是這樣的場景并不能滿足所有的要求,比如我們很多時候只需要返回數(shù)據(jù)即可,而不是一個jsp頁面。那么這時候spring mvc3的@responsebody和@responseentity就支持這樣的功能。controller直接返回數(shù)據(jù)(這里我們說說json數(shù)據(jù)),而不是直接指向具體的視圖。這里簡單的分別舉一個上傳和下載的例子。
1. 文件上傳
1.1 使用jquery在index.jsp實現(xiàn)ajax請求
01 | <%@ page language= "java" contenttype= "text/html; charset=utf-8" |
03 | pageencoding= "utf-8" %> |
11 | <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > |
13 | <title>insert title here</title> |
15 | <script type= "text/javascript" src= "scripts/jquery-1.9.1.min.js" ></script> |
21 | $( "#testjson" ).click(function(){ |
27 | $.post(url, args, function(data){ |
29 | for (var i= 0 ; i<data.length; i++){ |
33 | var lastname = data[i].lastname; |
35 | alert(id + ": " + lastname); |
53 | <a href= "emps" >list all employees</a><br/><br/> |
57 | <a href= "testjson" id= "testjson" >testjson</a> |
這里核心的就是用jquery寫的ajax請求
請求的url就是定義的href;
data為請求響應后返回的數(shù)據(jù);
正常情況下,我們應該請求到所有員工的信息,并且通過這里的遍歷得到每一個員工的所有信息如id、lastname等
1.2. 這里我們需要引入三個jar包
-
jackson-annotation-2.1.5.jar
-
jackso-core-2.1.5.jar
-
jackso-databind-2.1.5.jar
這三個主要是用于后面在返回數(shù)據(jù)的轉換中用到的。
1.3. 在handler springmvctest中添加接口
3 | @requestmapping ( "testjson" ) |
5 | public collection<employee> testjson(){ |
7 | return employeedao.getall(); |
這里我的個人理解,就是將通過employeedao查詢到的所有的員工信息,作為響應返回給接口,并最終通過一系列處理得到一個json的數(shù)據(jù)形式,然后在前臺頁面中遍歷解析。而完成這一切就是歸功于注解@responsebody。
具體來說,是有內(nèi)部的一些converter來做這些轉換的,在接口方法中打斷點,進入調(diào)試

選擇dispatcherservlet,找到this->handleradapters->elementdata,在這個數(shù)組中找到requestmappinghandleradapter,點進去找到messageconverters,便可以看到總共有7個converters

這里第7個mappingjackson2httpmessageconverter就是我們添加了以上三個jar包后才加載進來的converter。可以看出,這里有足夠過對于不同數(shù)據(jù)類型處理的轉換器。
1.4 在index.jsp中添加鏈接
1 | <form action= "testfileupload" method= "post" enctype= "multipart/form-data" > |
3 | file: <input type= "file" name= "file" /> |
5 | desc: <input type= "text" name= "desc" /> |
7 | <input type= "submit" value= "submit" /> |
最終的上傳結果如下

2. 文件下載
2.1 準備下載源
在webcontent下新建files目錄,放入aaa.txt,作為下載源
2.2 在index.jsp添加超鏈接作為下載入口
1 | <a href= "testresponseentity" id= "testjson" >testresponseentity</a><br/> |
2.3 在handler springmvctest中添加接口
01 | @requestmapping ( "testresponseentity" ) |
03 | public responseentity< byte []> testresponseentity(httpsession session) throws ioexception{ |
07 | servletcontext servletcontext = session.getservletcontext(); |
09 | inputstream in = servletcontext.getresourceasstream( "/files/aaa.txt" ); |
11 | body = new byte [in.available()]; |
17 | httpheaders headers = new httpheaders(); |
19 | headers.add( "content-disposition" , "attachment;filename=aaa.txt" ); |
21 | httpstatus statuscode = httpstatus.ok; |
23 | responseentity< byte []> response = new responseentity<>(body, headers, statuscode); |
啟動tomcat,我們可以看到aaa.txt真的可以下載啦~~~

好了,到此為止,我們都干了啥
1. 支持國際化
2. 文件上傳
3. 文件下載
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。