2012-08-15 28 views
2

所以我有一堆在我的郵件消息的「開頭」的鑰匙文件中像春:消息,如何讓所有的消息與關鍵

alt.text1=Hello 
alt.text2=Goodbye 

在我的JSP我可以讀取並顯示這些就好了與

<spring:message code="alt.text1"/> 
<spring:message code="alt.text2"/> 

這使得作爲

Hello Goodbye 

的問題是,我怎麼能獲取所有其消息密鑰的S-與'alt'的撻。 IE我想顯示所有'alt'消息一次,但不顯示文件中不以alt開頭的任何其他消息。

我知道這些消息是JSP中的散列映射,我怎樣才能訪問這個映射來通過它的鍵和值來訪問它呢?

回答

1

我最終創建了一個如下所示的CustomerMessageSource,並將listAllAltLabels的結果放到視圖中。然後,這是一個迭代的簡單情況。

public class CustomMessageSource extends ReloadableResourceBundleMessageSource { 

    public Map<String, String> listAllAltLabels(String basename, Locale locale) { 
     Map<String, String> altLabels = new HashMap<String, String>(); 

     PropertiesHolder propertiesHolder = getMergedProperties(locale); 
     Properties properties = propertiesHolder.getProperties(); 

     for(Object key : properties.keySet()){ 
      if(((String)key).startsWith("alt.")) { 
       altLabels.put((String)key, (String)properties.get(key)); 
      } 
     } 

     return altLabels; 
    } 

} 

我用彈簧的Webflow(隱藏了大部分控制器),但在控制器/動作基本上某處渲染頁面之前,請撥打listAllAltLabels和分配結果「altLabelMessages」,並將其放入模型/視圖。

然後在視圖(JSP)

<c:forEach items="${altLabelMessages}" var="message"> 
    <form:option value="${message.key}" label="${message.value}"/> 
</c:forEach> 
+0

嗨對不起...我不知道你在哪裏加載屬性文件。你用不同的方法做了這個嗎?感謝您分享這一點.... – 2017-03-20 09:00:29

0

我不知道Spring,但它看起來像沒有tag使用Spring來獲取消息包。

如果您使用<fmt:setBundle/>,您將收到一個LocalizationContext對象,其中包含一個ResourceBundle。不幸的是,keySet方法不能使用表達式語言訪問。

你可以做的是將消息包加載到bean中,並創建一個以alt.開頭的密鑰列表,或者只是公開整個密鑰集。