Spring message 标签有单引号字符的问题

标签 spring properties single-quotes

我的资源包中定义了 3 个属性:

test.key1 = Just a normal snippet.
test.key2 = snippet with {0} arguments {1}.
test.key3 = snippet with 'charac'ter'.
test.key4 = snippet with {0} arguments' and 'characters {1}.

这是我在 jsp 中访问它们的方式:

<spring:message code="test.key1"/>
<spring:message code="test.key2" arguments="ARG1,ARG2"/>
<spring:message code="test.key3"/>
<spring:message code="test.key4" arguments="ARG1,ARG2"/>

渲染它们看起来像这样:

Just a normal snippet.
snippet with ARG1 arguments ARG2.
snippet with 'charac'ter'.
snippet with ARG1 arguments and characters {1}.

所以基本上发生的事情是:一旦 '(单引号)字符出现,参数就不再被填充并且单引号不再被渲染。有趣的是,当单独应用参数或单引号时,spring 没有问题。

我能够通过使用像这样的 2x 单引号来修复测试场景 4:

test.key4 = snippet with {0} arguments'' and ''characters {1}.

哪个也正确呈现

snippet with ARG1 arguments' and 'characters ARG2.

当然,这是一个维护噩梦:所有包含单引号但没有参数的属性都应该有 1 倍的单引号,所有同时包含单引号字符和参数的属性都需要 2 倍的单引号。

这是为什么,有没有办法解决这种不良行为?

最佳答案

显然,这种奇怪的行为是“按设计的”。所有信息都汇总在 this article 中.

要解决此问题,您要么只在具有参数的属性中将单引号加倍,要么向消息资源 bean 添加一个附加属性,在这种情况下,所有单引号都必须加倍,即使在没有参数的属性中也是如此争论。这更加一致!

<property name="alwaysUseMessageFormat" value="true"/>

关于Spring message 标签有单引号字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30579233/

相关文章:

java - Spring 4 MVC 验证不工作 - BindingResult hasErrors 是 false

javascript - 为什么提交到Spring Controller 后java无法识别html文本?

java.util.Properties 多个值 ArrayIndexOutOfBoundsException

javascript - react 错误无法读取未定义的属性 'onChange'

python - SA : can I have a 'year' column_property for a Date column?

keyboard-shortcuts - RubyMine:按键到 "convert to single quoted"?

java - 如何在 Spring Batch 中设置多线程?

java - 使用 jUnit 测试 Spring Restful Web 服务

php - 语法错误,意外的T_STRING,需要 ','或 ';'” [重复

mysql - 如何使用 bash shell 脚本更改临时 root MySQL 密码?