前言:dispatcherservlet里包含handlermapping和handleradapter两个处理器
dispatcherservlet位置在所有请求之前的,也就是说所有请求都要通过dispatcherservlet来处理,这也是springMVC的核心
handlerMapping作用:根据求情中到相应的handler(controller)
handleradapter作用:去适配(找到)并执行对应handler(controller)的方法,如处理方法参数、相关注解、数据绑定、消息转换、返回值、调用视图解析器等等。
MVC:Model(dao,service) View(jsp) Controller(servlet)
模型 视图 控制器
一,web开发的两个发展阶段
二,SpringMVC流程图(官方中文版)
实际上(具体流程)SpringMVC流程图
注意:实线部分Spring已经完成的
我们只需要是实现虚线部分
三,SpringMVC的简单搭建 (底层实现)
1.导入MVC依赖(添加web支持)
使用Idea做web开发时,注意:别忘了在项目结构中的 artifacts/WERB-NF下创建一个lib,并将依赖jar包全部导入其中
2.配置web.xml,注册DispatcherServlet
DispatcherServlet,是servlet上一层,对用户和servlet之间进行接收或转发(分发的作用),是SpringMVC的特有的,
因为有封装了一层,是程序更加简易
<?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"> <!--1.注册DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别-1--> <load-on-startup>1</load-on-startup> </servlet> <!--/ 匹配所有请求:(不包括,jsp)--> <!--/* 匹配所有请求:(包括,jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
注意:/和/*的区别
答:/:只匹配所有请求,不会去匹配jsp页面
/*:匹配所有请求,会去匹配jsp页面
3,创建控制器(类)
package com.king.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloController implements Controller { public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { //ModelAndView 模型和视图 ModelAndView mv = new ModelAndView(); //封装对象,放在ModelAndView mv.addObject("msg","HelloSpringMVC!"); //封装要跳转的视图,放在ModelAndView中 mv.setViewName("hello");//: /WEB-INF/jsp/hello.jsp return mv; } }
4,创建bean配置文件(Application.xml)
作用:将对应类导入springIOC容器中
<?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" 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/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--处理映射器(url处理器)--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--处理器适配器(url适配器)--> <!--视图解析器--> <!--视图解析器:DispatcherServlet给他的ModelAndView--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> <!--Handler--> <bean id="/hello" class="com.king.controller.HelloController"/> </beans>
SpringMVC工作流程小结:1,用户操作,生成一个请求(servlet)
2.这个servlet进入到SpringMVC的DispatcherServlet(请求分发器),通过映射处理器(BeanNameUrlHandlerMapping)的execution方法解析,获得参数,返回给请求分发器,
3.DispatcherServlet在将信息传给url/映射适配器,通过它去找相对应controller,控制层的接口实现类,
4.controller将已经封装好的model和view传回给适配器(HandlerAdapter),适配器再传给dispatcherServlet
5.最后跳转页面,并将model部署到页面中实现后台与用户的交互
四,注解实现
web映射
<?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"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Spring IOC容器
<?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: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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--指定要扫描的包,这个包下的注解就会生效--> <context:component-scan base-package="com.king.controller"/> <!--让springmvc不处理静态资源 .css .js .mp3 .mp4--> <mvc:default-servlet-handler/> <!-- 支持mvc注解驱动 在spring中一般采用@RequestMapping,如果向@RequestMapping生效, 必须向上下文中注册DefaultAnnotationHandleMapping,映射处理器(url处理) 和以一个AnnotationMethodHandleAdapter, 这两个一个是作用到类上,一个作用到方法上去处理 而annotation-driver配置自动帮助我们进行属性注入操作 --> <mvc:annotation-driven/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> </beans>
Controller模拟控制器接口
package com.king.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { //请求映射器 @RequestMapping("/h1") public String hello(Model model){ //封装数据 model.addAttribute("msg","hello,springmvc"); return "hello";//页面跳转 } }
@Controller:作用在类上,说明将当前类交给Spring容器托管
@RequestMapping 请求映射器
约束配置
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <!--将java代码目录中的xml输出,默认不输出除resources目录外的xml文件,--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>