Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get spring application environment in thymeleaf

My Spring Boot application runs with 3 configurations:

  • application.properties --> for development environment
  • application-test.properties --> for test environment
  • application-production.properties --> for production environment

How is it possible to get in thymeleaf environment the application is running?

I need to include the Google Analytics code only in production environment.

like image 677
occurred Avatar asked May 17 '14 12:05

occurred


People also ask

Which API represents the environment in which the current Spring Boot application is running?

Interface Environment. Interface representing the environment in which the current application is running.

Is Thymeleaf fully integrated with Spring?

It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.

How do you get the model attribute in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages .


2 Answers

You can do the following if you only have one profile active at a time.

<div th:if="${@environment.getActiveProfiles()[0] == 'production'}">   This is the production profile - do whatever you want in here </div> 

The code above is based on the fact that the Thymeleaf's Spring dialect lets you access beans using the @ symbol. And of course the Environment object is always available as a Spring bean.

Also note that Environment has the method getActiveProfiles() which returns an array of Strings (that is why [0] is used in my answer) which we can call using standard Spring EL.

If more than one profiles are active at a time, a more robust solution would be to use Thymeleaf's #arrays utility object in order to check for the presence of the string production in the active profiles. The code in that case would be:

<div th:if="${#arrays.contains(@environment.getActiveProfiles(),'production')}">      This is the production profile </div> 
like image 164
geoand Avatar answered Sep 20 '22 07:09

geoand


Simply add this class which allows to set global variables for views:

@ControllerAdvice public class BuildPropertiesController {      @Autowired     private Environment env;      @ModelAttribute("isProd")     public boolean isProd() {         return Arrays.asList(env.getActiveProfiles()).contains("production");     } } 

And then use ${isProd} variable in your thymeleaf file:

<div th:if="${isProd}">      This is the production profile </div> 

Or you can set active profile name as a global variable:

@ControllerAdvice public class BuildPropertiesController {      @Autowired     private Environment env;      @ModelAttribute("profile")     public String activeProfile() {         return env.getActiveProfiles()[0];     } } 

And then use ${profile} variable in your thymeleaf file (if you have one active profile):

<div>      This is the <span th:text="${profile}"></span> profile </div> 
like image 38
Igorock Avatar answered Sep 20 '22 07:09

Igorock