Spring MVC默认的Locale和Locale更改不起作用 [英] Spring MVC the default Locale and Locale change doesn't work

查看:139
本文介绍了Spring MVC默认的Locale和Locale更改不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的Spring开发人员,他试图开发支持两​​种语言的示例Web应用程序。我想将默认语言环境设置为阿拉伯语,并在用户在JSP页面中单击所需语言时更改语言环境。

I am a new Spring developer tried to develop sample web app with two languages support.I want to set the default locale to Arabic language and change the locale when the user clicks the desired language in JSP page.

这是我的mvc-dispatcher-servlet.xml,

Here is my mvc-dispatcher-servlet.xml,

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven/>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.benchmark.ushers.presentation.controller"/>

<bean id="internalResourceResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!-- resource bundle  configuration-->
<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:locale/messages" />
    <property name="fallbackToSystemLocale" value="false"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

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

<mvc:interceptors>  
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
          <property name="paramName" value="lang"></property>
    </bean>
</mvc:interceptors>
<!-- end of resource bundle  configuration-->

我的JSP页面如下,

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<tiles:insertDefinition name="defaultTemplate">
	<tiles:putAttribute name="body">



		<div class="body">
			<h1>Ushers</h1>

			lang : <a href="?lang=en">English</a> | <a
				href="?lang=ar">Arabic</a>

			<h3>
				welcome.springmvc :
				<spring:message code="footer.content" text="default text" />
			</h3>
			
			<h3>
				hello :
				<spring:message code="footer.hello" text="default text" />
			</h3>


		</div>



	</tiles:putAttribute>
</tiles:insertDefinition>

我不知道我的代码有什么问题,而只显示英文文本。

I do not know what is wrong in my code while the only displayed the English text only.

推荐答案

上面的配置在问题是对的。问题是在请求的页面集中作为web.xml文件中的欢迎页面,因此它在没有任何拦截器的情况下执行。

The above configuration in the question is correct. The problem was in the requested page sets as welcome page in web.xml file so it is executed without any interceptors.

在web中评论此部分后,每件事情都可以正常工作。 xml

Every thing works fine after comment this part in web.xml

<!--  <welcome-file-list>
		<welcome-file>/WEB-INF/pages/adminHome.jsp</welcome-file>
</welcome-file-list>-->

这篇关于Spring MVC默认的Locale和Locale更改不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