Skip to content
thesarfo

Concept

Spring Start Here: Implementing a Web App with Spring MVC

Wiring up a @Controller with @RequestMapping, serving Thymeleaf templates, and the four ways to get data from an HTTP request — parameters, headers, path variables, and the body.

views 0

Getting started with Spring MVC

Say we want a static web page with some content displayed in the browser. First write an HTML document with the content, then write a controller with an action for that page.

To mark a class as a controller, use @Controller — Spring adds a bean of this class to its context to manage it. Inside, you define controller actions: methods associated with specific HTTP requests.

To have the browser display the page when a user visits /home, annotate the action method with @RequestMapping("/home"). The method returns, as a string, the name of the document to send as a response.

@Controller
public class MainController {
@RequestMapping("/home")
public String home() {
return "home.html";
}
}

We annotate the outer class as a Controller, then map the home method to the /home path, so it returns the static page home.html.

You can use Thymeleaf as a template engine for dynamic content — in that case put the HTML file in resources/templates instead of resources/static.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
<h1>Welcome <span th:style="'color:' + ${color}"
th:text="${username}"></span>!</h1>
</body>
</html>

Getting data from the HTTP request

Say you’re implementing a web forum where users can add and edit posts. The client sends post details to the server, which stores or changes them in a database — this requires the client to be able to send data to the server. There are four common ways to do that:

  1. Request parameter — a simple key-value(s) pair appended to the URI as a query expression (also called a query parameter). Use this only for small amounts of data.
  2. Request header — similar to a request parameter but sent via the HTTP header instead of the URI. Still not suited for large quantities of data.
  3. Path variable — sends data through the request path itself. Like request parameters, this is for small amounts of data, but use it specifically when the value is mandatory.
  4. HTTP request body — used to send larger quantities of data (as a string, or even binary data like a file). Mostly used for REST endpoints.

Using a request parameter

Useful for optional data — a common case is search/filter criteria, where the client sends only some parameters and the server handles missing ones gracefully. To read a request parameter, add a parameter to the controller’s action method and annotate it with @RequestParam — Spring reads the value from the HTTP request parameter with the same name.

@Controller
public class MainController {
@RequestMapping("/home")
public String home(
@RequestParam(required = false) String color,
Model page) {
page.addAttribute("username", "Katy");
page.addAttribute("color", color);
return "home.html";
}
}

We add a color parameter annotated with @RequestParam, plus a Model parameter used to pass data from the controller to the view — here, the color the client sent. You can add more request params the same way.

Using a path variable

Also a way of sending data from client to server, but the value is embedded directly in the path rather than passed as a request parameter:

  • request parameter: http://localhost:8080/home?color=blue
  • path variable: http://localhost:8080/home/blue

There’s no key — you take the value from a precise position in the path. You can use more than one path variable, but avoid more than 2 (use request parameters instead beyond that). Path variables are for mandatory values, not optional ones.

@Controller
public class MainController {
@RequestMapping("/home/{color}")
public String home(
@PathVariable String color,
Model page) {
page.addAttribute("username", "Katy");
page.addAttribute("color", color);
return "home.html";
}
}

Define a path variable by naming it inside curly braces in the path, then mark the corresponding method parameter with @PathVariable — the parameter name must match the variable name in the path.