SpringMVC

一.引入SpringMVCjar

Web  webmvc   context  context-support

-----Web ---

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

 

---------webmvc---------

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

 

---------context ---------

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

----------context-support--------------

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.1.RELEASE</version>
</dependency>

 

 

:第一个项目的整体流程

01.用户在index.jsp页面中点击Hello超链接

02.点击之后请求路径是  /hello   <a href="hello">Hello</a>

03.会被我们web.xml文件中配置的DispatcherServlet拦截

04.默认执行doDispatch()

      HandlerExecutionChain mappedHandler = null;  处理器执行链

      mappedHandler = this.getHandler(processedRequest); 根据用户的请求获取处理器执行链

        4.1.跟进getHandler()看到了

            for (HandlerMapping hm : this.handlerMappings)

            handlerMappings是一个List<HandlerMapping>

            HandlerExecutionChain handler = hm.getHandler(request);

            hmList<HandlerMapping>是集合中的一个元素===HandlerMapping

            根据用户的请求在处理器映射器中查询Handler

        4.2.跟进getHandler()看到了

            Object handler = getHandlerInternal(request); 获取需要执行的Handler

            HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);

        4.3.跟进getHandlerExecutionChain

          发现方法中有处理器拦截器

          for (HandlerInterceptor interceptor : this.adaptedInterceptors)

   所以mappedHandler(处理器执行链)中包含一个即将执行的handler和一些列的interceptor(拦截器)

05.HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

   获取处理器适配器

   跟进 getHandlerAdapter()

   for (HandlerAdapter ha : this.handlerAdapters)

   handlerAdapters是一个List<HandlerAdapter>

   ha就是List<HandlerAdapter>其中一个元素 就是HandlerAdapter

          if (ha.supports(handler)) {

return ha;

}

   5.1:跟进supports

      进入了HandlerAdapter====》是一个接口!

      接口有5个实现类!默认执行3个!

      webmvcjar包中的根目录下找到DispatcherServlet.properties

       默认的处理器映射器的配置

        org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,

       org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

        默认的处理器适配器的配置

        org.springframework.web.servlet.HandlerAdapter

        =org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,

        org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,

        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

例子

public class HelloController extends AbstractController{
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("cn.kitty.controller.HelloController------");
        return new ModelAndView("/WEB-INF/jsp/hello.jsp");
    }
}

 

Springmvc-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:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               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/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--配置默认的处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

    <!--用户请求bean-->
    <bean id="/hello" class="cn.kitty.controller.HelloController"></bean>

</beans>

 

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello.jsp</title>
</head>
<body>
第一个Spring的例子
</body>
</html>

 

原文地址:https://www.cnblogs.com/cuixiaomeng/p/7718664.html