SpringMVC执行流程

SpringMVC执行流程:

SpringMVC执行流程_第1张图片

  1. 服务器启动,初始化,加载Tomcat的配置文件
  2. 加载项目的配置文件web.xml
  3. 创建前端控制器DispatcherServlet,加载springMVC.xml配置文件
  4. 开启注解扫描,创建SpringMVC三大组件:处理器映射器(HandlerMapping)、处理器适配器(HandlerAdapter)、视图解析器(ViewResolver)
  5. 用户向服务器发送请求,前端控制器DispatcherServlet的处理器映射器HandlerMapping获取请求路径,解析所有的@RequestMapping注解,进行匹配,找到请求路径对应的Controller方法,将结果返回给前端控制器DispatcherServlet
  6. DispatcherServlet调用处理器适配器(HandlerAdapter)执行处理器映射器(HandlerMapping)在Controller中找到的方法,返回可执行结果ModelAndView给DispatcherServlet
  7. DispatcherServlet调用视图解析器(ViewResolver)获取执行方法返回的结果将逻辑视图解析成真正的视图,返回给前端控制器DispatcherServlet
  8. DispatcherServlet对页面渲染,最后响应给浏览器。

案例主要代码:

web.xml


    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">

    
    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springMVC.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

web-app>

springMVC.xml


<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:component-scan base-package="com.it"/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/pages/"/>
        
        <property name="suffix" value=".jsp"/>
    bean>

    
    
    
    <mvc:annotation-driven />

beans>

HelloController.java

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

/**
 * 处理请求的控制器类,也叫做处理器(Handler)
 */
@Controller // 创建对象加入容器
public class HelloController {

    @RequestMapping("/hello.do")
    public String save() {
        System.out.println("处理请求......");
        return "success";
    }
}

你可能感兴趣的:(SpringMVC执行流程,SSM)