Spring Webflows语言环境检测不正确为en_US,但有证据表明语言环境已更改

时间:2013-04-29 09:22:32

标签: spring-mvc internationalization spring-webflow-2

我的项目中有4个可用的语言环境:ar_AE,en_GB,en_US,ur_PK。我localeResolver中的默认设置是en_GB。

无论我将当前的语言环境设置为什么,当我输入webflow时,EL表达式${pageContext.response.locale}始终评估为en_US,但是正确查找消息(例如,如果我当前ar_AE,文本是阿拉伯文,文本方向是rtl,但我JSP中的当前语言是en_US)。

我的区域设置:

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

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en_GB" />
</bean>

我的JSP代码段:

<a href="#i18n" class="dropdown-toggle" data-toggle="dropdown">
    <span class="${pageContext.response.locale}-i18n-option i18n-option-selected"> 
        <spring:message code="Native Name (${pageContext.response.locale})" /> 
    </span>
    <b class="caret"></b>
</a>

我的网络流配置:

<!--Maps request paths to flows in the flowRegistry--> 
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> 
    <property name="order" value="0" /> 
    <property name="flowRegistry" ref="flowRegistry" /> 
</bean>

<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>

<!-- Executes flows: the entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />

<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/webflows">
    <webflow:flow-location-pattern value="/**/flow.xml" />
</webflow:flow-registry>

<!-- Plugs in a custom creator for Web Flow views -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" development="true" />

<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers" ref="viewResolver" />
</bean>

那么如何在Webflow中的JSP中显示正确的当前语言环境?

1 个答案:

答案 0 :(得分:1)

问题显然是WebContext的WebContext没有改变,我需要在我的viewResolver bean中显式设置context属性:

<bean 
    id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver" 
    p:requestContextAttribute="requestContext" 
    p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />

然后我可以将JSP中的行改为:

<span class="${requestContext.locale}-i18n-option i18n-option-selected"> 
    <spring:message code="Native Name (${requestContext.locale})" /> 
</span>

从新的上下文属性requestContext读取。由于我已将viewResolver bean用于Web流和非Web流页面,因此该解决方案可在全球范围内使用。