Come utilizzare la proprietà dal file di proprietà specificato nel PropertyPlaceholderConfigurer in JSP

StackOverflow https://stackoverflow.com/questions/3933862

  •  30-09-2019
  •  | 
  •  

Domanda

Nel mio contesto applicativo ho definito file delle proprietà:

<context:property-placeholder  location="classpath:application.properties" />

Voglio ottenere il valore della proprietà definita in quel file sulla pagina JSP. C'è un modo per farlo nel modo

${something.myProperty}?
È stato utile?

Soluzione

PropertyPlaceholderConfigurer può analizzare solo segnaposto in configurazione di Spring (XML o annotazioni). È molto comune nelle applicazioni di primavera utilizzano un fagiolo Properties. È possibile accedervi dalla visualizzazione in questo modo (supponendo che si sta utilizzando InternalResourceViewResolver):

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list><value>classpath:config.properties</value></list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="exposedContextBeanNames">
        <list><value>properties</value></list>
    </property>
</bean>

Poi, nel vostro JSP, è possibile utilizzare ${properties.myProperty} o ${properties['my.property']}.

Altri suggerimenti

Dopo la Primavera 3.1, è possibile utilizzare tag <spring:eval /> con SPEL in questo modo:

<spring:eval expression="@applicationProps['application.version']" 
             var="applicationVersion"/>

Per usare con più sedi in un elenco che potrebbe non essere presenti come può essere fatto con il contesto: Dimora-segnaposto di fagioli:

<beans:bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <beans:property name="ignoreResourceNotFound" value="true" />
    <beans:property name="locations">
        <beans:list>
            <beans:value>classpath:application.properties</beans:value>
            <beans:value>classpath:environment.properties</beans:value>
            <beans:value>classpath:environment-${env}.properties</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

Per utilizzare la proprietà di espansione segnaposto ricorsiva nelle viste, è necessaria una soluzione diversa, dare un'occhiata a questa risposta:

https://stackoverflow.com/a/10200249/770303

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

Ora, questo è il vostro File Properties

`site.name=Cool Bananas`

E. Qui va il tuo JSP

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

Questo ti mostrerà le tabelle dello schema corrente (che si è iscritto):

select table_name from user_tables order by table_name;

Questo ti mostrerà le tabelle di schema, per il quale si dispone di selezionare i diritti almeno:

select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top