Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value of an environment variable in a Thymeleaf template?

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.

like image 446
Jared Avatar asked Dec 05 '25 07:12

Jared


1 Answers

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>
like image 112
sparkyspider Avatar answered Dec 07 '25 20:12

sparkyspider