Sunday, September 15, 2019

Spring MVC Tutorial

A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.
A Spring MVC provides an elegant solution to use MVC in spring framework by the help of DispatcherServlet. Here, DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controllers, models, and views.

Spring Web Model-View-Controller

Spring MVC Tutorial
  • Model - A model contains the data of the application. A 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

Spring MVC Tutorial
  • 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.

Advantages of Spring MVC Framework

Let's see some of the advantages of Spring MVC Framework:-

  • Separate roles - The Spring MVC separates each role, where the model object, controller, command object, view resolver, DispatcherServlet, validator, etc. can be fulfilled by a specialized object.
  • Light-weight - It uses light-weight servlet container to develop and deploy your application.
  • Powerful Configuration - It provides a robust configuration for both framework and application classes that includes easy referencing across contexts, such as from web controllers to business objects and validators.
  • Rapid development - The Spring MVC facilitates fast and parallel development.
  • Reusable business code - Instead of creating new objects, it allows us to use the existing business objects.
  • Easy to test - In Spring, generally we create JavaBeans classes that enable you to inject test data using the setter methods.
  • Flexible Mapping - It provides the specific annotations that easily redirect the page.

Spring Web MVC Framework Example

Let's see the simple example of a Spring Web MVC framework. The steps are as follows:
  • Load the spring jar files or add dependencies in the case of Maven
  • Create the controller class
  • Provide the entry of controller in the web.xml file
  • Define the bean in the separate XML file
  • Display the message in the JSP page
  • Start the server and deploy the project

Directory Structure of Spring MVC

Spring MVC Tutorial

Directory Structure of Spring MVC using Maven

Spring MVC Tutorial

Required Jar files or Maven Dependency

To run this example, you need to load:
  • Spring Core jar files
  • Spring Web jar files
  • JSP + JSTL jar files (If you are using any another view technology then load the corresponding jar files).
If you are using Maven, you don't need to add jar files. Now, you need to add maven dependency to the pom.xml file.

1. Provide project information and configuration in the pom.xml file.

pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.javatpoint</groupId>  
  5.   <artifactId>SpringMVC</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>SpringMVC Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junit</groupId>  
  13.       <artifactId>junit</artifactId>  
  14.       <version>3.8.1</version>  
  15.       <scope>test</scope>  
  16.     </dependency>  
  17.       
  18.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  19. <dependency>  
  20.     <groupId>org.springframework</groupId>  
  21.     <artifactId>spring-webmvc</artifactId>  
  22.     <version>5.1.1.RELEASE</version>  
  23. </dependency>  
  24.   
  25. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  26. <dependency>    
  27.     <groupId>javax.servlet</groupId>    
  28.     <artifactId>servlet-api</artifactId>    
  29.     <version>3.0-alpha-1</version>    
  30. </dependency>  
  31.   
  32.   </dependencies>  
  33.   <build>  
  34.     <finalName>SpringMVC</finalName>  
  35.   </build>  
  36. </project>  

2. Create the controller class

To create the controller class, we are using two annotations @Controller and @RequestMapping.
The @Controller annotation marks this class as Controller.
The @Requestmapping annotation is used to map the class with the specified URL name.
HelloController.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. @Controller  
  5. public class HelloController {  
  6. @RequestMapping("/")  
  7.     public String display()  
  8.     {  
  9.         return "index";  
  10.     }     
  11. }  

3. Provide the entry of controller in the web.xml file

In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>SpringMVC</display-name>  
  4.    <servlet>    
  5.     <servlet-name>spring</servlet-name>    
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  7.     <load-on-startup>1</load-on-startup>      
  8. </servlet>    
  9. <servlet-mapping>    
  10.     <servlet-name>spring</servlet-name>    
  11.     <url-pattern>/</url-pattern>    
  12. </servlet-mapping>    
  13. </web-app>  

4. Define the bean in the xml file

This is the important configuration file where we need to specify the View components.
The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.
This xml file should be located inside the WEB-INF directory.
spring-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.   
  14.     <!-- Provide support for component scanning -->  
  15.     <context:component-scan base-package="com.javatpoint" />  
  16.   
  17.     <!--Provide support for conversion, formatting and validation -->  
  18.     <mvc:annotation-driven/>  
  19.   
  20. </beans>  

5. Display the message in the JSP page

This is the simple JSP page, displaying the message returned by the Controller.
index.jsp
  1. <html>  
  2. <body>  
  3. <p>Welcome to Spring MVC Tutorial</p>  
  4. </body>  
  5. </html>  
Output:
Spring MVC Tutorial

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...