Spring MVC
By: Suvam Banerjee
Tags: Spring, Java

Spring MVC framework is an architecture that uses Model View and Controller components which is build on core spring. MVC provides flexibility and loosely coupled web components.

Model: It acts as a data carrier between controller, database, and view. Basically, these are normal POJO. Property of POJO is bound with form attributes which fetch data from the view and deliver it to the controller and then database.

View: These are Html or JSP pages that are rendered on the client-side with appropriate model data in them.

Controller: Controller is responsible to accept client request and bypass it to the appropriate service and after that generated model to proper view page. In spring MVC a front controller is present know as Dispatcher servlet which is the first controller that interact directly with the client and is responsible to bypass request to sub-controllers.

Controllers are nothing special but it's just a java class that is decorated with @Controller annotation on top of the class and can have request mapping based on which dispatcher servlet recognizes a controller.

Its methods are again decorated with @RequestMapping(/URL..) based on URL these methods are invocked. We can also specify the HTTP get or post method using request mapping. Controller request mapping and its method request mapping acts like a folder and its subfolder eg: http:Xyz/controllerMapping/methodMapping , we can give any method name for request mapping.

eg:@Controller

public class HelloWorld{

@RequestMapping(value = “/hello”)

public String print(ModelMap model) {

model.addAttribute(“message”, “Hello Spring MVC Framework!”);

return “hello”; }

}

public class HelloWorld{ @RequestMapping(value = “/hello”) public String print(ModelMap model) { model.addAttribute(“message”, “Hello Spring MVC Framework!”); return “hello”; } } In place of @RequestMapping we can use @GetMapping and @PostMapping for HTTP post or get requests, and handler methods name can be same for both mapping in the same controller class.

In the above example, the return statement is the most interesting part of spring MVC, that returned string is the view page name. In a normal spring MVC project, we need to specify prefix and suffix that will be appended with a returned string which will create an exact path, and the exact view page name and a model will be attached with it using the model.addAttribute(key, value).

configuration for prefix and suffix for view

<bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name=”prefix” value=”/WEB-INF/view/” /> <property name=”suffix” value=”.html” /> </bean> <mvc:view-controller path=”/” view-name=”index” />

creating a view:

<html>

<head>

<title>Hello Spring MVC</title>

</head>

<body>

<h2>${message}</h2>

</body>

</html>

${message} is an attribute that came from controller and we are binding that with html page using $ and attribute name.

Advantages of using spring mvc:

1)Code reusability

2)Flexible and easy mapping of controllers by using annotations

2) Spring MVC separates each role, where the model object, controller, view resolver, DispatcherServlet, validator, dao, etc.