在其他 Spring 消息(属性文件)中使用一个消息作为参数

And*_*ndy 9 resources spring message properties

我需要做这样的事情:

bob.common=goat
bob.have=I have a {bob.common}!
bob.want=I want a {bob.common}!
bob.need=I need a {bob.common}!
Run Code Online (Sandbox Code Playgroud)

这种事情可能吗?我知道这看起来很愚蠢,但是这里需要能够重用一个公共部分,而且我们真的不能(不想)以编程方式做到这一点。

我们已经在我们的属性中使用了编号参数,但我们希望能够传递对另一个属性的引用。

Jul*_*iou 5

我建议这样做:

bob.common=goat 
bob.have=I have a {0}!
bob.want=I want a {0}!
bob.need=I need a {0}!
Run Code Online (Sandbox Code Playgroud)

然后在您的页面中:

<spring:message code="bob.common" var="animal"/>
<spring:message code="bob.have" arguments="${animal}"/>
<spring:message code="bob.want" arguments="${animal}"/>
<spring:message code="bob.need" arguments="${animal}"/>
Run Code Online (Sandbox Code Playgroud)

例如,如果您想改变您的动物,那么您想要做的方式就太严格了。


mil*_*use 3

根据Spring 变更日志,从 2.5.3 开始就支持了:

  • PropertyPlaceholderConfigurer 也支持占位符键中的嵌套键(例如“${db.${environment}}”)

因此,对于您的示例案例,您应该能够使用:

bob.have=I have a ${bob.common}!
Run Code Online (Sandbox Code Playgroud)

并且PropertyPlaceholderConfigurer应该识别“嵌套键”并正确解析它。

  • FWIW,这在实际的 *Messages* 文件中对我不起作用。我觉得这太好了,令人难以置信。我错过了什么吗? (8认同)