This tutorial walks you through the process of creating a Hello World example web site with Spring MVC XML Configurations and Thymeleaf

What you'll build

What you'll need

  • JDK 1.8+
  • Maven 3+

Stack

  • Spring MVC 5
  • Thymeleaf View Template

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── springmvc
│       │               └── HelloController.java
│       ├── resources
│       │   ├── application.properties
│       │   └── logback.xml
│       └── webapp
│           ├── WEB-INF
│           │   ├── views
│           │   │   └── hello.html
│           │   ├── appconfig-mvc.xml
│           │   ├── appconfig-root.xml
│           │   └── web.xml
│           └── resources
│               ├── css
│               │   └── main.css
│               └── js
│                   └── main.js
└── pom.xml

Project dependencies


Define Controller and View Template

Hello Controller

Controller maps HTTP Requests with View.


@RequestMapping maps /hello request to hello() method.

name is a query string parameter of /hello request.

Model object passes value to hello view (hello.html).

Thymeleaf View Template

Thymeleaf performs server side rendering for the HTML content. It parses the hello.html template below and evaluate the Spring EL (Expression Language) to render the value of ${name} parameter that was set in the controller.


Static files



Config and Run

XML Configurations

An application usually has several XML configuration files, but there should only one bootstrap file (appconfig-root.xml). This bootstrap file should use the <import resource="" /> to include other config files.




Run with Maven and Jetty

Type the below command at the project root directory and visit to localhost:8080

mvn clean jetty:run

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/spring-mvc-examples/springmvc-xml-thymeleaf