DEV Community

eidher
eidher

Posted on • Updated on

Spring Web MVC

Spring MVC is a web framework based on the model–view–controller pattern. It is based on the Spring principles and supports a wide range of server-side rendering technologies as JSP, Thymeleaf, Groovy, among others.

Alt Text

Dispatcher Servlet

It is a front controller that coordinates all the request handling activities. Delegates to the web infrastructure beans and invokes the user's web components. It is automatically created and configured by Spring Boot. If you are not using Spring Boot, you need to add a ViewResolver bean definition and the @EnableWebMvc annotation.

Controller

Annotate controllers with @Controller and annotate methods in the controller with @RequestMapping or @GetMapping to tell Spring what method to execute when processing a particular HTTP GET request. Controllers typically return a logical view name as String.

In the next example when calling http://localhost:8080/ it would be redirected to index.html and when calling http://localhost:8080/hello it would be redirected to welcome.html.

@Controller
public class HelloController {

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @RequestMapping("/hello")
    public String hi(Model model) {
        model.addAttribute("name", "Spring Boot");
        return "welcome";
    }

    @RequestMapping("/showAccount")
    public String accountDetails(@RequestParam("entityId") long id, Model model) {
        ...
    }

    @GetMapping("/accounts/{accountId}")
    public String show(@PathVariable("accountId") long accountId, Model model) {
        ...
    }
}
Enter fullscreen mode Exit fullscreen mode

The controller method parameters are provided by Spring. In the previous example, the model parameter is used for sending data to the view. You can use HttpServletRequest for the request, HttpSession for session access, Principal for the authenticated user, etc. See Handler Methods Documentation

Use the @RequestParam annotation to extract parameters from the request. In the above example, you can send the entityId parameter calling to http://localhost:8080/showAccount?entityId=1.

To extract the value from the request URL, you can use the {...} placeholders and the @PathVariable annotation. In the above example, you can send the accountId parameter calling to http://localhost:8080/accounts/1. In this case, the annotation value ("accountId") after the @PathVariable is unnecessary because it matches the parameter name.

View

A view renders web output. It could be an HTML or JSP file. ViewResolvers select the view based on the view name returned by the controller. In Spring Boot, you just need to add the dependency for the ViewResolver (Mustache, Thymeleaf, Groovy, etc)

For instance, this would be the dependency for mustache:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

And this would be the welcome.html file:

<!DOCTYPE html>
<html lang="en">
<body>
    <div>Hello {{name}}</div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

According to the controller, this would print "Hello Spring Boot", taking the name attribute from the model.

Top comments (0)