How to get the current environment in Thymeleaf

Sometimes you want to include a specific piece of code to rendered HTML only based on the environment in which the current application is running. A simple example is the Google Analytics tracking code that you only want to be added to the production environment.

Thymeleaf allows us to access the current active Spring application profile to hide or show elements depending on the runtime configuration. In this article, you'll learn how to access the Spring Environment bean in a Thymeleaf template to be used in a conditional statement.

Accessing Environment Bean

In Thymeleaf, you can access all available Spring beans using the @ symbol. Environment is a special Spring bean that represents the current application environment. You can use it to get the currently active profile and then control the elements visibility accordingly.

The Environment bean provides the getActiveProfiles() method that returns an array of strings containing the names of the current active profiles. Profiles in Spring Boot are used to register the beans conditionally, based on the deployment environment.

The following example demonstrates how you can access the Environment bean in Thymeleaf template and display a text only if the current active profile is prod:

<div th:if="${@environment.getActiveProfiles()[0] eq 'prod'}">
    This is the production environment!
</div>

Now if you run the application with the -Dspring.profiles.active=prod command-line argument, you should see the above text displayed on the screen.

Alternatively, you can also specify the current active profile directory in the application.properties or application.yml class by adding the following line:

spring.profiles.active=prod

If more than one profiles are active at a time, a better solution would be to use Thymeleaf's #arrays utility object to check for the presence of the string prod in the active profiles as shown below:

<div th:if="${#arrays.contains(@environment.getActiveProfiles(), 'prod')}">
    This is the production environment!
</div>

Using Environment bean, you can also access other application properties. Let us say you have the following properties defined in your application.properties file:

com.attacomsian.security-salt=3ax5s4Tw!34Sfs4ldd445345
com.attacomsian.failed-login-attempts=4
com.attacomsian.deployment-key=4534VRS

To access the above properties in a Thymeleaf template, you should call the @environment.getProperty() method as shown below:

table>
    <thead>
    <tr>
        <th>Property Name</th>
        <th>Property Value</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Security Salt</td>
        <td th:text="${@environment.getProperty('com.attacomsian.security-salt')}"></td>
    </tr>
    <tr>
        <td>Failed Login Attempts Allowed</td>
        <td th:text="${@environment.getProperty('com.attacomsian.failed-login-attempts')}"></td>
    </tr>
    <tr>
        <td>Deployment Key</td>
        <td th:text="${@environment.getProperty('com.attacomsian.deployment-key')}"></td>
    </tr>
    </tbody>
</table>

Conclusion

In this article, we looked at how to access the current environment in a Thymeleaf template to conditionally show or hide elements of an HTML document. Spring Boot provides an Environment bean that can be used to access the current active profiles as well as other configuration properties. In Thymeleaf, you can access this bean by using the @ symbol, for example, @envionment.

Read Next: How to use Thymeleaf with Spring Boot

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.