Spring MVC Framework – Integration of MVC with Spring

FREE Online Courses: Click, Learn, Succeed, Start Now!

1. Objective

In our last tutorial, we discussed Integration of Spring Logging with log4j  In this article, we will learn about the Spring MVC Framework – integration of MVC framework with the Spring Framework to develop an application. Using MVC framework you will develop flexible and loosely coupled Web App. You will also see a working example using Eclipse IDE.
So, let’s start Spring MVC Framework.

Spring MVC Framework - Integration of MVC with Spring

Spring MVC Framework – Integration of MVC with Spring

2. Spring MVC Framework

The Spring MVC framework uses Model View Controller(MVC) architecture. The MVC pattern separates the several aspects of the application such as input logic, UI logic, and business logic. It also uses MVC ready components that are used for developing flexible and loosely coupled web applications.
Let’s read about Spring Framework Architecture with 4 Modules
Spring MVC Framework is as follows:

  • The Model encapsulates app data which generally contains POJO.
  • The View is for rendering model data. Which in general generates HTML output that client browser can understand.
  • The Controller processes user requests and builds the appropriate model. Then it passes it to the View for rendering.

a. MVC Framework- The DispatcherServlet

The Spring MVC framework is made around DispatcherServlet which handles HTTP request and response. The following diagram illustrates the working of DispatcherServlet:

Spring MVC Framework

Spring MVC Framework – The DispatcherServlet

The mentioned components HandlerMapping, Controller and ViewResolver are parts of WebApplicationContext an extension of ApplicationContext.
The steps which are done to an incoming HTTP request to DispatcherServlet:

  • DispatcherServlet consults HandlerMapping to call Controller after receiving HTTP request.
  • The Controller takes request and calls appropriate methods based on GET and POST. The method will then set model data based on business logic. It also returns view name to DispatcherServlet.
  • The ViewResolver of DispatcherServlet picks up defined view for a request.
  • After the view gets finalized DispatcherServlet passes model data to view. This is then rendered on the browser.

Let’s Know about Spring JDBC Framework – JDBC Template with Eclipse IDE

b. Required Configuration

Using URL mapping in web.xml map the requests that you want DispatcherServlet to handle.
Consider the below example for mapping fir HelloWeb DispatcherServlet:

<web-app id = "WebApp_ID" version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>

Put the web.xml in WebContent/WEB-INF directory of your web application. Upon initialization of HelloWeb DispatcherServlet, the framework will try to load application context from [servlet-name]-servlet.xml present in WEB_INFdirectory.
If you don’t want [servlet-name]-servlet.xml as default filename and WebINF as default location add the ContextLoaderListener in your web.xml as:

<web-app...>
<!-------- DispatcherServlet definition goes here----->
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

Following is the required config for Hello-Web-servlet.xml present in WEB-INF directory:

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package = "com.example" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>

Some points about HelloWeb-servlet.xml:

  • The [servlet-name]-servlet.xml will be used for creating the beans defined and overriding the definitions of any beans with the same name.
  • The tag <context:component-scan..> is for activating Spring MVC annotation scanning capability.
  • The InternalResourceViewResolver has rules defined to resolve view naming.

Do you about How to Design Customised Events in Spring Framework
Now you will see how to develop MVC components Controller and View.

c. Creating the Controller

Here you will see how to define the controller. The @RequestMapping is for mapping a URL to handler method or to an entire class. The @Controller indicates a particular class serving a role of controller. Let’s see the example of defining controller:

@Controller
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}

Some things you should keep in mind for defining the controller. You should define business logic inside service method. Also, based on business logic you should create a model within this method which in this example a model is created with its attribute “message”. A defined service method can return the name of view as a string which will be used for rendering the model. In the example above “hello” is returned as View name.

d. Creating the JSP Views

The Spring MVC framework has many types of views such as JSP, HTML, XML, XSLT etc. Here you will see JSP for creating View which is a general case. The example is defined below in which ${message} is the attribute setup in the controller.

<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

So, this was all about Spring MVC Framework. Hope you like our explanation.

3. Conclusion

Hence, in this session of Spring MVC framework, you learned about the MVC model of Spring which is used for developing Spring Web application. The topic was discussed by splitting the components of MVC framework which is Model, View, and Controller. Each component was discussed in detail with providing the code in Eclipse IDE. The code developed was the HelloWorld Web application. At last, it gave you an idea how to integrate MVC framework with Spring. Furthermore, if have any query, feel free to ask in the comment section.
Related Topic- Spring Boot CLI
For reference 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *