tags | projects | |||||||
---|---|---|---|---|---|---|---|---|
|
|
This guide shows how to use Spring Boot’s Command Line Interface to create a rich application with a backend support by Spring MVC and a web-based front end using Thymeleaf template engine and jQuery.
You’ll build a web application using Groovy and Spring while also using Thymeleaf and jQuery.
Spring Boot comes with a Command Line Interface. There are several ways to install it.
You can install it via gvm:
curl -s get.gvmtool.net | bash
With gvm installed, you then install the CLI:
gvm install springboot spring --version
If you are using a Mac, you can also the CLI using Homebrew.
brew tap pivotal/tap brew install springboot spring --version
With either of these options, you are now in business to create a Spring application with minimal effort.
Now create the simplest application possible.
app.groovy
link:app.groovy[role=include]
This Spring MVC controller defines a REST endpoint at /greeting
. We’ll write the template for that later in this guide.
Let’s demonstrate this using a jQuery animation. We first need to grab a copy of jQuery. The simplest option would to add a Groovy @Grab annotation to the top of your application that automatically fetches it.
link:app.groovy[role=include]
Note
|
Feel free to read more about Spring Boot + webjars. |
Let’s create a Thymeleaf template to support our Spring MVC endpoint.
mkdir templates
Now add the following template:
templates/greeting.html
link:templates/greeting.html[role=include]
Note
|
Because we used webjars, jQuery is found at webjars/jquery/<version>/<library> .
|
Now launch the application.
spring run -cp . app.groovy
See the animation by visiting http://localhost:8080/greeting.
This almost empty application comes preloaded with a lot of features thanks to Spring Boot.
-
@Controller gives a signal that this is a Spring MVC application and Boot will launch an embedded Tomcat servlet container.
-
Several paths are autoloaded for serving assets(see webjars and static web content blog entries for more details). That’s why we placed our web template in the
/templates
folder.
From here you can add RESTful endpoints to the backend, work on your front end, and grow your application as needed.
Spring Boot CLI provides a rapid way to create a back end server app. It also supports plugging in your favorite Javascript resources and HTML templates. You also could have staged CSS assets. Bottom line: you don’t have to create a project file. Instead, you can dive quickly into building your application.