Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

Spring, Spring App, Spring MVC, and Thymeleaf

Notes taken while reading the reference materials.

References

Serving Web Content by spring.io guides

Thymeleaf by thymeleaf.org docus

Springboot

Requirements to build a simple website:

  1. An IDE
  2. JDK 1.8 or later
  3. Gradle 4.0+ or Maven 3.2+

Helpful Utilities:

Architecture Comments

Drop all static resource files into subfolder /static or /public
The default index page is used (by default) as the root or home page of the webapp.
Spring uses Controllers to render pages (like MVC). Annotation: @Controller.
Each Controller defines a 'Route'.
HTTP Web Requests are trapped and directed to "method_name" using annotation @GetMapping.
Annotation @RequestParam binds QueryString value to a named parameter e.g. @RequestParam(name="username", required=false, defaultView=String:default_value).
ThymeLeaf performs server-side rendering of the HTML.
String attributes are passed to the HTML file via elements with attribute th:text (expression).

Spring Boot Devtools

Enables:

Devtools is not enabled in a JAR, but can be enabled by setting properties in Maven or Gradle files e.g. 'excludeDevtools=false'

Setup

Utilize Spring Initializr: Select Project, Language, and Boot version, JAR/WAR file generation, and JDK version. Can also add "Dependencies". Generates a downloadable Zip file.

Dev Overview

Controllers Templates

Gradlew

Creating and Running a Jar File

./gradlew bootrun and ./gradlew build => Return an executable JAR file.
JAR files are stored in the Project path 'build/libs/application-name-version.jar'

Thymeleaf

The following is a very high-level overview of Thymeleaf, based on the paged linked in References, above.

Controllers

Website paths, model data, and template views are considered a 'model map'.
Thymeleaf takes a model map and transforms it into a Thymeleaf context object.

Attributes

Model Attributes are data that can be accessed during view execution.
Context Variables are the Thymeleaf equivalent of Model Attributes.

Parameters

Request parameters can be viewed with params dot notation.
Multi-valued Request Params can be accessed using bracket notation.

Session Attributes

Session atributes are accessed using session.
Get direct access to 'java.servlet.http.HttpSession' object by using #session

Servlet Context Attributes

Shared between Requests and Sessions.
Access them using #servletContext.method

Spring Beans

Beans registered with the Spring Application Context carry methods defined in @Configuration.
Use Beans in the HTML via the 'th:text=` attribute.

Return to Parent Readme.md