2012-11-20 44 views
0

我想使用控制器從.properties文件讀取屬性,並在jsp文件中顯示它的值,這是一個使用依賴注入的視圖,方法是將檢索到的屬性存儲在pojo中。如何向bean中注入從控制器讀取的屬性文件的值?

+2

請嘗試提交一點點,以便它可以幫助您的問題 –

+0

爲什麼你會儲存在POJO的價值觀去挖掘,我認爲這是相當關於MessageSource的組織.springframework.context.support.ReloadableResourceBundleMessageSource - 以下是谷歌建議的第一個鏈接:http://viralpatel.net/blogs/spring-3-mvc-internationalization-i18n-localization-tutorial-example/ –

回答

2

爲此使用PropertyPlaceholderConfigurer。這些屬性將在春季加載,因此您的控制器無需執行此操作。您可以直接將屬性注入到視圖中。

+0

嗨,這可能是愚蠢的問題,但我不知道答案。如果彈簧通過PropertyPlaceholder加載屬性文件,我如何在我的jsp中查看它(視圖) –

0

試試這個

@Component 
class MyComponent { 

    @Property(key = "proo.xmlurl") 
    public void setUrlString(String urlStr) { 
     try { 
      this.url = new URL(urlStr); 
     } catch(MalformedURLException e) { 
      throw new IllegalArgumentException(urlStr + " is not a valid http   url", e); 
     } 
    } 
} 
在屬性

文件把這個

proo.xmlurl=${proo.xmlurl} 
0

AppContext可以有這樣的:

<context:property-placeholder location="classpath:my.properties" ignore-unresolvable="true"/> 

控制器可以有這個

@Value("${language}") 
private String language; 

@Value("${allLanguages}") 
private String allLanguages; 

,其屬性文件中包含這個,或者類似的

language = java 
alllanguages = java and \ 
       c++ 
somethingelse = whatever 
相關問題