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
Spring i18消息属性不通过控制器反映在JSP中_Spring_Internationalization - Fatal编程技术网

Spring i18消息属性不通过控制器反映在JSP中

Spring i18消息属性不通过控制器反映在JSP中,spring,internationalization,Spring,Internationalization,非常感谢您的帮助,因为我在整理这些东西时迷失了方向,JSP并没有按照URL参数中传递的语言显示spring消息。当在welcome.JSP中选择了不同的语言时,JSP无法通过SessionLocaleResolver获取SessionLocale。我做错了什么 我使用的是Spring 3.1.1。提前谢谢 <!-- Configures the @Controller programming model --> <mvc:annotation-driven /> <

非常感谢您的帮助,因为我在整理这些东西时迷失了方向,JSP并没有按照URL参数中传递的语言显示spring消息。当在welcome.JSP中选择了不同的语言时,JSP无法通过SessionLocaleResolver获取SessionLocale。我做错了什么

我使用的是Spring 3.1.1。提前谢谢

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:view-controller path="/views/**" view-name="secure/index" />

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

<!-- URL interceptor -->
<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

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

<!-- Application Message Bundle -->
<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
消息文件放在/WEB-INF/messages中/ 消息属性

welcome_springmvc=Welcome to Spring MVC
message_welcome_back_user=Welcome back {0} Your Last Successful : {1} Failed : {2}
welcome_springmvc=\u6B22\u8FCE\u5230 Spring \u7684 MVC
welcome_springmvc=Bienvenue \u00E0 Spring MVC
消息_zh.properties

welcome_springmvc=Welcome to Spring MVC
message_welcome_back_user=Welcome back {0} Your Last Successful : {1} Failed : {2}
welcome_springmvc=\u6B22\u8FCE\u5230 Spring \u7684 MVC
welcome_springmvc=Bienvenue \u00E0 Spring MVC
消息“欢迎回来”\u user=\u6B22\u8FCE\u56DE\u6765{0}\u4E0A\u6B21\u6210\u529F:{1}\u5931\u8D25:{2}

消息\u fr.properties

welcome_springmvc=Welcome to Spring MVC
message_welcome_back_user=Welcome back {0} Your Last Successful : {1} Failed : {2}
welcome_springmvc=\u6B22\u8FCE\u5230 Spring \u7684 MVC
welcome_springmvc=Bienvenue \u00E0 Spring MVC
消息\u欢迎回来\u user=Bienvenue\u00E0 nouveau{0}Votre r\u00E9ussite Derni\u00E8re:{1}manqu\u00E9:{2}

---welcome.jsp

Spring Message from Property:<b><spring:message code="welcome_springmvc" text="default text" /></b>
Message from Controller:<b>${welcome_back_msg}
Spring消息来自属性:
来自控制器的消息:${welcome\u back\u msg}
JSP图像

您可以看到来自属性always fallsback到locale=en的Spring消息。
感谢您在这方面的帮助。

仅实例化LocaleChangeInterceptor Bean是不够的。以及所有拦截器,它必须注册为拦截器:

因此,将bean def放在
标记中:

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


顺便说一句:看看jsp spring消息标签

谢谢Ralph,我们添加了,没有运气。最后,我们在控制器中尝试了下面的代码,结果一切正常。再次感谢Ralph给我们一些提示,让我们附加我们的松散部分。
LocaleResolver LocaleResolver=context.getApplicationContext().getBean(“LocaleResolver”,LocaleResolver.class);字符串j_lang_code=(字符串)request.getSession().getAttribute(“语言环境”);如果(j_lang_code==null){j_lang_code=DEFAULT_lang_code;}Locale Locale=new Locale(j_lang_code);setLocale(请求、响应、区域设置);request.setAttribute(DispatcherServlet.LOCALE\u RESOLVER\u属性,localeResolver);setAttribute(DispatcherServlet.LOCALE_解析器_BEAN_名称,“localeResolver”)