In a Thymeleaf template, how can I get the value of a system environment variable?
I thought perhaps ${@environment.getProperty('VariableName', 'DefaultVariableValue')} would work...however; it seems to always return DefaultVariableValue, even when VariableName is defined in the environment.
Are you using Spring? You can pull the property from the controller, insert it into the model, and then reference the model value from the Thymeleaf template. Alternatively, if you're not using spring, get the system property using System.getProperty("variableName"). See the example below of my 500 page that needed a variable.
@Controller
public class Error {
@Value("${variableName}")
private String variableName;
@RequestMapping("/500")
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView internalServerError() {
ModelAndView mav = new ModelAndView("error");
mav.addObject("variableName", variableName);
return mav;
}
}
And in the template:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Nitro Error</title>
<base th:href="${baseUrl}"/>
</head>
...
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With