Kadavi / Dahling

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tags projects
JavaScript
spring-boot
groovy
cli
jquery
webjars
spring-boot

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.

What you’ll build

You’ll build a web application using Groovy and Spring while also using Thymeleaf and jQuery.

Install Spring Boot’s CLI

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.

Creating a Spring application

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.

Adding a Javascript library

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.

Writing a web front end

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>.

Running the application

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.

Summary

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.

About


Languages

Language:Shell 62.9%Language:Groovy 37.1%