SSM - Spring MVC 框架学习

127 阅读1分钟

什么是 MVC

Model(dao service)

View(jsp = Java + HTML)

Controller(Servlet)

是一个软件设计的规范,可以参考,方便软件的开发;

什么是 Spring MVC

对于 MVC 这种软件设计思想的具体实现框架;里面封装了 Servlet 可以使得网页开发变得简单了一些;开发的效率得到了一定的提升;

pojo vo dto

都是存实体类代码的文件夹,但是在具体的场景,一个有时候不是需要一个对象的所有属性,vo 等对象的细分可以对一个完整对象的部分属性进行封装,达到减少系统资源浪费等需求;

SpringMVC 核心

请求分发调度的作用 DispatcherServlet; 将前端访问的 Servlet 请求进行调度处理,减少 Servlet 代码的开发;

image.png

SpringMVC 底层在本质上面也是一个 Servlet ,但是对于请求的分发做了优化处理;

image.png

SpringMVC 原理

image.png

image.png

image.png

使用 MVC 可以重复使用的配置文件

webapp WEB-INF 下面的 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">

    <!--配置 DispatcherServlet Spring MVC 的核心东西,请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--绑定SpringMVC 的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <!--启动级别,服务器启动的时候就启动这个 Servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--
          /  只会匹配所有的请求,不会匹配 jsp 一般只会写这个
          /* 匹配所有的请求,包括 jsp
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>
</web-app>

resources 下面的 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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--上面的各种标签位置的变动也是有可能使得启动服务器失败的-->
    <!--xmlns:xsi 当前的 xml 文件需要遵循的规范-->
    <!--context mvc 当前 xml 需要使用的标签,进行标签的引用,和 jar 包的引用类似,等号右边是引用的标签遵守的规范-->
    <!--xsi:schemaLocation-->
    <!--xsi:schemaLocation,上面引用的指定文件的验证文件,保证标签书写的规范化-->


    <!--Spring MVC 的项目直接使用下面的配置即可,不需要修改-->


    <!--扫描包,扫描包里面的注解,使用注解进行开发,扫描控制器的位置-->
    <context:component-scan base-package="com.luobin.controller"/>

    <!--原来的映射以及适配器,使用这个就可以代替掉 省略掉前面配置的处理器以及配置器-->
    <mvc:annotation-driven/>

    <!--过滤掉前端的一些没有用的文件 .css .js 等-->
    <mvc:default-servlet-handler/>

    <!--进行返回时候的页面文件的拼接-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>