The Spring Web MVC framework helps us develop a flexible web application. It provides Model-View-Controller (MVC) architecture and ready components. In addition, the MVC pattern divides the application based on different aspects (input logic, business logic, and UI logic). It also provides a loose coupling between these elements.
- The Model is the input logic, that encapsulates the application data, and in general, they will consist of POJO.
- The View is the UI logic, that renders the model data and generates HTML output that the client’s browser can interpret.
- The Controller is the business logic, that processes user requests and builds an appropriate model, and passes it to the view for rendering.
- DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controller, model, and view.
- Model consists of the data of the application. Data can be either a single object or a collection of multiple objects.
- Controller is responsible for the business logic of an application. @Controller annotation marks the class as a controller.
- View represents the information in a specific format. Generally, to create a view page, we use JSP+JSTL.
- Front Controller is nothing but the DispatcherServlet class. It is responsible for managing the entire flow of the Spring MVC application.
The flow of Spring MVC
- DispatcherServlet consults the HandlerMapping to call the appropriate Controller after receiving an HTTP request.
- The request goes to the Controller, which then calls the appropriate service methods based on the used GET or POST method. Based on defined business logic, the service method will set model data and returns view name to the DispatcherServlet.
- The DispatcherServlet takes help from ViewResolver to pickup the defined view for the request.
- Once the view is final, the DispatcherServlet passes the model data to the view. Finally, it renders the view on the browser.
How to create a Spring MVC project ?
- Load the spring jar(Spring Core, Spring Web, JSP + JSTL) files or add dependencies in the case of Maven to pom.xml file.
- Create a controller class
- Provide entry of this controller in the web.xml file (To map requests that you want the DispatcherServlet to handle, by using URL mapping with <servlet-mapping> tag)
- Define the bean in a separate XML file
- Display the message on the JSP page
- Start the server and deploy the project
Some annotations used in Spring MVC
Annotation | Usage |
---|---|
@Controller | It indicates that a particular class serves the role of a controller. |
@RequestMapping | It is used to map a URL to either an entire class or a particular handler method. |
@RequestParam | It is used to read the form data and bind it automatically to the parameter present in the provided method. |
@EnableWebMvc | It is used to enable Spring MVC support through a Java configuration class. |
@PathVariable | It is used to show that a method parameter is bound to a URI template variable. |
@SessionAttribute | It is used to pass value across different requests through the session. |
@RequestBody | It is used to show a method parameter bound to the body of the web request |
@ResponseBody | It is used to show a method return value bound to the web response body. |
@ExceptionHandler | It is used to handle exceptions thrown by request handling. |
@CrossOrigin | It is used to enable cross-origin resource sharing only for this specific method. |
@Repository | It is used to make the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException by importing the DAOs into the DI container. |
@Service | It’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better. |