Spring MVC 了解地區(qū)更改攔截器

2018-07-26 15:41 更新

You can enable changing of locales by adding the LocaleChangeInterceptor to one of the handler mappings (see [Section 21.4, "Handler mappings"](mvc.html

mvc-handlermapping "21.4 Handler mappings" )). It will detect a parameter in

the request and change the locale. It calls setLocale() on the LocaleResolver that also exists in the context. The following example shows that calls to all *.view resources containing a parameter namedsiteLanguage will now change the locale. So, for example, a request for the following URL, <http://www.sf.net/home.view?siteLanguage=nl> will change the site language to Dutch.

你可以在處理器映射(詳見Spring MVC 處理器映射小節(jié))前添加一個LocaleChangeInterceptor攔截器來更改地區(qū)信息。它能檢測請求中的參數(shù),并根據(jù)其值相應(yīng)地更新地區(qū)信息。它通過調(diào)用LocaleResolversetLocale()方法來更改地區(qū)。下面的代碼配置展示了如何為所有請求*.view路徑并且攜帶了siteLanguage參數(shù)的資源請求更改地區(qū)。舉個例子,一個URL為<http://www.sf.net/home.view?siteLanguage=nl>的請求將會將站點語言更改為荷蘭語。

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="siteLanguage"/>
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor"/>
        </list>
    </property>
    <property name="mappings">
        <value>/**/*.view=someController</value>
    </property>
</bean>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號