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

What you'll build

What you'll need

  • JDK 1.8+
  • Maven 3+

Stack

  • Spring MVC 5
  • JSP 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.jsp
│           │   ├── 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 JSP view template files.


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

name is a query string parameter of /hello request.

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

JSP/JSTL View Template

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


Static files



Config and Run

XML Configurations

A Spring MVC XML application usually has several XML configuration files, but there should have 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-jsp