Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据用户刚刚设置的语言读取属性文件(在spring应用程序中)_Java_Spring_Jsp - Fatal编程技术网

Java 如何根据用户刚刚设置的语言读取属性文件(在spring应用程序中)

Java 如何根据用户刚刚设置的语言读取属性文件(在spring应用程序中),java,spring,jsp,Java,Spring,Jsp,我将在春季创建一个小型web应用程序。我创建了两个属性文件,意大利语和英语 通过像这样的链接,用户可以选择语言 在我的文件servlet.xml中 ...... <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:messa

我将在春季创建一个小型web应用程序。我创建了两个属性文件,意大利语和英语

通过像
这样的链接,用户可以选择语言

在我的文件servlet.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="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>

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>
........

问题是使用
Locale.getDefault()
iI获取JVM的区域设置,而不是用户设置的语言。

如果您可以访问
HttpServletRequest
表单类,请像这样尝试

String s = context.getMessage("intestazione",null, request.getLocale()));

只需将当前区域设置声明为参数,即可将其注入控制器方法。或者,您可以使用LocaleContextHolder.getLocale()。

看起来像是重复的:请看,您不能简单地将Locale定义为任何控制器映射方法的参数之一吗?谢谢,这就是我所需要的:)谢谢您的帮助,但请使用request.getLocale()似乎我得到的是JVM语言环境,而不是用户在我的web应用程序上设置的语言环境。
String s = context.getMessage("intestazione",null, request.getLocale()));