[Note]Spring MVC、hibernate validator和i18n(二)

來源:互聯網
上載者:User

承接上節,本節將對Spring中的i18n的一個執行個體進行講解。首先還是把項目的目錄結構展示一下:
SpringMVCBasic
    META-INF
    styles
    WEB-INF
        classes
        lib
        messages
        spring
        views
        urlrewrite.xml
        web.xml

 

 

上一節提到,使用者的請求“/”會被Spring mvc導向到名字為welcome的view,然後views目錄下的welcome.jsp就會被解析,下面是該檔案的內容:<%@page contentType="text/html" pageEncoding="UTF-8"%>/<br /><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><br /><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<br /> "http://www.w3.org/TR/html4/loose.dtd"></p><p><html><br /> <head><br /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /> <link rel="stylesheet" href="<c:url value=" mce_href="<c:url value="/styles/blueprint/screen.css" />" type="text/css" media="screen, projection"><br /><link rel="stylesheet" href="<c:url value=" mce_href="<c:url value="/styles/blueprint/print.css" />" type="text/css" media="print"><br /><!--[if lt IE 8]><br /><link rel="stylesheet" href="<c:url value=" mce_href="&lt;c:url value="/styles/blueprint/ie.css" />" type="text/css" media="screen, projection"><br /><![endif]--><br /> <title><fmt:message key="welcome.title"/></title><br /> </head><br /> <body><br /> <div class="container"><br /> <h1><br /> <fmt:message key="welcome.title"/><br /> </h1><br /> <p><br /> Locale = ${pageContext.response.locale}<br /> </p><br /> <ul><br /><li><br /> <a href="?locale=zh_cn">zh</a> | <a href="?locale=en_us">en</a> | <a href="?locale=es_es">es</a> | <a href="?locale=de_de">de</a><br /> </li><br /> </ul><br /> <ul><br /><li><br /> <a href="account"><fmt:message key="view.title"/></a><br /> </li><br /> </ul><br /> </div><br /> </body><br /></html>

關於該檔案要說明的幾點有:

<!--[if lt IE 8]>
  <link rel="stylesheet" href="<c:url value="/styles/blueprint/ie.css" />" type="text/css" media="screen, projection">
<![endif]-->
是指當用戶端的瀏覽器版本低於IE8時就使用ie.css中定義的樣式。

${pageContext.response.locale}顯示了目前使用者使用的locale

fmt:message和<a href="?locale=zh_cn">zh</a>等配合使用,就能使fmt:message要顯示的內容根據Local改變。上一節提到過,這個功能是Spring的org.springframework.web.servlet.i18n.LocaleChangeInterceptor實現的。fmt:message可以顯示綁定的資源檔中的內容,上節提到,/WEB-INF/messages目錄下的messages*.properties檔案被綁定。在本項目的,/WEB-INF/messages下有多個messages*.properties檔案,根據檔案的尾碼名(如_zh,_en,_de等)分別對應不同的Locale,如果檔案尾碼名與當前的Locale沒有匹配的,就預設使用messages.properties檔案,先將messages.properties和messages_zh.properties檔案的內容展示如下:

messages.properties
welcome.title=Congratulations! Spring is running!<br />typeMismatch=could not be parsed<br />createForm.title=Create Account<br />view.title=@Controller Example<br />accountFields=Account Fields<br />viewAccount=View Account<br />accountName=Name<br />accountBalance=Balance<br />accountEquityAllocation=Equity Allocation<br />accountRenewalDate=Renewal Date<br />submit=Submit

message_zh.properties

welcome.title=/u606D/u559C!Spring/u6B63/u5728/u8FD0/u884C!<br />typeMismatch=/u4E0D/u80FD/u88AB/u89E3/u6790<br />createForm.title=/u521B/u5EFA/u5E10/u6237<br />view.title=@Controller/u793A/u4F8B<br />accountFields=/u5E10/u6237/u7684/u5B57/u6BB5<br />viewAccount=/u67E5/u770B/u5E10/u6237<br />accountName=/u540D/u5B57<br />accountBalance=/u5E73/u8861<br />accountEquityAllocation=/u516C/u5E73/u5206/u914D<br />accountRenewalDate=/u91CD/u5EFA/u65E5/u671F<br />submit=/u63D0/u4EA4

注意,由於我的開發環境是沒有安裝中文語言套件的ubuntu9.10,所以輸入的中文都被轉換為了UTF-8碼,並且在web.xml檔案中也對UTF-8進行了強制性的設定。

其他local對應的messages.properties檔案就不再列出。

將fmt:message標籤中的key值指定為messages.properties檔案中各等式左邊的值,那麼fmt:message就能根據不同的Locale顯示相應的等號左邊的值。

 

welcome.jsp檔案中的<a href="account"><fmt:message key="view.title"/></a>連結會向伺服器發送"/account"請求,該請求會被一個自訂的控制器接收處理,下一節將對該控制器進行說明。

聯繫我們

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