Spring Web MVC
Tags: spring, web, mvc, java
Published: 2020-11-09 06:06:53

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](https://dev-to-uploads.s3.amazonaws.com/i/jp9uqkisgu4up4mkz8p8.png)

#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.

```java
@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) {
		...
	}
}
```

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](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-methods)

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>
```

And this would be the welcome.html file:

```html
<!DOCTYPE html>
<html lang="en">
<body>
	<div>Hello {{name}}</div>
</body>
</html>
```

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