Question

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?

Was it helpful?

Solution

I use

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

OTHER TIPS

The link @Sudarshan_SMD posted gave me an idea and I finally got this to work as below.

Put the following bean in your spring context.

<bean id="sysprops" class="java.lang.System" factory-method="getProperties" />

Then access the bean directly as follows in your thymeleaf template.

${@sysprops['yourPropertyName']}

This works because @sysprops allows direct access to the bean and the bean, which is java.lang.System, extends java.util.Hashtable and therefore allows key based access as opposed to function invocation based access. Doing it this way also means that you only have to define this bean once and use it across all your templates which is very convenient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top