2013-02-27 7 views
16

私はこのような豆とapp-servlet.xmlで私のプロパティを設定しています:MVCアプリ

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="location" value="/WEB-INF/my.properties"></property> 
    </bean> 

ほとんどの時間、私は私のコントローラのプロパティにアクセスしますか、

@Value("${dbtype}") 
public String dbType; 

しかし、JSPファイルでプロパティを使用してコントローラをバイパスしたい場合はどうすればよいでしょうか。意味私は、コントローラからモデル属性としてJSPに渡される値型を望んでいません。

jspで直接プロパティにアクセスする方法はありますか? @ nkjava.blogspotと同じプロパティBeanを(作成するための

<util:properties 
    id="propertyConfigurer" 
    location="classpath:yourPropertyFileClasspathHere" 
/> 
<context:property-placeholder properties-ref="propertyConfigurer" /> 

:文脈では

+0

チェックこのリンクhttp://forum.springsource.org/showthread.php?96715-Reading-values-stored-in-properties-file-from-JSP – nav0611

答えて

31

春のコンフィグ

<util:properties id="propertyConfigurer" 
        location="classpath:yourPropertyFileClasspathHere "/> 
<context:property-placeholder properties-ref="propertyConfigurer" /> 

JSP

<spring:eval expression="@propertyConfigurer.getProperty('propertyNameHere')" /> 
1

だけでこれを行います。彼のcom answer)。 しかし、これはすべての仕事に必要なことではありません。

これで、このBeanをJSPに公開する必要があります。 これを行う方法はいくつかありますが、ビューリゾルバのタイプによって異なります。 InternalResourceViewResolverの解決策があります。 "exposeContextBeansAsAttributes"をtrueに設定し、 "exposedContextBeanNames"に必要なBeanのリストを設定する必要があります。

tilesもまた解決策です。

単純にこのBeanをJSPで使用するよりも簡単に使用できます。 ELを経由して例えば:

${propertyConfigurer['my.string.from.prop.file']} 
11

あなたはまた、それが1つのプロパティのプレースホルダのプロパティを調べるにあなたを結ぶ、またはあなたがJavaの設定を使用して、ちょうどPropertySourcesPlaceholderConfigurerをインスタンス化している場合は、環境オブジェクトを使用しているしません何ができますか:

<spring:eval expression="@environment.getProperty('application_builtBy')" /> 
+0

行きます私はこれをもっと役に立ちました。ありがとう – kakabali

8
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
    id="messageSource" 
    p:basenames="WEB-INF/i18n/site" 
    p:fallbackToSystemLocale="false"/> 

は今、これはあなたのプロパティファイルである

site.name=Cool Bananas 

そしてここでは、あなたのJSP

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 
<html> 
    <head> 
    <title><spring:message code="site.name"/></title> 
    </head> 
    <body> 
    </body> 
</html> 
+0

タグライブラリ宣言を指定していただきありがとうございます。これはあまりにも頻繁に省略されています。 – baraber

関連する問題