In this chapter, we will learn how to create a sample Thymeleaf-based project to demonstrate the capabilities of Spring CLI. Follow the below mentioned step to create a sample project −
Sr.No | 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. |
@Controller @Grab('spring-boot-starter-thymeleaf') class MessageController { @RequestMapping("/message") String getMessage(Model model) { String message = "Welcome to Howcodex.Com!"; model.addAttribute("message", message); return "message"; } }
<!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>
<!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>
To 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)
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 the Message link and you will see the following output −
Message − Welcome to Howcodex.Com!
Consider the following points to understand the actions taken by Spring CLI −
The @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 have not specified any group id or version id here.
Finally after code compilation, deploy the war on a embedded tomcat, start embedded tomcat server on the default port 8080.