كيفية استخدام الممتلكات من ملف العقار المحدد في PropertyHolderConfigurer في JSP

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

  •  30-09-2019
  •  | 
  •  

سؤال

في سياق التطبيق الخاص بي ، حددت ملف الخصائص:

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

أريد الحصول على قيمة الخاصية المحددة في هذا الملف على صفحة JSP. هل هناك طريقة للقيام بذلك في الطريق

${something.myProperty}?
هل كانت مفيدة؟

المحلول

PropertyPlaceholderConfigurer يمكن فقط تحليل العناصر النائبة في تكوين الربيع (XML أو التعليقات التوضيحية). شائع جدًا في تطبيقات الربيع ، استخدم أ Properties فاصوليا. يمكنك الوصول إليه من عرضك بهذه الطريقة (على افتراض أنك تستخدم 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>

ثم ، في JSP الخاص بك ، يمكنك استخدام ${properties.myProperty} أو ${properties['my.property']}.

نصائح أخرى

بعد الربيع 3.1 ، يمكنك استخدام <spring:eval /> علامة مع سبيل مثله:

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

للاستخدام مع مواقع متعددة في قائمة قد لا تكون موجودة كما يمكن القيام بها مع السياق: Property-Placeholder Bean:

<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>

لاستخدام توسع العناصر النائبة للعقار في وجهات النظر ، تحتاج إلى حل مختلف ، وإلقاء نظرة على هذه الإجابة:

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

`<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>`

سيظهر لك هذا جداول المخطط الحالي (الذي قمت بتسجيل الدخول):

select table_name from user_tables order by table_name;

سيوضح لك ذلك طاولات المخطط ، التي لديك حقوق مختارة على الأقل:

select owner, table_name from all_tables where owner='<owner>' order by owner, table_name;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top