如何使spring mvc应用程序多语言?
我正在使用SpringMVC4开发web服务,我必须使它成为多语言的。这样:如果即将到来的url请求包含土耳其语的如何使spring mvc应用程序多语言?,spring,spring-mvc,internationalization,Spring,Spring Mvc,Internationalization,我正在使用SpringMVC4开发web服务,我必须使它成为多语言的。这样:如果即将到来的url请求包含土耳其语的“…?lang=tr”,英语的“…?lang=en”。我在stackoverflow上读到覆盖LocaleChangeInterceptor。但当我删除它时,应用程序不起作用了。我正在使用它。我还没有找到解决办法。对于下面的配置,即使我切换语言,消息也始终为英语。此外,我还需要用java而不是jsp获取lang消息。但当我初始化时,它总是在java类中返回默认消息。我把这里搞砸了。请
“…?lang=tr”
,英语的“…?lang=en”
。我在stackoverflow上读到
覆盖LocaleChangeInterceptor
。但当我删除它时,应用程序不起作用了。我正在使用它。我还没有找到解决办法。对于下面的配置,即使我切换语言,消息也始终为英语。此外,我还需要用java而不是jsp获取lang消息。但当我初始化时,它总是在java类中返回默认消息。我把这里搞砸了。请求你的帮助
@Autowired
private MessageSource messageSource;
Locale locale = LocaleContextHolder.getLocale();
String msg = messageSource.getMessage("deneme", null, "Deault Message!", locale);
dispatcher servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
<property name="basename" value="classpath:/messages"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="localeChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver">
<property name="defaultLocale" value="en"></property>
</bean>
<!-- Defining which view resolver to use -->
<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
将此项添加到xml中以注册localChangeInterceptor,它将正常工作
<mvc:annotation-driven/>
<context:component-scan
base-package="controller">
</context:component-scan>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
可能重复ofhttp://stackoverflow.com/questions/22490558/internationalization-with-spring-mvc?rq=1yes. 有点像我的问题。但我也在问如何用java获取消息。当我根据那篇文章修改xml时,我得到“没有名为”localeChangeInterceptor“的bean定义为”异常。还有这个错误:“类”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping“被标记为已弃用”