目录
[2. Java web的发展史](#2. Java web的发展史)
[Model I 和Model II](#Model I 和Model II)
[3. MVC模式](#3. MVC模式)
[4. Spring MVC配置](#4. Spring MVC配置)
[5. SpringMVC原理](#5. SpringMVC原理)
[5.1 SpringMVC中心控制器](#5.1 SpringMVC中心控制器)
[6. Maven配置](#6. Maven配置)
1.引入
$$ Spring与Spring MVC的区别
SSM 包含三部分:
- Spring MVC
- Spring
- Mybatis
SSM就是升级版的Servlet。
Servlet有哪些功能?
- 通过JDBC与数据库进行交互
- 和页面进行交互
- 业务层(一般不用Servlet做业务,业务层目前在我的项目中是缺失的。我用的是反射/代理)
SSM是对这些进行了升级。
- 和页面进行交互是通过使用Spring MVC。
- 业务层:本质还是用反射,只是用Spring框架做的,AOP与业务相关,面向切面编程
- 和数据库通过Mybatis
2. Java web的发展史
Model I 和Model II
*1. Model I开发模式
Model1的开发模式是:JSP+JavaBean的模式,它的核心是Jsp页面,在这个页面中,Jsp页面负责整合页面和JavaBean(业务逻辑),而且渲染页面,它的基本流程如下:

这样的设计模式到这一个jsp页面负责了视图的展示和业务流程的控制高度耦合的结果是Jsp代码十分复杂,后期维护困难!
*2. Model II开发模式
在Model II的模式下。利用jsp页面、servlet和JavaBean组件分工协作共同完成系统的所有任务。其中jsp 负责数据显示逻辑任务,servlet 负责程序的流程控制任务,JavaBean 负责处理业务逻辑任务。它的基本流程如下:

Model2:相对于Model1来说,职责分工更为明确,在Model1的基础上,抽取了Servlet层,体现了一个分层的思想,适合大型的项目开发!(当时的评判标准是适合大型项目开发的,现在看起来已经过时了!)
3. MVC模式
Spring mvc是基于Spring的一个模块,专门做web开发,可以理解为是Servlet的升级。
在Spring MVC框架当中, Controller 替代Servlet担负控制器的职能,
**M:**指model:模型层,指工程当中的Java Bean, 作用是 处理数据
Java Bean分两类:
- 一类是实体类Bean:专门存储业务数据的
- 一类为业务处理Bean:指Servlet或Dao对象,专门处理业务逻辑和数据
V:View, 视图层指项目当中的html或jsp等页面,作用是 与用户进程交互 ,展示数据。
C:Contoller, 控制层,指工程当中的servlet,作用是 接受请求和响应数据
MVC思想不是那个语言所特有的设计模式,也不是web应用所特有的思想,而是一种规范。MVC思想将一个应用分成三个基本部分:Model (模型) 、View (视图) 和 Controller (控制器),这三个部分以最少的耦合协同工作,从而提高了应用的可扩展性和可维护性。他们三者的关系如下图所示

4. Spring MVC配置
搭建Spring MVC入门程序
** 第一步:

** 第二步:

** 第三步:
等待加载,显示这些

把pom.xml的name那三行删掉。
** 第四步,加入依赖
XML
<!--版本锁定-->
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--servlet API-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>

把index.jsp删掉
** 第五步:
首先需要将原本的web.xml配置文档做出如下更改
全删掉复制以下(因为原来的已经过时了)
XML
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
** 第六步
配置DispatcheServlet
Spring MVC是基于原生的servlet,通过强大的前端控制器DispatcheServlet,对请求和响应进行统一处理。
Spring MVC的本质就是一个servlet,是对原生的servlet进行了封装。在以前我们浏览器的每一次请求都需要我们写一个对应的servlet,现在我们只需要将浏览器的请求和响应交给DispatcheServlet进行统一的处理。
在web.xml配置文件中核心控制器DispatcherServlet
XML
<!--在web.xml中配置Spring提供的过滤器类 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--不拦截所有是html的页面请求-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--配置前端控制器,对浏览器发送的请求进行统一处理-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件的位置和名称,配置的是Spring配置-->
<init-param>
<!--contextConfigLocation:上下文配置路径,固定值-->
<param-name>contextConfigLocation</param-name>
<!--classpath:类路径,值得是Java和resources文件夹-->
<!--springmvc.xml:指的是配置文件的名称:需要配置springmvc.xml,在下面-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--配置启动加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--开启项目时打开的页面-->
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
** 第七步
在main 下面创建java resource 写项目


** 第八步:
在resources 下创建springmvc.xml文件

** 第九步:
配置springmvc.xml
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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--配置spring创建容器时要扫描的包--> <context:component-scan base-package="com.qcby"></context:component-scan> <!--处理映射器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--处理器适配器--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--配置视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine" ref="templateEngine"/> </bean> <!-- templateEngine --> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> </bean> <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/html/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML5"/> </bean> <!-- JSON View --> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> </bean> <!-- 配置spring开启注解mvc的支持 默认就是开启的 ,要想让其他组件(不包含映射器、适配器、处理器)生效就必须需要配置了--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
** 第十步:
编写index.html页面
在webapp 下右键创建index.html文件

把以下代码复制进去,修改项目名字。
html
<html>
<head>
<meta charset="utf-8">
<title>入门程序</title>
</head>
<body>
<h3>入门</h3><a href="/SpringMVCDemo/hello.do" >入门程序</a>
</body>
</html>
** 第十一步:
编写suc.html页面
在html文件包下创建suc.html

html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>Hello <b th:text="${msg}"></b></h1>
</body>
<script>
</script>
</html>
** 第十二步:
编写Controller类和方法

java
//把当前类交给IOC容器进行管理
@Controller //Spring创建对象的方式
public class HelloController {
/**
* 处理超链接发送出来的请求
* @param model
* @return
*/
@RequestMapping(path = "/hello.do")
public String sayHello(Model model){
System.out.println("入门方法执行了2...");
// 向模型中添加属性msg与值,可以在html页面中取出并渲染
model.addAttribute("msg","hello,SpringMVC");
// 配置了视图解析器后,写法
return "suc"; //页面
}
}
因为springmvc.xml里:

** 第十三步:
启动Tomcat服务器,进行测试。
添加配置 :
点击这个窗口
如果是unnamed的话,可以改名 ,先找到Tomcat的包(我放在了E盘)
工作区 里的后面带exploded的
要删掉!!!,不然访问不到页面。
启动启动启动----------------------------------------
在浏览器输入路径即可。
小结:
5. SpringMVC原理
5.1 SpringMVC中心控制器
Spring的web框架围绕DispatcherServlet 【servlet调度】设计
DispatcherServlet的作用是将请求发送到不同的处理器

入门案例的执行流程
-
当启动Tomcat服务器的时候,因为配置了load-on-startup标签,所以会创建DispatcherServlet对象,就会加载springmvc.xml配置文件
-
开启了注解扫描,那么HelloController对象就会被创建
-
从index.jsp发送请求,请求会先到达DispatcherServlet核心控制器,根据配置@RequestMapping注解找到执行的具体方法
-
根据执行方法的返回值,再根据配置的视图解析器,去指定的目录下查找指定名称的JSP文件
-
Tomcat服务器渲染页面,做出响应
SpringMVC官方提供图形(很重要!!!要记住)

入门案例中的组件分析
前端控制器(DispatcherServlet)
处理器映射器(HandlerMapping)
处理器(Handler)
处理器适配器(HandlAdapter)
视图解析器(View Resolver)
视图(View)
6. Maven配置

Maven文件夹一定要在D盘的根目录下,因为里面配置。
