JaiswalTraining

Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Spring MCV Module

  • MVC is another commonly used design pattern that can be applied to many different architectures (GUI, Web, etc…)
  • MVC dictates that the application is broken up as a Model to hold instance data, a View to define how to present the data, and a Controller to produce the Model and inject it into the View, along with application-wide tasks like validation, security and navigation.
  • When building MVC applications for the web, you could use a common framework like Struts or Spring, or you could write your own

How Does It Work
  • Spring creates a lightweight container that handles incoming HTTP requests by mapping them to Controllers, which return a Model and View that produces the HTTP response.
  • All configuration is done through an XML file, defining several individual beans and describing the relationships between them.
  • Create a Spring MVC application by adding spring.jar to an existing web application and creating the elements contained in the next several slides.
Spring MVC Basics

  • All calls go through the DispatcherServlet Config file is [servletname]-servlet.xml by default.
MVC: instances of the following:
M – Model: Eg. Java Bean,POJO etc
V – View: org.springframework.web.servlet.View
C – Controller: org.springframework.web.servlet.mvc.Controller





MVC for Web

Event – HTTP request from client
Controller – Custom Spring Controller Class
View – Eg. JSP
Model –Eg Java Beans

Model is passed to the server-side View which is returned to the client


Spring sequence diagram



DispatcherServlet
  • The DispatcherServlet is the Spring Front Controller
  • Initializes WebApplicationContext
  • Uses /WEB-INF/[servlet-name]-servlet.xml by default
  • WebApplicationContext is bound into ServletContext

DispatcherServlet Configuration

HandlerMapping
Routing of requests to handlers

ViewResolver
Maps symbolic name to view

Controllers
  • Controller interface defines one method
  • ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception
  • ModelAndView consists of a view identifier and a Map of model data

Controller Implementations

  • CommandControllers bind parameters to data objects
  • AbstractCommandController
    • basic controller, knows about caching, turning on/off get/set/post/head
  • AbstractFormController
  • SimpleFormController
    • for form handling, hooks for attaching commands, validator
  • WizardFormController
    • easy wizard controller
Spring MVC Lifecycle

Step 1: Incoming HTTP request is mapped to the Spring DispatcherServlet.
Step 2: The DispatcherServlet creates a container using the bean definitions found in the Servlet configuration file.
Step 3: The container finds an appropriate Controller to process the request.
Step 4: The Controller performs some custom logic and returns a ModelAndView to the container.
Step 5: The container renders the ModelAndView using the appropriate ViewResolver bean.
Step 6: An HTTP response is sent to the browser

link.html

<html>
<body>
<a href="Link.spring">GO TO SPRING HOME PAGE</a>
</body>
</html> 


Spring MVC Step 1
  • Incoming HTTP request is mapped to the Spring DispatcherServlet.
  • This is configured like any other Servlet mapping. Pick a URI-Pattern and associate it with the DispatcherServlet
web.xml

<servlet>
  <servlet-name>Dispatcher</servlet-name>
  <servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-
class>
  </servlet>
 
 <servlet-mapping>
  <servlet-name>Dispatcher</servlet-name>
  <url-pattern>*.spring</url-pattern>
 </servlet-mapping> 
 
Spring MVC Step 2

  • The DispatcherServlet creates a container using the bean definitions found in the Servlet configuration file.
  • The DispatcherServlet locates the configuration file using the naming convention servletname-servlet.xml. 

Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework
.org/schema/beans http://www.springframework
.org/schema/beans/spring-beans-2.5.xsd"
>

  <bean name="/Link.spring" class="mycontroller.LinkController"/>
  
  
</beans>

Spring MVC Step 3
  • The container finds an appropriate Controller to process the request.
  • For example, a request for /Link.spring is mapped to the Controller defined by the mycontroller.LinkController class
Spring MVC Step 4
  • The Controller performs some custom logic and returns a ModelAndView to the container.
  • The Controller is a Java class that extends the org.springframework.web.servlet.mvc.Controller class.
  • The request is handled by the handleRequest method.
LinkController.java
package mycontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LinkController implements Controller {

  public ModelAndView handleRequest(HttpServletRequest arg0,
      HttpServletResponse arg1throws Exception {

    return new ModelAndView("Welcome.jsp");
  }
}


Spring MVC Step 5

The container renders the ModelAndView using the appropriate View.

Welcome.jsp
<h2>
<font color="Green">
Welcome to Spring Home Page.
</font>
</h2>

Download Zip Code

Output






Spring provides an MVC framework for developing web applications, separating the Model, View, and Controller.