【问题标题】:Use one Message as argument in other Spring Messages (properties file)在其他 Spring 消息(属性文件)中使用一条消息作为参数
【发布时间】:2011-12-19 20:54:14
【问题描述】:

我需要做这样的事情:

bob.common=goat
bob.have=I have a {bob.common}!
bob.want=I want a {bob.common}!
bob.need=I need a {bob.common}!

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

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

【问题讨论】:

    标签: spring resources properties message


    【解决方案1】:

    根据Spring changelog,从 2.5.3 开始支持:

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

    所以对于你的例子,你应该可以使用:

    bob.have=I have a ${bob.common}!
    

    PropertyPlaceholderConfigurer 应该能够识别“嵌套键”并正确解决。

    【讨论】:

    • 有趣。明天我会试一试!
    • FWIW,这在实际的 Messages 文件中对我不起作用。我认为这太好了,不可能是真的。我错过了什么吗?
    【解决方案2】:

    我建议这样做:

    bob.common=goat 
    bob.have=I have a {0}!
    bob.want=I want a {0}!
    bob.need=I need a {0}!
    

    然后在你的页面中:

    <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}"/>
    

    如果你想改变你的动物,你想做的方式就太严格了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多