Spring MVC architecture uses the “FrontController” design pattern which is fundamental to any MVC design implementation. The DispatcherServlet is at the heart of this design whereby HTTP requests are delegated to the controller, views are resolved to the underlying view technology, in addition to providing support for uploading files. The DispatcherServlet like any regular servlet can be configured along with custom handler mappings.
Spring MVC framework enables separation of modules namely Model, View, and Control and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using Maps. Additionally, validation for types mismatch and two-way data-binding support in the user interface is provided. Easy form-submission and data-binding in the user interface are possible with spring form tags, model objects, and annotations.
The article discusses the steps involved in developing a Spring web MVC application, explaining the initial project setup for an MVC application in Spring. JSP(Java Server Pages) is used as a view technology.
The following are the dependencies for Spring web MVC. While spring-web MVC jar would suffice for all container requirements to develop the MVC application, JSTL-jar is included for JSP:
Dependencies within pom.xml
The DispatcherServlet would be configured in web.xml as follows. The listener class ContextLoaderListener would load the root application context and transfer the handle to dispatcher servlet mentioned in the servlet-mapping element. All requests corresponding to the ‘/’ URL mapping would be handled by this dispatcher servlet.
The related WebApplicationContext for a dispatcher servlet can be found in the default location ‘servletName’-servlet.xml using the context-param contextConfigLocation. The WebApplicationContext contains the MVC-specific configurations including view-resolvers, datasource, messagesource, multipart-resolver (file-upload), etc.
In the above XML, the element component-scan with context namespace will scan all the component classes for annotations(@Component, @Configuration, @Service, @Repository, @Autowired) in the base packages and also initialize the beans. The element annotation-driven within the MVC namespace will scan and initialize @Controller annotated components.
The element default-servlet-handler, within MVC namespace, extends the default HTTP dispatcher servlet to also serve requests for static resources located in the web-root.
We have also initialized a bean with id ‘viewProvider’ for rendering .jsp pages. The class InternalResourceViewResolver would accept the prefix and suffix parameters to construct the path to view page. For example, if the controller class returned a view named as ‘greet’ then the view-resolver class would identify the path relative to web-root as “/greet.jsp” by appending the given suffix and prefix strings.
Finally, the .jsp file which will be the view. We are sending an attribute from controller class to the view which will be displayed upon hitting the matching URL.
The controller class code as follows.
Please check the view below which displays the string “greeting” value provided in the controller.
- Spring Boot | How to access database using Spring Data JPA
- Introduction to Model View View Model (MVVM)
- Introduction to Spring Boot
- Spring MVC using Java based configuration
- How to create a REST API using Java Spring Boot
- How to create a basic application in Java Spring Boot
- Spring Boot | How to publish JSON messages on Apache Kafka
- Spring Boot | How to publish String messages on Apache Kafka
- Laravel | View Basics
- How to Setup View Engine in Node.js ?
- HTML | view Event Property
- Reader's View of a GeeksforGeeks webpage
- How to Setup Handlebars View Engine in Node.js ?
- How to insert HTML into view from AngularJS controller?
- Different ways for passing data to view in Laravel
- Model-View-Controller(MVC) architecture for Node applications
- How to define additional details that the user can view or hide?
- How to place the image above the slider in mobile view in bootstrap?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.