Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Most of the time I access the properties in my controllers or other classes like this:

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

But what if I want to use a property in a JSP file and bypass the controller. Meaning I don't want the value type being passed from the controller to the JSP as a model attribute.

Is there a way to access properties directly in a jsp?

like image 772
birdy Avatar asked Feb 27 '13 11:02

birdy


People also ask

How read properties file in JSP?

In order to get your properties file, you could use the helpful method in ServletContext: getResourceAsStream. getValuesFromPropertiesFile(request, "my_properties. txt" ); It could be noted that the Properties class does implement the Map interface, and you can pass it straight back to your JSP for read-only purposes.


3 Answers

Spring config

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

jsp

<spring:eval expression="@propertyConfigurer.getProperty('propertyNameHere')" /> 
like image 156
NaveenKumar1410 Avatar answered Sep 19 '22 02:09

NaveenKumar1410


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')" />
like image 43
Kieran Avatar answered Sep 22 '22 02:09

Kieran


<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>
like image 39
Nikhil Kotak Avatar answered Sep 20 '22 02:09

Nikhil Kotak