在web应用中使用Spring(尚硅谷未使用Maven)
尚硅谷的spring4倒数第三节
1、如何在web中使用IOC容器(完整过程)?
说明:通过在web.xml中配置context的parm中的spring的xml文件名,再由监听器的初始化方法中获取contex的xml文件名,从而获取到对应的IOC容器存放到Context中attribute中,最后就是从servlet中由context获取到IOC容器来获取bean实例调用即可。
目录说明:
① 创建web工程,导入Tomcat的jar包以及Spring对应jar包

② 创建Spring的xml文件,配置bean对象
在src下创建Spring的xml文件(**applicationContext.xml**),并注入bean对象
<?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 id="person" class="com.mjj.SpringMvc.bean.Person"><property name="username" value="changlu"></property></bean></beans>
③ web.xml中使用来保存Spring的配置文件名
通过
<!--设置context属性,将对应xml文件名放入通过调用context获取到value值--><context-param><param-name>configLocation</param-name><param-value>applicationContext.xml</param-value></context-param>
④ 实现监听器接口,创建IOC容器并存放到Context实例中(重要)
主要步骤:
1.从servletContextEvent中获取ServletContext实例
2.从servletContext中获取到对应在web.xml中保存的context-param参数(就是对应spring的xml文件名)
3.通过ClassPathXmlApplicationContext以及配置文件名来创建IOC容器
4.将IOC容器存放到ServletContext中的属性中保存(为下面servlet获取)
package com.mjj.SpringMvc.listener;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;/*** @anthor changlu* @create 2020-08-23-19:17*/public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {//从ServletContextEvent中获取到实例ServletContext servletContext = servletContextEvent.getServletContext();//从context中获取到对应设置的inti参数值就是对应的xml配置String configLocation = servletContext.getInitParameter("configLocation");//1、创建Ioc容器 从ServletContext中的参数获取到的值作为参数ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);//2、将IOC容器放置到context中servletContext.setAttribute("ApplicationContext",context);}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {}}
web.xml(配置监听器)
<!--配置监听器--><listener><listener-class>com.mjj.SpringMvc.listener.MyListener</listener-class></listener>
⑤ 创建Servlet程序,获取Context中IOC容器获取bean类,输出
ExerServlet.java
/**
* @anthor changlu
* @create 2020-08-23-19:31
*/
public class ExerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取到ServletContext实例
ServletContext servletContext = getServletContext();
//2、从ServletContext实例中获取到Ioc容器
ApplicationContext context = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
//通过调用getBean()获取到实例
Person bean = context.getBean(Person.class);
bean.hello();//调用方法
}
}
web.xml
<servlet>
<servlet-name>ExerServlet</servlet-name>
<servlet-class>com.mjj.SpringMvc.Servlet.ExerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExerServlet</servlet-name>
<url-pattern>/exerServlet</url-pattern>
</servlet-mapping>
⑥ 通过jsp中的链接地址访问到servlet程序
<%--
Created by IntelliJ IDEA.
User: 93997
Date: 2020/8/23
Time: 19:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/SpringMvc_exer/exerServlet">点击跳转</a>
</body>
</html>
实际演示

运行结果:
一、SpringMVC基本概念
以前的ssh:spring、struts2、Hibermate
现在的ssm:spring、springMVC、Mybatis
1、三层架构和MVC
1.1、三层架构


1.2、MVC模型
1. MVC全名是Model View Controller 模型视图控制器,每个部分各司其职。
2. Model:数据模型,JavaBean的类,用来进行数据封装。
3. View:指JSP、HTML用来展示数据给用户
4. Controller:用来接收用户的请求,整个流程的控制器。用来进行数据校验等。
2、SpringMVC概述
2.1、SpringMVC 是什么
SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。
Spring 框架提供了构建 Web 应用程序的全功 能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发框架,如 Struts1(现在一般不用),Struts2 等。
SpringMVC 已经成为目前最主流的 MVC 框架之一,并且随着 Spring3.0 的发布,全面超越 Struts2,成 为最优秀的 MVC 框架。 它通过一套注解,让一个简单的 Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持 RESTful 编程风格的请求。
2.2、SpringMVC 在三层架构的位置

2.3、 SpringMVC 的优势

2.4、SpringMVC 和 Struts2 的优略分析(需要熟知)
共同点: 它们都是表现层框架,都是基于 MVC 模型编写的。 它们的底层都离不开原始 ServletAPI。 它们处理请求的机制都是一个核心控制器。
区别:
① Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter 。
② Spring MVC 是基于方法设计的,而 Struts2 是基于类,Struts2 每次执行都会创建一个动作类。所 以 Spring MVC 会稍微比 Struts2 快些。
③ Spring MVC 使用更加简洁,同时还支持 JSR303, 处理 ajax 的请求更方便 (JSR303 是一套 JavaBean 参数校验的标准,它定义了很多常用的校验注解,我们可以直接将这些注 解加在我们 JavaBean 的属性上面,就可以在需要校验的时候进行校验了。)
④ Struts2 的 OGNL 表达式使页面的开发效率相比 Spring MVC 更高些,但执行效率并没有比 JSTL 提 升,尤其是 struts2 的表单标签,远没有 html 执行效率高。
二、SpringMVC的入门
1、入门程序案例
1.1、需求分析(发送请求,跳转页面)

1.2、创建web工程(详细过程)
1.2.1、增加键值对加快创建工程速度(下面第三张图)
archetypeCatalog
internal




