【问题标题】:Silent fail for spring:message春季静默失败:消息
【发布时间】:2014-08-18 11:31:53
【问题描述】:

我正在使用 Spring Message Source 来实现我的 webapp 的国际化。

问题是当消息未定义时抛出JspException,在开发阶段添加许多文本时真的很痛苦。

我想要的是记录错误(以便知道缺少哪些文本)并在生成的 HTML 中打印密钥或占位符。

这是可配置的吗?

--

edit:有text属性,但是需要添加到每个message标签中,并且不记录任何东西

【问题讨论】:

    标签: java spring jsp spring-mvc internationalization


    【解决方案1】:

    您可以在标签中使用文本属性。

    <spring:message code="welcome.springmvc" text="default text" />
    

    【讨论】:

    • 谢谢,请看我的编辑,当有大量标签时,这根本不可读
    【解决方案2】:

    找到它: 必须将 useCodeAsDefaultMessage 设置为 true
    它不会记录错误,但至少不会因致命错误而困扰您

    例如在 XML 配置中:

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>/WEB-INF/i18n/main</value>
                <value>/WEB-INF/i18n/dashboard</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="true"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    

    protip:在开发中,您还可以使用 ReloadableResourceBundleMessageSourcecacheSeconds=0 在每次页面刷新时重新加载翻译

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2016-12-13
      • 2021-06-10
      相关资源
      最近更新 更多