使用SessionLocaleResolver检索区域设置信息

时间:2014-07-10 11:15:42

标签: java spring locale

我在application-context.xml中包含以下内容:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

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


<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

在我的控制器类中,我添加了以下代码来检索语言环境:

@RequestMapping(value = "/userSearch", method = RequestMethod.GET)
public String showUserForm(Model model, HttpSession session) {

    Locale locale = (Locale) session.getAttribute( 
            SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);

    return showHomePage(model, session);
}

Web.xml包含:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

调试时,locale取值为null。它应该采用新设置的语言环境值。我通过url设置的值传递?lang = fr。区域设置值应设置为fr,但它只是给出null。有没有其他方法可以完成相同的工作?需要一些建议。

0 个答案:

没有答案