简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM