Spring MVC体系结构和处理请求控制器
一,MVC模式
模型(Model)对应组件:JavaBean
视图(View)对应组件:JSP或者HTML文件
控制器(Controller)对应组件:Servlet
1,JSP Model1 (早期使用的模式)
2,JSP Model2 (现在使用的)
JSP:负责生成动态网页
Servlet:负责流程控制
JavaBean:负责业务逻辑处理
3,MVC处理过程
4,MVC的优缺点
优点:
· 多视图共享一个模型,大大提高代码的可重用性
· MVC三个模块相互独立,松耦合架构
· 控制器提高了应用程序的灵活性和可配置性
· 有利于软件工程化管理
缺点:
· 原理复杂
· 增加了系统结构和实现的复杂性
· 视图对模型数据的低效率访问
二,Spring MVC架构
1,Spring MVC
· 结构最清晰的MVC Model2实现
· Controller (控制器)
· ModelAndView(模型和视图)
2,Spring MVC环境搭建
Spring MVC框架搭建步骤
①,下载jar文件并导入工程
spring-web-5.2.3.RELEASE.jar
spring-webmvc-5.2.3.RELEASE.jar
②,配置文件
在web.xml中配置Servlet 具体如下:
<?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_3_1.xsd"
version="3.1">
<!--DispatcherServlet 中央核心控制器-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
<!--SpringMVC.xml src目录下的Spring MVC文件-->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
<!--所有.do结尾的控制(请求)-->
</servlet-mapping>
<!--过滤器-->
<filter>
<filter-name>characterencoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterencoding</filter-name>
<url-pattern>/*</url-pattern>
<!--web目录下的文件-->
</filter-mapping>
</web-app>
创建并配置Spring MVC的配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!--自动扫描上下文包-->
<context:component-scan base-package="cn.tb.*" />
<!--自动开启mvc模式的注解-->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--prefix 前缀 value的值是jsp文件存放的路径-->
<property name="prefix" value="/" />
<!--suffix 后缀 value的值是jsp文件的后缀名-->
<property name="suffix" value=".jsp" />
</bean>
</beans>
③,创建Controller处理请求的控制器
④,创建View-JSP
⑤,部署运行
3,Spring MVC请求处理流程
4,Spring MVC体系结构
①,DispatcherServlet(前端控制器)
Spring MVC最核心的类
web.xml中配置
②,Handler(处理器):对应MVC中的(Controller层)
类型:Object
作用:实际处理请求
标注了@RequestMapping的所有方法都可以看作是一个Handler
③,ModelAndView
逻辑视图名
模型对象
④,核心组件
· HandlerMapping(处理器映射)
→ BeanNameUrlHandlerMapping(默认)
将请求URL映射到同名的控制器Bean上
→ DefaultAnnotationHandlerMapping
将请求映射到标注@RequestMapping注解的控制器和处理方法上
→ RequestMappingHandlerMapping
· HandlerAdapter(适配器)
→ AnnotationMethodHandlerAdapter
→ RequestMappingHandlerAdapter
· ViewResolver(视图解析器)
→ InternalResourceView
5,Spring MVC框架特点
· 清晰地角色划分
· 灵活的配置功能
· 提供了大量的控制器接口和实现类
· 真正做到与View层的实现无关(JSP、Velocity.Xslt等)
· 国际化支持
· 面向接口编程
· Spring提供了Web应用开发的一整套流程,不仅仅是MVC,他们之间可以很方便的结合一起
三,实例展示
搭建Spring MVC环境,并实现在前端页面输出 “hello!!!欢迎学习springMVC!!!”
我的项目结果如下
需要在web目录下的WEB-INF下创建一个lib目录,用于存放需要的jar包
web.xml文件和SpringMVC文件在上面已经配置好了,这里就不重复了
前端页面:
<%--
Created by IntelliJ IDEA.
User: manager
Date: 2021/3/17
Time: 11:24:59
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>
hello!!!!!欢迎学习springMVC!!!!!
</body>
</html>
测试类:
package cn.tb.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Test1 {
@RequestMapping("/hello.do")
public String hello(){
System.out.println("HELLO");
return "index";
}
}
测试结果: