我正在尝试向 Spring MVC 项目(3.2.0.RELEASE)添加 i18n 支持。我在/src/main/resources/bundle 下面有两个 bundle :
messageBundle_en.properties
messageBundle_vi.properties
spring mvc的配置如下:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:bundle/messageBundle" />
</bean>
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="vi" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
使用上述配置,应用程序无法运行。错误是
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'message.home.header.welcome' for locale 'en_US'.
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
我花了很多时间比较添加 i18n 支持的教程。我发现只有一个区别:CookieLocaleResolver
和 ReloadableResourceBundleMessageSource
的 bean 定义具有 id 属性。所以我将配置更改为
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:bundle/messageBundle" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="vi" />
</bean>
现在运行良好!
ReloadableResourceBundleMessageSource
和 CookieLocaleResolver
是否需要在其定义中包含 id?
为什么InternalResourceViewResolver
不需要ID?
不知道有没有人可以给我详细解释一下。
最佳答案
DispatcherServlet.java
public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver";
private void initLocaleResolver(ApplicationContext context) {
try {
this.localeResolver = context.getBean(LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Using LocaleResolver [" + this.localeResolver + "]");
}
}
catch (NoSuchBeanDefinitionException ex) {
// We need to use the default.
this.localeResolver = getDefaultStrategy(context, LocaleResolver.class);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate LocaleResolver with name '" + LOCALE_RESOLVER_BEAN_NAME +
"': using default [" + this.localeResolver + "]");
}
}
}
Spring 使用一些常规的 bean 名称来初始化 DispatcherServlet。
在你的情况下,如果没有找到名为“localeResolver”的bean,spring将使用默认的bean(因此你的自定义LocaleResover将被忽略)。
更新
在 messageSource 情况下,
"When an ApplicationContext is loaded, it automatically searches for a MessageSource bean defined in the context. The bean must have the name messageSource. If such a bean is found, all calls to the preceding methods are delegated to the message source. If no message source is found, the ApplicationContext attempts to find a parent containing a bean with the same name. If it does, it uses that bean as the MessageSource. If the ApplicationContext cannot find any source for messages, an empty DelegatingMessageSource is instantiated in order to be able to accept calls to the methods defined above."
引用自 spring 文档。
关于spring - 在 Spring MVC 中定义 MessageSource 和 LocaleResolver bean 以添加 i18n 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17909598/