Wednesday, June 22, 2016

Spring MVC Basics

Flow of the Spring MVC:
  •  The DispatcherServlet configured in web.xml file receives the request.
  • The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller.
  • Then the Controller executes the logic business logic (if written by the programmer) and then returns ModeAndView object to the DispatcherServlet.
  • The DispatcherServlet determines the view from the ModelAndView object.
  • Then the DispatcherServlet passes the model object to the View.
  •  The View is rendered and the Dispatcher Servlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.
    Sample 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">
    <display-name>MVC</display-name>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    </web-app>
    view rawweb.xml hosted with ❤ by GitHub

    Controller
    Controllers are annotated with @Controller. The Spring MVC Dispatcher Servlet scans for all the conroller classes. There is no need to define the controllers as beans in the conext xml file, Spring automatically does that for us. All we have to do is adding one line in the context file
    web-application-config.xml, which is passed in as an initialization paramter to the Spring MVC Dispatcher Servlet.

    Sample Controller class:
    package com.learning.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    @Controller
    public class HelloWorldController {
    /**
    * Web context path hello
    **/
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHello() {
    return "hello";
    }
    /**
    * Webcontext path mvc
    **/
    @RequestMapping(value = "/mvc", method = RequestMethod.GET)
    public ModelAndView helloWorld() {
    String message = "Hello World, Spring 3.0!";
    return new ModelAndView("mvc", "message", message);
    }
    }

    Above is an example of a controller. URI path /mvc/* is mapped to this controller. Each handler method further narrows it down by additional URI path pattern and http method. For helloWorld() method maps to /mvc  GET method. Handlermethods have flexible signature and return type. In this example, they return three types:

    • void: view is implied by RequestToViewNameTranslator
    • string: view name
    • Model object: view is implied by RequestToViewNameTranslator
    Model
    Model in Spring MVC is actually a map, which contains beans (map values) and bean names (map keys). Model is completely separated from view and view rendering technology.
    A special type of model is Form Model. Form model is specified in the form tag as following:

    In the handler method, form model object can be passed in as method parameter, or returned as return value. By convention-over-configuration, Spring figures out form model object name by its class name. You can always use @ModelAttribute if the names does not match.

    Spring 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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.learning.controller" />
    <mvc:interceptors>
    <!-- This interceptor is to pre handle or post handle every web request through servlet container.-->
    <bean class="com.learning.interpreter.HelloWorldInterceptor" />
    </mvc:interceptors>
    <bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    </beans>

    No comments:

    Post a Comment