2011-11-01 157 views
3

我有必要做這樣的說法:使用一個消息在其他春消息(屬性文件)

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

是這樣的事情可能嗎?我知道這看起來很愚蠢,但是能夠重新使用一個普通的作品在這裏是需要的,我們真的不能(不想)以編程方式進行。

我們已經在我們的屬性中使用了編號參數,但我們希望能夠將參考傳遞給另一個屬性。

回答

2

按照Spring changelog,這已被支持,因爲2.5.3:

  • PropertyPlaceholderConfigurer佔位符鍵支持嵌套鍵以及
(如 「$ {分貝$ {}環境}」)

因此,對於你的例子情況下,你應該能夠使用:

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

PropertyPlaceholderConfigurer應該承認「嵌套關鍵「並正確解決。

+0

有趣。我明天就會出手! – Andy

+1

FWIW,這在我的實際*消息*文件中不起作用。我認爲這真是太好了。我錯過了什麼嗎? – dbreaux

1

我建議這樣做:

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

你想要做就太嚴格,如果你想改變你的動物例如方式。