Spring boot

  1. Using profiles in spring boot application
  2. Passing variables from controller to thymeleaf html
  3. Passing variables from controller to javascript file via thymeleaf template
  4. Embedded Tomcat access logs

Using multiple profiles

The application can be compiled based on the deployment environment. The way to do that is The application yaml files will look as follows:

The application.yaml file with general information, active profile set to prod_pg:

The specific environment file, e.g. application-prod_pg.yaml will have the variables specific to production, postgress:

The specific environment file, e.g. application-dev_pg.yaml will have the variables specific to development, postgress:

These variables can now be referred to in the application and be dependent on the profile you are compiling with:

@Value("${websiteUrl}")
private String websiteUrl;

Passing variables from controller to thymeleaf html

Generate the variables in controller and pass them as model attributes

@Controller public class HomeController { @Value("${env}") private String env; @Value("${baseUrl}") private String baseUrl; @GetMapping("/") public String home(Model model) { String cssUrl = baseUrl + "/css/"; String jsUrl = baseUrl + "/js/"; String theme = "light"; String company = "cba"; String bundle = "bundle"; String endpoints = "endpoints"; //CSS files model.addAttribute("cssFileName1", cssUrl + theme + ".css"); model.addAttribute("cssFileName2", cssUrl + company + ".css"); //Js files model.addAttribute("jsFileName1", jsUrl + bundle + ".js"); model.addAttribute("jsFileName2", jsUrl +endpoints + ".js"); //Js variables model.addAttribute("baseUrl", baseUrl); String pageTitle = "Store Application"; model.addAttribute("pageTitle", pageTitle); return "index"; }

Using the passed variables in the thymeleaf annotations in the index.html of the template directory for dynamic paths for css and javascript files .


Thymleaf will create the literal values from the template when you pass these variables

<link rel="stylesheet" th:href="@{{fileName}(fileName=${cssFileName1})}"> <link rel="stylesheet" th:href="@{{fileName}(fileName=${cssFileName2})}"> <script type="module" th:src="@{{fileName}(fileName=${jsFileName1})}"> <script th:src="@{{fileName}(fileName=${jsFileName2})}"></script> <title th:text="${pageTitle}"></title>

Passing variables from controller to javascript file via thymeleaf template

The variable from controller can be passed as global variable in html template using inline directive.
The variable becomes global and could be used by any javascript code included after the declaration.
<script th:inline="javascript"> /*[+ var baseUrl = [[${baseUrl}]]; +]*/ </script> <script th:src="@{{fileName}(fileName=${jsFileName2})}"></script>

Embedded Tomcat logging enabled in springboot

To enable the embedded tomcat logging add the following lines to application.properties file
spring.application.name=yourapp
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
server.tomcat.basedir=tomcat
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.pattern=common

top of the page