Java SpringMVC:Spring:message和RequestLocale之间的差异
我有一些矛盾,我需要一些专家的建议。我使用的是SpringMVC3.2 有这种豆子:Java SpringMVC:Spring:message和RequestLocale之间的差异,java,spring,spring-mvc,tags,locale,Java,Spring,Spring Mvc,Tags,Locale,我有一些矛盾,我需要一些专家的建议。我使用的是SpringMVC3.2 有这种豆子: <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:messages" /> <property name="de
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="10" /> <!-- for easier development -->
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
使用spring标记时,区域设置解析为“es”,并按预期使用消息(这是我的系统语言)
然而,如果我试图以编程方式获取区域设置,我总是得到“en”。我尝试过以下几种选择:
选项1:
@Autowired
private LocaleResolver localeResolver;
....
localeResolver.resolveLocale(request);
LocaleContextHolder.getLocale();
RequestContextUtils.getLocale(request);
选项2:
@Autowired
private LocaleResolver localeResolver;
....
localeResolver.resolveLocale(request);
LocaleContextHolder.getLocale();
RequestContextUtils.getLocale(request);
选项3:
@Autowired
private LocaleResolver localeResolver;
....
localeResolver.resolveLocale(request);
LocaleContextHolder.getLocale();
RequestContextUtils.getLocale(request);
所有的结果都是“恩”朗
问题是,如何获取spring使用的区域设置:message标记
根据文献记载,
当请求传入时,DispatcherServlet将查找区域设置解析程序,如果找到,它将尝试使用它来设置区域设置。使用RequestContext.getLocale()方法,您始终可以检索由区域设置解析程序解析的区域设置
但是最终html中的所有文本都是“es”,此方法返回“en”消息标记使用
RequestContext
检索MessageSource
和当前的Locale
。这可以在MessageTag类中的resolveMessage方法中看到:
return messageSource.getMessage(this.message, getRequestContext().getLocale());
请参阅:通常,可以使用
RequestContext.getLocale()
()以编程方式访问区域设置
但是,在控制器的@RequestMapping
处理程序方法中,您可以依靠Spring来获取区域设置。
由于Locale
是一个变量,只需将Locale
类型的变量声明为控制器方法的参数,即可访问当前区域设置
拦截器在所示的配置中,locale拦截器声明为bean,但未注册为拦截器。请从以下位置尝试此操作:
或者,从Spring3.0开始,您可以使用mvc:interceptors
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
好吧,我自己回答。我对它进行了深入的调试,发现问题在于ReloadableResourceBundleMessageSource定义中缺少一个属性。添加fallbackToSystemLocale=false并删除localeResolver中的默认区域设置现在区域设置在请求中解析为“es” 到目前为止还不错。遗憾的是,通过“?lang=en”参数更改区域设置的部分不起作用,但这是另一回事 分辨率:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="2" />
<property name="fallbackToSystemLocale" value="false" />
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
尝试了一个参数,但仍然是“en”。这一点很好!mvc:拦截器解决了第二个问题。谢谢我调试了spring源代码,直到这一行和区域设置在此点为“en”,然而,返回的消息来自“es”消息文件!我完全迷路了:注册拦截器:将拦截器声明为bean还不够,您应该使用mvc:interceptor或