• SpringMVC流程


    2021年6月1日22:32:04
    今天学习了一下SpringMVC。总结一下。

    SpringMVC的过程

    前提:一个SpringMVC的服务启动好了。等待请求进来。

    浏览器发送了一个请求过来比如是http://localhost:8080/hello

    服务器接到了这个请求。

    开始SpringMVC之旅。

    1、DispatchSevrlet(前端控制器,也叫分发处理器)接到了这个请求,开始做去处理器映射(HandlerMapping)中查找处理器
    2、找到后再去HandlerExecution中找执行器
    3、找到后返回到DispatchSevrlet
    4、DispatchSevrlet去HandlerAdapter(处理器适配器)中寻找对应的Controller
    5、Controller开始执行对应的业务代码
    6、业务代码执行完毕,然后做一个视图跳转,就是把需要返回的视图加入到mv中,返回这个视图模型(ModelAndView,mv)给HandlerAdapter
    7、HandlerAdapter再返回给DispatchSevrlet
    8、DispatchSevrlet把这个mv发给ViewerResolver(视图解析器)
    9、视图解析器把这个mv中的数据,填充到对应的页面上(这个动作叫渲染视图页面)
    10、渲染完后,把这个视图发给DispatchSevrlet
    11、DispatchSevrlet收到视图后返回给请求的客户端

    好了,整个SpringMVC的过程完毕了。

    为了配合这个过程,肯定是需要有配置文件的。
    涉及到的配置文件有:
    1、这是一个web项目,肯定要有web.xml,这个文件中需要配置的东西有

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <!--DispatchSevrlet是Spring的核心-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--DispatchSevrlet要绑定SpringMVC的配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!--和服务器一起启动&ndash;&gt;-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    2、在上面的web.xml中看到配置了一个springmvc.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--处理器映射器-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--处理器适配器-->
        <bean class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter"/>
    <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    实际开发中不会这么写的。关于三个器。可以点进去看看。

    实际开发也很少使用jsp了。现在都用的是模板引擎thymeleaf freemarker.

    注解版本

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <!--DispatchSevrlet是Spring的核心-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--DispatchSevrlet要绑定SpringMVC的配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!--和服务器一起启动&ndash;&gt;-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    

    springmvc.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
           https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--自动扫描包-->
        <context:component-scan base-package="com.test.controller"/>
        <!--不用处理静态资源文件-->
        <mvc:default-servlet-handler/>
        <!--替代HandlerMapping和HandlerAdapter-->
        <mvc:annotation-driven/>
        <!--上面三个是固定的-->
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    hello.java

    package com.test.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/user")
    public class hello {
    
        @RequestMapping("/hello")
        public String hello(Model mv) {
            mv.addAttribute("msg", "hello");//把msg填充为hello
            return "hello";//返回hello.jsp页面
        }
    }
    
    
  • 相关阅读:
    [ 转载 ] Mysql 远程连接+开放80和3306端口 常用配置
    [ 转载 ] Mysql 数据库常用命令
    [ 转载 ] Centos 安装mysql后启动失败 出现 ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
    [2020多校联考]手套
    [2020多校联考]糖果机器
    [2020多校联考]染色相邻的边
    [2020多校联考]四个质数的和
    [2020多校联考]简单题
    [2020多校联考]MC
    [2020多校联考]进化
  • 原文地址:https://www.cnblogs.com/dhu121/p/14839278.html
Copyright © 2020-2023  润新知