English 中文(简体)
Spring Boot CLI - Starter Thymeleaf Project
  • 时间:2024-11-03

Spring Boot CLI - Starter Thymeleaf Project


Previous Page Next Page  

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

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

TestApppcation/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";
   }
}

TestApppcation/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> 

TestApppcation/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="/message">Message</a></p>
   </body>
</html> 

Run the apppcation

Type the following command


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

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


  .   ____          _            __ _ _
 /\ / ___ _ __ _ _(_)_ __  __ _    
( ( )\___ |  _ |  _| |  _ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
     |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-02-03 11:36:33.362  INFO 10764 --- [       runner-0] o.s.boot.SpringApppcation               : Starting apppcation using Java 11.0.11 on DESKTOP-86KD9FC with PID 10764 (started by intel in E:TestTestApppcation)
2022-02-03 11:36:33.372  INFO 10764 --- [       runner-0] o.s.boot.SpringApppcation               : No active profile set, falpng back to default profiles: default
2022-02-03 11:36:35.743  INFO 10764 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initiapzed with port(s): 8080 (http)
2022-02-03 11:36:35.768  INFO 10764 --- [       runner-0] o.apache.catapna.core.StandardService   : Starting service [Tomcat]
2022-02-03 11:36:35.769  INFO 10764 --- [       runner-0] org.apache.catapna.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-03 11:36:35.829  INFO 10764 --- [       runner-0] org.apache.catapna.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cp.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@553a3d88] of class [class org.springframework.boot.cp.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2022-02-03 11:36:35.896  INFO 10764 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initiapzing Spring embedded WebApppcationContext
2022-02-03 11:36:35.897  INFO 10764 --- [       runner-0] w.s.c.ServletWebServerApppcationContext : Root WebApppcationContext: initiapzation completed in 2085 ms
2022-02-03 11:36:36.436  INFO 10764 --- [       runner-0] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2022-02-03 11:36:37.132  INFO 10764 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path   
2022-02-03 11:36:37.153  INFO 10764 --- [       runner-0] o.s.boot.SpringApppcation               : Started apppcation in 4.805 seconds (JVM running for 8.633)
2022-02-03 11:37:03.049  INFO 10764 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initiapzing Spring DispatcherServlet  dispatcherServlet 
2022-02-03 11:37:03.049  INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initiapzing Servlet  dispatcherServlet 
2022-02-03 11:37:03.054  INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initiapzation in 3 ms

Browse the apppcation in Browser

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


Go to Message

Cpck on Message pnk 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 2.6.3 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