How to get system properties or environment variables in spring thymeleaf template?

Getting system properties or environment variables in spring thymeleaf template is eally easy. Spring framework provies a @Value annotation to inject string value from the annotation to the field. The value can be from application properties or from environments. Lets consider the following example:

Lets say we have a property called spring.profiles.active in our application.properties file. Now we want to get this in our spring controller and then from the controller to thymeleaf template file.

Entry in application.properties

spring.profiles.active=stage

@Value anotation in controller

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @RequestMapping({"/"})
    public String home(Model model) {
        model.addAttribute("targetEnv", activeProfile);
        return "home";
    }

Access from thymeleaf template


Share