This post walks you through the process of creating a Hello World example web site with Spring MVC.

Hello Spring MVC Series:
- Spring MVC Hello World Example with XML Configuration, Maven and JSP
- Spring MVC Hello World Example with XML Configuration, Maven, and Thymeleaf

What you'll build

What you'll need

  • JDK 1.8 or later
  • Maven 3 or later

Stack

  • Spring MVC 4
  • Thymeleaf

Project structure

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

Project dependencies


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 of the HTML. 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.


XML Configuration

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




Run

mvn jetty:run

Source code

[email protected]:hellokoding/hello-springmvc-xml-maven-thymeleaf.git https://github.com/hellokoding/hello-springmvc-xml-maven-thymeleaf

You may also like the Hello Spring Boot Series:
- Spring Boot Hello World Example with FreeMarker
- Spring Boot Hello World Example with Thymeleaf
- Spring Boot Hello World Example with JSP

Serving Web Content with Spring MVC