Spring MVC is a Java framework that is used to develop flexible and loosely coupled web applications. It follows the MVC (Model View Controller) pattern.
It follows all the basic features of core spring like Dependency Injection, Inversion Control, etc.
MVC can be used in the Spring framework using DispatcherServlet. DispatcherServlet is a class that maps incoming requests to the right resources.
- Model: It encapsulates the data of the application, which can be a single object or a collection of objects.
- View: It represents the information in a particular format. JSP + JSTL is used to create views.
- Controller: It contains the business logic of the application.
- Front Controller: DispatcherServlet is used as a front controller.
How Spring MVC works?
- The incoming request is first received by the DispatcherServlet.
- The DispatcherServlet then consults the HandlerMapping and forwards the request to the Controller.
- The Controller calls the appropriate service methods and returns an object of ModelAndView containing the model data and view name.
- To find the View, the DispatcherServlet sends the view name to the ViewResolver.
- The DispatcherServlet now passes the model object to the View.
- The View then returns the result to the user.
Advantages of Spring MVC:
- Separate roles: It separates each role as a model object, controller, ViewResolver, DispatcherServlet, etc.
- Light weight: Spring MVC uses light weight servlet container.
- Rapid Development: It provides a fast and parallel development.
- Reusable Business code: Existing codes can be reused in the application.
- Flexible Mapping: It contains some specific annotations for flexible mapping.
- Easy to test: Spring MVC contains Java Beans, which makes it easy to test the application.
- Powerful Configuration: It gives configuration for the framework as well as application classes.
Steps to create a Spring MVC project:
- Either load the jar files (Spring core jar file, Spring web jar file, JSP + JSTL jar file) or add maven dependencies.
- Create a controller class.
- The entry of the controller should be provided in the web.xml file.
- In a separate XML file, define Bean.
- Output the message in a JSP file.
- Start the server and deploy the project.
Example of Spring MVC:
- HelloWorldController Class
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HelloWorldController extends AbstractController { private String message; @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("welcomePage","welcomeMessage", message); } public void setMessage(String message) { this.message = message; } }
2. Entry of controller class in web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app>
3. Define bean in the xml file
<bean id="viewResolver" class=" org.springframework.web.servlet.view. InternalResourceViewResolver" > <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean name="/welcome.htm" class="com.vaannila.HelloWorldController" > <property name="message" value="Hello World!" /> </bean> </beans>
4. Displaying message in JSP page i.e. index.jsp
<html> <body> <p>Welcome to Spring MVC Tutorial</p> </body> </html>
OUTPUT:
The files that are required to execute the above example are:
antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.context.support-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
org.springframework.web-3.0.0.M3
org.springframework.web.servlet-3.0.0.M3
One Response