SpringMVC基础
1.SpringMVC概述
1.1 三层架构
三层架构:
- 表现层:负责数据展示
- 业务层:负责业务处理
- 数据层:负责数据操作
![image-20210429150507447](https://i-blog.csdnimg.cn/blog_migrate/6d5f86ba0cf22b2134cea8e92d1bfcbb.png)
1.2 MVC
MVC(Model View Controller),一种用于设计创建web应用程序表现层的模式
- Model(模型):数据模型,用户封装数据
- View(视图):页面视图,用户展示数据
- jsp
- html
- Controller(控制器):处理用户交互的调度器,用于根据用户需求处理程序逻辑
- Servlet
- SpringMVC
![image-20210429150913422](https://i-blog.csdnimg.cn/blog_migrate/1d45e26271889cb59cbc15ff8b1841f8.png)
1.3 SpringMVC简介
概念:SpringMVC是一种基于Java实现MVC模型的轻量级web框架
SpringMVC优点:
- 使用简单
- 性能突出(相比现有的框架技术)
- 灵活性强
2.入门案例
2.1 入门案例制作(重点)
-
XML版
-
XML+注解版(主体)
-
纯注解版(变形)
-
基于Spring环境开发
-
SpringMVC版本与Spring版本同步–5.1.9
![image-20210429170503625](https://i-blog.csdnimg.cn/blog_migrate/912f372ac984e1a0affed307e9505783.png)
步骤:
- 导入坐标
<dependencies>
<!--servlet3.1规范坐标-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
<scope>provided</scope>
</dependency>
<!--springmvc坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
<scope>provided</scope>
</dependency>
<!--spring web坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--构建-->
<build>
<!--设置插件-->
<plugins>
<!--具体的插件配置-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
-
定义业务层处理器Controller,并配置成spring的bean(等同于Servlet)
@Controller public class UserController { @RequestMapping("/save") public String save() { System.out.println("user mvc controller is running..."); return "success.jsp"; } }
该bean的处理需要使用独立的配置文件扫描(XML版):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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <!--文件扫描,配置作用范围--> <context:component-scan base-package="com.itheima"/> </beans>
-
web.xml中配置SpringMVC核心控制器,用于将请求转发到对应的具体业务处理器Controller中(等同于Servlet配置)
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocaltion</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
-
设定具体Controller的访问路劲(等同于Servlet在web.xml中的配置),并设置返回页面
@Controller public class UserController { @RequestMapping("/save") public String save() { System.out.println("user mvc controller is running..."); return "success.jsp"; } }
此处记录一个问题:
问题现象:org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: org.apache.catalina…
问题解决:
-
查看web.xml是否无误,路径等是否有误,一个servlet不能同时使用xml和注解配置,否则也很会出现该错误;
-
查看项目lib目录是否存在,idea默认不创建,
进入Project Structures(ctrl+shift+alt+S)–>点击左侧Project Settings下的Artifacts。
点击中间栏项目,这里会两个文件,一个是:项目名:war(war压缩包),一个是项目名:war exploded(war未压缩包)。
点击war exploded项目,在右侧中第一栏Output Layout(项目发布生成的文件)下,展开WEB-INF文件夹,此时该目录下只有classes目录,无lib目录,自己手动创建一个lib目录,并点击上面+选择Library File添加maven导入的jar包
重启tomcat就解决了(我是这样解决的,不保证所有类似问题都能解决)
2.2 入门案例工作流程分析(重点)
服务器启动:
- 加载web.xml中的DispatcherServlet
- 读取spring-mvc.xml中的配置,加载所有com.itheima包中所有标记为bean的类
- 读取bean中方法上标注@RequestMapping("/save")的内容
处理请求:
- DispatcherServlet配置拦截所有请求 /
- 使用请求路径与所有加载的@RequestMapping的内容进行比对
- 执行对应的方法
- 根据方法的返回值在webapp目录中查找对应的页面并展示
2.3 SpringMVC技术架构图(重点)
SpringMVC技术架构图:
![image-20210429185141784](https://i-blog.csdnimg.cn/blog_migrate/1f94b5b1548169dc0c32c7f0aa276d6c.png)
- DispatcherServlet:前端控制器,是整体流程控制中心,由其调用其他组件处理用户的请求,有效的降低了组件间的耦合性
- HandleMapping:处理器映射器,负责根据用户请求找到对应具体的Handler处理器
- Handler:处理器,业务处理的核心类,通常由开发者编写,描述具体的业务
- HandlerAdapter:处理器适配器,通过它对处理器进行执行
- ViewResolver:视图解析器,将处理结果生成View视图
- View:视图,最终产出结果,常用视图入jsp、html
3.基础配置
3.1 Controller加载控制(重点)
SpringMVC的处理器对应bean必须按照规范格式开发,为避免加入无效的bean可通过bean加载过滤器进行包含设定或排除设定,表现层bean标注通常设为@Controller
<context:component-scan base-package="com.itheima">
<context:include-filter
type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
以前使用方式:
@ComponentScan(
value = "com.itheima",
excludeFilters =
@ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class)
)
此处记录一个问题:
问题现象:启动tomcat完成时,idea弹窗提示“http://localhost:80/找不到应用程序”
解决方法:点击idea主界面运行图标旁下拉列表,选择Edit Configuration,进入配置界面,Open Browser选择一个固定浏览器,不要使用默认的浏览器(tomcat启动完成自动使用该浏览器访问)
制作案例:和前面案例一样,只需修改spring-mvc.xml配置文件:
<context:component-scan base-package="com.itheima">
<!--添加过滤,包含该注解才会被扫描到-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
bean加载控制说明:
- 业务层与数据层bean加载由spring控制,参照spring课程加载方式
- 表现层bean加载由SpringMVC单独控制
- 表现层处理器bean使用注解@Controller声明
- bean加载控制使用包含性过滤器
- 过滤器类型为通过注解进行过滤
- 过滤的注解名称为Controller
3.2 静态资源加载
核心控制器拦截的是所有请求,需要对非普通资源请求进行放行,通过配置放行资源实现
-
<mvc:resources mapping="/img/**" location=