Spring Framework

Spring MVC Architecture:

We create a dynamic project on workspace, download all jar file uses in spring framework. If you make project on maven then add dependency in pom.xml file.

Now we define configuration on web.xml file. The web.xml file is the deployment descriptor for a Servlet-based Java web application. We define servlet mapping for spring mvc.

<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/techa2z-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Url pattern say any type of request will be handled by servlet.

Now define spring mvc bean handler xml file. which is defined in web.xml file i.e. techa2z-servlet.xml before this work define a controller class with package. That controller class will be loaded by bean class BeanNameUrlHandlerMapping.
On the basis of this Handler Mapping All request forwarded to the bean. Now define your request handler mapping name in and declare corresponding Controller.

Here view resolver used to handle all views in the form of suffix .jsp inside the /WEB-INF/pages/  .


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
     <bean id="HeandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean>
     <bean name="/welcome.html" class="com.techa2zsolution.controller.SpringFirstController" ></bean>
   
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix"><value>/WEB-INF/pages/</value></property>
         <property name="suffix"><value>.jsp</value></property>
     </bean>
   
</beans>

This is controller class which extends AbstractController Class for handling all request consisting inside the class, it's treaded as controller for all request.
AbstractController class consist handlerRequestInternal method having parameter HttpServletRequest and HttpServletResponse which handler request and send back response.

public class SpringFirstController extends AbstractController {
   
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("firstSpring");
        mav.addObject("message", "Hi, Welcome To First Spring Applcation Test!");
        return mav;
    }
}

ModelAndView role back response to the firstSpring.jsp page. By taking message which is added in ModelAndView object.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>First Spring Application Project!</title>
</head>
<body>
    <h1>This is My First Spring Application Project! </h1>
    <b>Welcome ,    ${message}</b>
</body>
</html> 

When we are working on the basis of Annotation then this concept is little bit change, your spring mvc handler xml file is modify by this way.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean> -->
    <!-- Hendler mapping is also be removed for this xml file, because you used @Annotation controller -->
    <!-- <bean name="/secondSpring.html" class="com.techa2zsolution.web.controller.SpringSecondController" ></bean>
        If you writting controller annotation in controller page!. Then Controller bean not required.
    -->
   
    <!-- If you can't be used bean based handler or controller action!    Then need to mension context:component-scan mapping -->
    <context:component-scan base-package="com.techa2zsolution.web.controller"></context:component-scan>
   
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix"><value>/WEB-INF/pages/</value> </property>
        <property name="suffix"><value>.jsp</value> </property>
    </bean>
   
</beans>


And controller class can't be extending AbstractController Class.

package com.techa2zsolution.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringSecondController {
   
    @RequestMapping("/secondSpring.html")
    public ModelAndView secondSpringController() {
        ModelAndView mav = new ModelAndView("springSecond");
        mav.addObject("message", "Annotation Based Application! ");
        return mav;
    }   
}