Skip to content

Instantly share code, notes, and snippets.

@woemler
Created November 8, 2013 19:29
Show Gist options
  • Save woemler/7376272 to your computer and use it in GitHub Desktop.
Save woemler/7376272 to your computer and use it in GitHub Desktop.
Using Properties file variables in JSP files and Controllers in Spring 3.2
app.var1=Hello
app.var2=World
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DefaultController {
@Value("${app.var2}")
private String var2;
@RequestMapping(value="/")
public String home(ModelMap map){
System.out.println(otherVar);
map.addAttribute("var2", var2);
return "view";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--Changed from defult-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:component-scan base-package="com.myapp" />
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="/static/" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!--Here is the special sauce-->
<util:properties id="viewPropertyConfigurer" location="classpath:app.properties"/>
<context:property-placeholder properties-ref="viewPropertyConfigurer" />
</beans>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<h2><spring:eval expression="@viewPropertyConfigurer.getProperty('app.var1')" />&nbsp;${var2}!</h2>
@pilot911
Copy link

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment