【问题标题】: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:在开发中,您还可以使用 ReloadableResourceBundleMessageSource 和 cacheSeconds=0 在每次页面刷新时重新加载翻译