Spring Boot CLI - Starter Thymeleaf Project


Advertisements


Let's create a sample Thymeleaf based project to demonstrate the capabilities of Spring CLI. Follow the below mentioned step to create a sample project −

Step Description
1 Create a Folder with a name TestApplication with subfolders templates and static.
2 Create message.groovy in TestApplication folder, message.html in templates folder, index.html in static folder as explained below.
3 Compile and run the application to verify the result of the implemented logic.

TestApplication/message.groovy

@Controller
@Grab('spring-boot-starter-thymeleaf')
class MessageController {
   @RequestMapping("/message")
   String getMessage(Model model) {
      String message = "Welcome to TutorialsPoint.Com!";
      model.addAttribute("message", message);
      return "message";
   }
} 

TestApplication/templates/message.html

<!DOCTYPE HTML>
<html xmlns:th = "http://www.thymeleaf.org">
   <head> 
      <title>Spring Boot CLI Example</title> 
      <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" />
   </head>
   
   <body> 
      <p th:text = "'Message: ' + ${message}" />
   </body>
</html> 

TestApplication/static/index.html

<!DOCTYPE HTML>
<html>
   <head> 
      <title>Spring Boot CLI Example</title> 
      <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" />
   </head>
   
   <body>
      <p>Go to <a href = "/msg">Message</a></p>
   </body>
</html> 

Run the application

Type the following command −

E:/Test/TestApplication/> spring run *.groovy

Now Spring Boot CLI will come into action, download required dependencies, run the embedded tomcat, deploy the application and start it. You can see the following output on console −

Resolving dependencies.............................

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

...
2017-11-08 16:27:28.300  INFO 8360 --- [       runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-11-08 16:27:28.305  INFO 8360 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 4.203 seconds (JVM running for 38.792)

Browse the application in Browser

Our spring based rest application is now ready. Open url as "http://localhost:8080/" and you will see the following output −

Go to Message

Click on Message link and you will see the following output −

Message: Welcome to TutorialsPoint.Com!

Points to consider

Following actions are taken by Spring CLI −

  • @Grab('spring-boot-starter-thymeleaf') annotation directs CLI to download spring-boot-starter-thymeleaf 1.5.8.RELEASE version.

  • Spring CLI automatically detects the version using its metadata as we've not specified any group id or version id here.

  • Finally it compiles the code, deploy the war on a embedded tomcat, start embedded tomcat server on the default port 8080.



Advertisements