Created
November 8, 2013 19:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.var1=Hello | |
app.var2=World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> | |
<h2><spring:eval expression="@viewPropertyConfigurer.getProperty('app.var1')" /> ${var2}!</h2> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you!