How to show values from property file in JSP in a spring MVC app

后端 未结 5 821
孤街浪徒
孤街浪徒 2020-12-04 20:14

I\'m setting my properties in app-servlet.xml with a bean like this:

    

        
相关标签:
5条回答
  • 2020-12-04 20:38

    What you can also do that doesn't tie you to looking up properties in a single property placeholder, or if you are using java config and just instantiating a PropertySourcesPlaceholderConfigurer is use the environment object:

    <spring:eval expression="@environment.getProperty('application_builtBy')" />
    
    0 讨论(0)
  • 2020-12-04 20:58

    In context just do this:

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

    for creating Properties bean(same as @nkjava.blogspot.com in his answer). But this is not all work need todo.

    Now you need to expose this bean to JSP. There are few way to do this, depends on type of view resolver. There is solution for InternalResourceViewResolver - you need to set "exposeContextBeansAsAttributes" to true and populate "exposedContextBeanNames" with list of required beans.

    For tiles also are solution.

    Than you can simply use this bean in your JSP. Via EL for example:

    ${propertyConfigurer['my.string.from.prop.file']}
    
    0 讨论(0)
  • 2020-12-04 20:59
    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" 
        id="messageSource"
        p:basenames="WEB-INF/i18n/site"
        p:fallbackToSystemLocale="false"/>
    

    Now this is your Properties File

    site.name=Cool Bananas
    

    And here goes your JSP

    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    <html>
      <head>
        <title><spring:message code="site.name"/></title>
      </head>
      <body>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-04 20:59

    in Spring version 4, you find the property file :

    1) xml mode

                    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                       <property name="ignoreUnresolvablePlaceholders" value="true"/>
                          <property name="locations">
                              <list>
                                <!-- default resources folder (default package maven project) -->
                                 <value>classpath:mongodb.remote.properties</value>  
                                    <!-- Or in /WEB-INF/ folder -->
                                  <value>/WEB-INF/mongodb.remote.properties</value>  
                              </list>
                          </property>
                      </bean>
    ----------------------------------------------------------------------------------------
    

    2) programmatic mode:

        If you have for example this package : com.profile.config, com.profile.controller, ecc.. 
        it's not problem if you put only com.profile, it's ok !!! Now
    
    
        @Configuration
        @ComponentScan(basePackages = "com.profile")
        /** resources folder & default package maven project*/
        @PropertySource(value = { "classpath:mongodb.remote.properties" }) 
        public class MyPropertySourcesPlaceholderConfigurer {
    
    
            @Bean
            public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
                return new PropertySourcesPlaceholderConfigurer();
            }
        }
    
    
        ---------------------------------------------------------------------------------
        Your property file
    
        label.test.val=this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    
        @Controller
        public class LabelsAndValuesController {
    
    
             @Value("${label.test.val}")
             String test;
    
        }
    

    output:

        ---------------------------------------------------------------------------------
        this is the property file value!!!!!
        ---------------------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-12-04 21:00

    Spring config

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

    jsp

    <spring:eval expression="@propertyConfigurer.getProperty('propertyNameHere')" />
    
    0 讨论(0)
提交回复
热议问题