目录

一、Spring Mvc的介绍

1、Spring Mvc的定义:

2、为什么要使用Spring Mvc

 3、springMVC核心开发步骤

    4、 SpringMVC的组件

二、SpringMVC部署(如何在项目中添加SpringMVC)

1、首先在pom文件中添加Spring-mvc的依赖

2、添加Springmvc框架配置文件 

 3、在web.xml中配置:

4、以Helloworld为例建立Servlet

 三、Spring mvc的工作原理

四、 Spring Mvc常用注解

1、常用注解:

 2、结果集的处理

五、静态资源的处理


一、Spring Mvc的介绍

1、Spring Mvc的定义:

Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。

2、为什么要使用Spring Mvc

很多应用程序的问题在于处理业务数据的对象和显示业务数据的视图之间存在紧密耦合,通常,更新业务对象的命令都是从视图本身发起的,使视图对任何业务对象更改都有高度敏感性。而且,当多个视图依赖于同一个业务对象时是没有灵活性的。

SpringMVC是一种基于Java,实现了Web MVC设计模式,请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将Web层进行职责解耦。基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,SpringMVC也是要简化我们日常Web开发。
 

 3、springMVC核心开发步骤


3.1 DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC

  3.2 HandlerMapping的配置,从而将请求映射到处理器

  3.3 HandlerAdapter的配置,从而支持多种类型的处理器

  3.4 处理器(页面控制器)的配置,从而刊行功能处理

  3.5 ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
 

    4、 SpringMVC的组件

 4.1 前端控制器(DispatcherServlet)

  4.2 请求到处理器映射(HandlerMapping)

  4.3 处理器适配器(HandlerAdapter)

  4.4 视图解析器(ViewResolver)

  4.5 处理器或页面控制器(Controller)

  4.6 验证器(Validator)

  4.7 命令对象(Command 请求参数绑定到的对象就叫命令对象)

  4.8 表单对象(Form Object提供给表单展示和提交到的对象就叫表单对象)
 

二、SpringMVC部署(如何在项目中添加SpringMVC)

1、首先在pom文件中添加Spring-mvc的依赖

<dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring.version}</version>
    </dependency>

2、添加Springmvc框架配置文件 

2.1开启注解代理

2.2扫描注解驱动

2.3视图模型处理

2.4静态资源处理

<?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" xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 通过context:component-scan元素扫描指定包下的控制器-->
    <!--1) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.zjy"/>

    <!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    <!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,-->
    <!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--3) ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar -->
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--4) 单独处理图片、样式、js等资源 -->
    <!--<mvc:resources location="/css/" mapping="/css/**"/>-->
    <mvc:resources location="/images/" mapping="/images/**"/>
    <!--<mvc:resources location="/js/" mapping="/js/**"/>-->


</beans>

 3、在web.xml中配置:

Spring与web集成的监听器

SpringMvc与web集成的Servlet

<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_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 读取Spring上下文的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Spring MVC servlet -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <!--web.xml 3.0的新特性,是否支持异步-->
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

4、以Helloworld为例建立Servlet

package com.zjy.biz.impl;

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

/**
 * @author小朱
 * @site Bi8boYin
 * @company xxx公司
 * @create  2022-08-16 21:53
 */

//被controller标记教给Spring管理
@Controller
public class HelloController {

    //自定义Mvc 浏览器发送请求  http://locahost:8080/hello/hello.action?methodName=hello
    // Spring Mvc 浏览器发送请求 http://locahost:8080/helloReq
    @RequestMapping("/helloReq")
    public String hello(){
        System.out.println("hello");
        return "hello";
    }
}

自定义Mvc与SpringMvc的区别:使用了注解

spring如何导入别人的项目并运行 springmvc导入_spring

 三、Spring mvc的工作原理

用一张图来描述Spring mvc的流程

spring如何导入别人的项目并运行 springmvc导入_xml_02

 文字描述:

 2.1 首先用户发送请求-->DispatherServlet(将用户发送的请求截取后面的请求)

  2.2 DispatcherServlet-->HandlerMapping(将请求发送到处理器映射)

  2.3 DispatcherServlet-->HandlerAdapter(处理器适配器找到对应的方法)

  2.4 HandlerAdapter-->处理器功能处理方法的调用

  2.5 ModelAndView的逻辑视图名-->ViewRecolver(找到视图)

  2.6 View-->渲染

  2.7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束
 

四、 Spring Mvc常用注解

1、常用注解:

@controller:用来定义控制层的组件

spring如何导入别人的项目并运行 springmvc导入_xml_03

 @requestMapping("/hello")

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

spring如何导入别人的项目并运行 springmvc导入_spring如何导入别人的项目并运行_04

 

spring如何导入别人的项目并运行 springmvc导入_spring如何导入别人的项目并运行_05

 @pathvariable():可以用来映射URL中的占位符到目标方法的参数中 路径传参

@RequestMapping("/del/{bid}")
    public String del(@PathVariable(value = "bid") Integer bid){
        this.bookService.deleteByPrimaryKey(bid);
        return "redirect:/book/list";
    }

 2、结果集的处理

        String

        ModelAndView

  2.1转发页面(之前在自定义mvc中就是使用这种)
                  2.2重定向页面
                  2.3转发子控制器
return "forward:/book/list";

                  2.4重定向子控制器
return "redirect:/book/list";

五、静态资源的处理

 静态处理就是将static目录下的css、js、以及image文件进行处理,之后就可以直接访问目录就可以访问到

没有在spring-mvc.xml中加入以下语句是访问不到static目录下的文件

<mvc:resources location="/static/" mapping="/static/**"/>