什么是SpringMVC?

Spring MVC 随着Spring3.0 的发布已经成为一个最优秀Spring 的MVC框架,特点有 MVC 设计模型请求驱动类型轻量级 web框架

Spring 提供的 监听器 ContextLoaderListener 做了什么

加载Spring配置文件、创建应用上下文对象并存储到ServletConttext域中,提供了一个客户端工具 WebApplicationContextUtils 获得 应用上下文对象

我们如何使用?

  • 在web.xml配置 ContexLoaderListener 监听器(Spring-web坐标)
  • 使用WebApplicationContextUtils 获得应用上下文对象 ApplicaitonContext

SpringMVC 开发步骤

切记 使用SpringMVC开发时,使用依赖 请统一版本,跨版本,你就等着报错吧!!!

切记 使用SpringMVC开发时,使用依赖 请统一版本,跨版本,你就等着报错吧!!!

切记 使用SpringMVC开发时,使用依赖 请统一版本,跨版本,你就等着报错吧!!!

  • 1、导包 SpringMVC包
  • 2、配置Servlet 核心前端控制器 DispathcerServlet
  • 3、编写Controller 和 视图页面
  • 4、将Controller 使用注解@Controller 配置到Spring 容器中
  • 5、开启组件扫描 放到 spinrg-mvc.xml
  • 6、测试
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 使用注解开发,需要配置组件扫描 这样 Spring就能知道被注解修饰的内容-->
    <context:component-scan base-package="com.zlk"/>
</beans>

spring-mvc.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"
       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.zlk"/>

</beans>

web.xml

<!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>



  <!-- 初始化全局参数 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置SpirngMVC 的前端控制器-->
  <servlet>
    <servlet-name>DispathcerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispathcerServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

自己随便写一个Controller层 写一个 RequestMapping() 即可

然后 自己测试,看看能不能调到 webapp 文件下的网页 即可

SpringMVC 执行流程 (手写)

首先要知道 组件

DispatcherServlet 前端控制器

HandlerMapping 处理器映射器

HandlerAdaptor 处理器 适配器

ViewResolver 视图解析器

  1. 用户发送请求 到DispatcherServlet
  2. DispatcherServlet前端控制器 接受请求 去调用 HandlerMapping 处理器映射器
  3. 处理器映射器找到具体的处理器(根据XML、或者注解)生成处理器对象,以及拦截器(如果有则生成没有就不生成) 一并返回给 DispatcherServlet前端控制器
  4. DispatcherServlet前端控制器 去 调用 HandlerAdaptor处理器适配器
  5. HandlerAdaptor处理器适配器 去调用具体的处理器 (Controller 也叫后端控制器)
  6. Controller 执行完成返回 ModelAndView。
  7. HandlerAdaptor处理器适配器 将后端控制器返回的 ModleAndView 返回给 DispatcherServlet前端控制器
  8. DispatcherServlet 将ModleAndViewer 传递给 ViewReslover视图解析器
  9. ViewReslover 解析后 返回DispatcherServlet 具体的View
  10. DispatcherServlet 根据View 进行渲染视图。并返还给用户。

SpringMVC 常见注解

@RequestMapping :用于建立请求URL与请求执行方法的对应关系

参数Method = “” ,可以选择 枚举形式RequestMethod.GET

配置内部资源视图解析器

spring-mvc.xml 添加下面配置

    <!-- 配置内部资源视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。

对于会员专享、整站源码、程序插件、网站模板、网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理

源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源