1.3、pom.xml中增加文件目录、添加依赖
增加两个目录:java以及resources
说明:初始是创建两个directory
之后java通过右键Mark Directory as -> Sources Root
resources通过右键Mark Directory as -> Resources Root
修改添加配置、增加依赖
配置处理:
增加版本锁定:**
<spring.version>5.0.2.RELEASE</spring.version>
添加依赖:
包含context、spring-web、spring-webmvc、servlet、jsp的jar包**
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<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>
1.4、web.xml配置前端控制器(DispatcherServlet)
说明:这里就是设置spring中的前端控制器,直接调用即可
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
1.5、配置Tomcat(详细过程)



这里选择第二个,图片有误
设置完即可运行访问
**
1.6、代码编写
需要创建一个spring配置文件,一个控制类、新增一个程序跳转页面、web.xml配置
目录演示
大概过程叙述说明
1)首先需要需要创建spring的配置文件,其中进行注解开启呀、配置视图解析器(包含访问路径)以及SpringMVC的注解支持
2)之后写对应控制类相当于之前的servelt程序,不过在SpringMVC中提前配置好前端控制器,之后增加一个注解即可获取访问请求,并且在对应方法之上增加映射地址来区分方法的调用
3)对web.xml进行而配置,之前已经创建好了DispatcherServlet,又spring配置文件需要加载才能执行,所以在这个servelt配置时使用init-parm存储到这里,并且使用标签设置为项目启动时就加载
4)在index.jsp页面增加链接到指定地址进行请求,并且增加hello.jsp页面作为跳转地址
① 新增的spring配置文件:放置在resources资源包中
其中引入了context、mvc的标签,开启注解扫描
配置视图解析对象(这个与对应控制器方法的返回值跳转页面有关)
开启SpringMVC框架注解的支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.changlu"/>
<!--配置视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/> <!--配置访问路径-->
<property name="suffix" value=".jsp"/> <!--配置资源文件的结尾-->
</bean>
<!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven/>
</beans>
② 控制类:HelloController(两个注解)
引入两个注解,都是MVC中的注解
① 控制器注解@Controller就是表示对应类为web.xml中的DispatcherServlet实例对象
② 请求映射注解@RequestMapping(path = “/hello”),指明访问这个方法的请求地址
③ 值得一说的是这个return 返回的名称可以指定为跳转jsp页面的名称,不过需要在spring的xml配置中配置
package com.changlu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @anthor changlu
* @create 2020-08-26-16:16
*/
//控制器注解
@Controller
public class HelloController {
@RequestMapping(path = "/hello") //配置请求地址注解
public String sayHello(){
System.out.println("hello,SpringMVC");
return "success";
}
}
③ web.xml配置(配置DispatcherServlet)
① 需要配置spring中的DispatcherServlet,可以与注解@Controller进行一起使用,相当于配置好这个之后只要写对应注解就能够接受指定路径请求。
② 由于spring的配置文件需要进行加载才能够进行期中的配置,所以在配置DispatcherServlet的同时增加至其中,使用
③
如果不配置在Tomcat启动时加载servlet,那么会获取到响应的情况下才会进行配置其中的配置文件
<!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>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置spring的配置文件:springmvc.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> <!--Tomcat服务器启动就进行配置这个servlet-->
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
④ 跳转成功页面:success.jsp
<%--
Created by IntelliJ IDEA.
User: 93997
Date: 2020/8/26
Time: 16:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>恭喜你成功访问!</h1>
</body>
</html>
⑤ 重置index.jsp页面(删除重新创建)
说**明:初始创建了工程文件,index.jsp没有上面处理乱码问题的标签,所以删除重建一个并且增加链接跳转链接**
<%--
Created by IntelliJ IDEA.
User: 93997
Date: 2020/8/26
Time: 16:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--处理乱码问题--%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入门程序哟</h3>
<a href="hello">点击跳转链接</a>
</body>
</html>
1.7、程序执行与页面显示


流程总结

2、SpringMVC基于组件方式的执行过程
图例演示

各类组件介绍
DispatcherServlet:前端控制器
用户请求到达前端控制器,它就相当于 mvc 模式中的 c,dispatcherServlet 是整个流程控制的中心,由 它调用其它组件处理用户的请求,dispatcherServlet 的存在降低了组件之间的耦合性。
HandlerMapping:处理器映射器
HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的 映射方式,例如:配置文件方式,实现接口方式,注解方式等。
Handler:处理器
它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由 Handler 对具体的用户请求进行处理。
HandlAdapter:处理器适配器
通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理 器进行执行。
View Resolver:视图解析器
View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名 即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
View:视图
SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView 等。我们最常用的视图就是 jsp。
一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
说明
在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件。
使用
它就相当于在 xml 中配置了: 


3、@RequestMapping注解
作用说明(源码+案例)

源码分析

案例介绍与演示
① 如果单独作用在方法上,就是一级域名查找
② 如果同时作用在类以及方法上,那么类上的就是一级域名,方法上就是二级域名,这样就可以区分对应业务方法请求
属性介绍(五个,介绍分析)

属性目录清单

介绍分析
- ① 首先是value以及path属性:请求地址
可以看到这两个互相对应,含义相同
写法演示:这三种都是相同的意思


- ② method属性:请求方式
源码分析:
对象数组类型,可以设置多个类型
发现是枚举类
写法演示:

- ③ params属性:请求参数
源码分析:
写法演示:

- ④ headers:包含请求头
源码分析:
写法演示:
整理者:长路 时间:2020/8/22-2020/8/26

