How to access system properties in Thymeleaf template?

junkie picture junkie · Feb 12, 2014 · Viewed 17.1k times · Source

I need to access system properties in a Thymeleaf template. It would be nice if this was possible so that I don't have to populate the spring mvc model explicitly with properties. I'm trying to use SPEL for this purpose but it's not working.

<h2 th:text="${ systemProperties['serverName'] }">Service name</h2>

<h2 th:text="*{ systemProperties['serverName'] }">Service name</h2>

Both of these give me:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 17): Cannot index into a null value

Even if I try to access a jdk property it gives the same error so I know it's not the fact that the property is missing. What am I doing wrong or is there another way to do this?

Answer

Dave Bower picture Dave Bower · Jan 6, 2015

I use

${@environment.getProperty('myPropertyName')}