在Spring MVC中定义MessageSource和LocaleResolver Bean以添加i18n支持 [英] Defining MessageSource and LocaleResolver beans in Spring MVC for adding i18n support

查看:354
本文介绍了在Spring MVC中定义MessageSource和LocaleResolver Bean以添加i18n支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向Spring MVC项目(3.2.0.RELEASE)添加i18n支持.我在/src/main/resources/bundle下有两个捆绑包:

I'm trying to add i18n support to a Spring MVC project(3.2.0.RELEASE). I have two bundles below /src/main/resources/bundle:

messageBundle_en.properties
messageBundle_vi.properties

spring mvc的配置如下:

And the configurations for spring mvc as below:

<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>

使用上述配置,该应用程序无法运行.错误是

with above configurations the application is not working. The error was

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支持的教程相比,我花了很多时间.我看到只有一个区别:CookieLocaleResolverReloadableResourceBundleMessageSource的bean定义具有id属性.所以我将配置更改为

I spent many hours comparing with tutorials of adding i18n support. I saw that there is only one difference: bean definitions of CookieLocaleResolver and ReloadableResourceBundleMessageSource have id attributes. So I changed the configurations to

<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>

现在一切正常!

ReloadableResourceBundleMessageSourceCookieLocaleResolver是否需要在其定义中包含ID?

Do ReloadableResourceBundleMessageSource and CookieLocaleResolver require to have ids in their definitions?

为什么InternalResourceViewResolver不需要ID?

想知道是否有人可以给我详细解释.

Wondering if anyone can give me an explanation in details.

推荐答案

DispatcherServlet.java

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.

Spring uses some conventional bean name to intialize DispatcherServlet.

在您的情况下,如果未找到名为"localeResolver"的bean(因此将忽略您的自定义LocaleResover),spring将使用默认值.

In your case, spring will use a default one if no bean named "localeResolver" is found (therefore your custom LocaleResover is ignored).

更新

在messageSource情况下,

In messageSource case,

"当加载ApplicationContext时,它会自动 搜索在上下文中定义的MessageSource bean. Bean必须具有名称messageSource .如果找到了这样的豆子,所有的 对前面方法的调用将委派给消息源.如果 找不到消息源,ApplicationContext尝试找到一个消息源. 包含同名bean的父对象.如果是这样,它将使用 bean作为MessageSource.如果ApplicationContext找不到任何 消息源,实例化一个空的DelegatingMessageSource 为了能够接受对上面定义的方法的调用."

"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文档.

quoted from spring doc.

这篇关于在Spring MVC中定义MessageSource和LocaleResolver Bean以添加i18n支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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