Spring MVC

Spring Web Model-View-Controller

  • Model — A model contains the data of the application. Data can be a single object or a collection of objects.
  • Controller — A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
  • View — A view represents the provided information in a particular format. Generally, JSP+JSTL is used to create a view page. Although spring also supports other view technologies such as Apache Velocity, Thymeleaf, and FreeMarker.
  • Front Controller — In Spring Web MVC, the DispatcherServlet class works as the front controller. It is responsible to manage the flow of the Spring MVC application.

Understanding the flow of Spring Web MVC

  • As displayed in the figure, all the incoming request is intercepted by the DispatcherServlet that works as the front controller.
  • The DispatcherServlet gets an entry of handler mapping from the XML file and forwards the request to the controller.
  • The controller returns an object of ModelAndView.
  • The DispatcherServlet checks the entry of view resolver in the XML file and invokes the specified view component.

Model

package com.myApp.spring.model;public class User {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
@Controller
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
  • You will define the required business logic inside a service method. You can call another method inside this method as per requirement.
  • Based on the business logic defined, you will create a model within this method. You can use setter different model attributes and these attributes will be accessed by the view to present the final result. This example creates a model with its attribute “message”.
  • A defined service method can return a String, which contains the name of the view to be used to render the model. This example returns “hello” as a logical view name.
<html>
<head>
<title>Hello Spring MVC</title>
</head>

<body>
<h2>${message}</h2>
</body>
</html>
  1. Clear separation of roles:- Each role like a controller, DispatcherServlet can be fulfilled by a specialized object.
  2. Adaptability, non-intrusiveness, and flexibility:- Define any controller method signature you need. (Such as @RequestParam)
  3. Customizable binding and validation:- Type mismatches as application-level validation errors that keep the offending value, localized date, and number binding, and so on instead of String-only form objects with manual parsing and conversion to business objects.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store