当前位置: 首页 > news >正文

运城市住房和城乡建设局网站企业做网站400电话作用

运城市住房和城乡建设局网站,企业做网站400电话作用,100元建网站,选一个网站做seo一、SpringMVC简介 1. 什么是MVC MVC是一种软件架构的思想#xff0c;将软件按照模型、视图、控制器来划分 M#xff1a;Model#xff0c;模型层#xff0c;指工程中的JavaBean#xff0c;作用是处理数据 JavaBean分为两类#xff1a; 一类称为实体类Bean#xff1…一、SpringMVC简介 1. 什么是MVC MVC是一种软件架构的思想将软件按照模型、视图、控制器来划分 MModel模型层指工程中的JavaBean作用是处理数据 JavaBean分为两类 一类称为实体类Bean专门存储业务数据的如 Student、User 等一类称为业务处理 Bean指 Service 或 Dao 对象专门用于处理业务逻辑和数据访问。 VView视图层指工程中的html或jsp等页面作用是与用户进行交互展示数据 CController控制层指工程中的servlet作用是接收请求和响应浏览器 MVC的工作流程 用户通过视图层发送请求到服务器在服务器中请求被Controller接收Controller调用相应的Model层处理请求处理完毕将结果返回到ControllerController再根据请求处理的结果找到相应的View视图渲染数据后最终响应给浏览器 2. 什么是SpringMVC SpringMVC是Spring的一个后续产品是Spring的一个子项目 SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的首选方案。 注三层架构分为表述层或表示层、业务逻辑层、数据访问层表述层表示前台页面和后台servlet 3. SpringMVC的特点 Spring 家族原生产品与 IOC 容器等基础设施无缝对接基于原生的Servlet通过了功能强大的前端控制器DispatcherServlet对请求和响应进行统一处理表述层各细分领域需要解决的问题全方位覆盖提供全面解决方案代码清新简洁大幅度提升开发效率内部组件化程度高可插拔式组件即插即用想要什么功能配置相应组件即可性能卓著尤其适合现代大型、超大型互联网项目要求 二、HelloWorld 1. 开发环境 IDEidea 2019.2 JDKjdk1.8 构建工具maven3.5.4 服务器tomcat8.5 Spring版本5.3.1 2. 创建maven工程 创建SpringMVCDemo项目在其下创建SpringMVCDemo1模块项目架构如图所示 2.1 添加web模块 2.2 打包方式war 2.3 引入依赖 !-- 设置打包方式为war包 -- packagingwar/packaging dependencies!-- SpringMVC --dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.1/version/dependency!-- 日志 --dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.3/version/dependency!-- ServletAPI --dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/versionscopeprovided/scope/dependency!-- Spring5和Thymeleaf整合包 --dependencygroupIdorg.thymeleaf/groupIdartifactIdthymeleaf-spring5/artifactIdversion3.0.12.RELEASE/version/dependency /dependencies注由于 Maven 的传递性我们不必将所有需要的包全部配置依赖而是配置最顶端的依赖其他靠传递性导入。 3. 配置web.xml 注册SpringMVC的前端控制器DispatcherServlet先在main文件下创建WEB-INF/web.xml右键选择add Framework support自动生成 3.1 默认配置方式 此配置作用下SpringMVC的配置文件默认位于WEB-INF下默认名称为servlet-name-servlet.xml例如以下配置所对应SpringMVC的配置文件位于WEB-INF下文件名为springMVC-servlet.xml ?xml version1.0 encodingUTF-8? web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!-- 配置SpringMVC的前端控制器对浏览器发送的请求统一进行处理 --servletservlet-namespringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class/servletservlet-mappingservlet-namespringMVC/servlet-name!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--url-pattern//url-pattern/servlet-mapping /web-app3.2 扩展配置方式 可通过init-param标签设置SpringMVC配置文件的位置和名称通过load-on-startup标签设置SpringMVC前端控制器DispatcherServlet的初始化时间 ?xml version1.0 encodingUTF-8? web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!-- 配置SpringMVC的前端控制器对浏览器发送的请求统一进行处理 --servletservlet-namespringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --init-paramparam-namecontextConfigLocation/param-name!-- classpath表示类路径默认为resources文件夹下 --param-valueclasspath:spingMVC.xml/param-value/init-param!--作为框架的核心组件在启动过程中有大量的初始化操作要做而这些操作放在第一次请求时才执行会严重影响访问速度因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时--load-on-startup1/load-on-startup/servletservlet-mappingservlet-namespringMVC/servlet-name!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--url-pattern//url-pattern/servlet-mapping /web-app注 url-pattern标签中使用/和/*的区别 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求 因此就可以避免在访问jsp页面时该请求被DispatcherServlet处理从而找不到相应的页面 /*则能够匹配所有请求例如在使用过滤器时若需要对所有请求进行过滤就需要使用/*的写法 4. 创建请求控制器 由于前端控制器对浏览器发送的请求进行了统一的处理但是具体的请求有不同的处理过程因此需要创建处理具体请求的类即请求控制器 请求控制器中每一个处理请求的方法成为控制器方法 因为SpringMVC的控制器由一个POJO普通的Java类担任因此需要通过Controller注解将其标识为一个控制层组件交给Spring的IoC容器管理此时SpringMVC才能够识别控制器的存在 Controller public class HelloController {}5. 创建springMVC的配置文件 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd!-- 自动扫描包 --context:component-scan base-packageorg.atguigu.controller/!-- 配置Thymeleaf视图解析器 --bean idviewResolver classorg.thymeleaf.spring5.view.ThymeleafViewResolverproperty nameorder value1/property namecharacterEncoding valueUTF-8/property nametemplateEnginebean classorg.thymeleaf.spring5.SpringTemplateEngineproperty nametemplateResolverbean classorg.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver!-- 视图前缀 --property nameprefix value/WEB-INF/templates//!-- 视图后缀 --property namesuffix value.html/property nametemplateMode valueHTML5/property namecharacterEncoding valueUTF-8//bean/property/bean/property/bean!--处理静态资源例如html、js、css、jpg若只设置default-servlet-handler标签则只能访问静态资源其他请求则无法访问此时必须设置mvc:annotation-driven/解决问题--mvc:default-servlet-handler/!-- 开启MVC注解驱动 --mvc:annotation-drivenmvc:message-converters!-- 处理响应中文内容乱码 --bean classorg.springframework.http.converter.StringHttpMessageConverterproperty namedefaultCharset valueUTF-8/property namesupportedMediaTypeslistvaluetext/html/valuevalueapplication/json/value/list/property/bean/mvc:message-converters/mvc:annotation-driven /beans6. 测试HelloWorld 6.1 实现对首页的访问 在请求控制器中创建处理请求的方法 // RequestMapping注解处理请求和控制器方法之间的映射关系 // RequestMapping注解的value属性可以通过请求地址匹配请求/表示的当前工程的上下文路径 // localhost:8080/springMVC/ RequestMapping(/) public String index() {//设置视图名称return index; }6.2 通过超链接跳转到指定页面 在index.html位于WEB-INF/templates路径下中设置超链接 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8title首页/title /head bodyh1首页/h1a th:href{/hello}HelloWorld/abr/ /body /html在请求控制器中创建处理请求的方法 RequestMapping(/hello) public String HelloWorld() {return target; }测试在浏览器中访问http://localhost:8080/springMVC/springMVC此处指tomcat中配置上下文路径 7. 小结 浏览器发送请求若请求地址符合前端控制器的url-pattern该请求就会被前端控制器DispatcherServlet处理。前端控制器会读取SpringMVC的核心配置文件通过扫描组件找到控制器将请求地址和控制器中RequestMapping注解的value属性值进行匹配若匹配成功该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称该视图名称会被视图解析器解析加上前缀和后缀组成视图的路径通过Thymeleaf对视图进行渲染最终转发到视图所对应页面 三、RequestMapping注解 1. RequestMapping注解的功能 从注解名称上我们可以看到RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来建立映射关系。 SpringMVC 接收到指定的请求就会来找到在映射关系中对应的控制器方法来处理这个请求。 2. RequestMapping注解的位置 RequestMapping标识一个类设置映射请求的请求路径的初始信息 RequestMapping标识一个方法设置映射请求请求路径的具体信息 Controller RequestMapping(/test) public class RequestMappingController {//此时请求映射所映射的请求的请求路径为/test/testRequestMappingRequestMapping(/testRequestMapping)public String testRequestMapping(){return success;}}3. RequestMapping注解的value属性 RequestMapping注解的value属性通过请求的请求地址匹配请求映射 RequestMapping注解的value属性是一个字符串类型的数组表示该请求映射能够匹配多个请求地址所对应的请求 RequestMapping注解的value属性必须设置至少通过请求地址匹配请求映射 a th:href{/testRequestMapping}测试RequestMapping的value属性--/testRequestMapping/abr a th:href{/test}测试RequestMapping的value属性--/test/abrRequestMapping(value {/testRequestMapping, /test} ) public String testRequestMapping(){return success; }4. RequestMapping注解的method属性 RequestMapping注解的method属性通过请求的请求方式get或post匹配请求映射 RequestMapping注解的method属性是一个RequestMethod类型的数组表示该请求映射能够匹配多种请求方式的请求 若当前请求的请求地址满足请求映射的value属性但是请求方式不满足method属性则浏览器报错405Request method ‘POST’ not supported a th:href{/test}测试RequestMapping的value属性--/test/abr form th:action{/test} methodpostinput typesubmit /formRequestMapping(value {/testRequestMapping, /test},method {RequestMethod.GET, RequestMethod.POST} ) public String testRequestMapping(){return success; }注 1、对于处理指定请求方式的控制器方法SpringMVC中提供了RequestMapping的派生注解 处理get请求的映射–GetMapping 处理post请求的映射–PostMapping 处理put请求的映射–PutMapping 处理delete请求的映射–DeleteMapping 2、常用的请求方式有getpostputdelete 但是目前浏览器只支持get和post若在form表单提交时为method设置了其他请求方式的字符串put或delete则按照默认的请求方式get处理 若要发送put和delete请求则需要通过spring提供的过滤器HiddenHttpMethodFilter在RESTful部分会讲到 5. RequestMapping注解的params属性了解 RequestMapping注解的params属性通过请求的请求参数匹配请求映射 RequestMapping注解的params属性是一个字符串类型的数组可以通过四种表达式设置请求参数和请求映射的匹配关系理解为请求参数的约束条件 “param”要求请求映射所匹配的请求必须携带param请求参数 “!param”要求请求映射所匹配的请求必须不能携带param请求参数 “paramvalue”要求请求映射所匹配的请求必须携带param请求参数且paramvalue “param!value”要求请求映射所匹配的请求必须携带param请求参数但是param!value a th:href{/test(usernameadmin,password123456)测试RequestMapping的params属性--/test/abrRequestMapping(value {/testRequestMapping, /test},method {RequestMethod.GET, RequestMethod.POST},params {username,password!123456} ) public String testRequestMapping(){return success; }注 若当前请求满足RequestMapping注解的value和method属性但是不满足params属性此时页面回报错400Parameter conditions “username, password!123456” not met for actual request parameters: username{admin}, password{123456} 6. RequestMapping注解的headers属性了解 RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射 RequestMapping注解的headers属性是一个字符串类型的数组可以通过四种表达式设置请求头信息和请求映射的匹配关系 “header”要求请求映射所匹配的请求必须携带header请求头信息 “!header”要求请求映射所匹配的请求必须不能携带header请求头信息 “headervalue”要求请求映射所匹配的请求必须携带header请求头信息且headervalue “header!value”要求请求映射所匹配的请求必须携带header请求头信息且header!value 若当前请求满足RequestMapping注解的value和method属性但是不满足headers属性此时页面显示404错误即资源未找到 7. SpringMVC支持ant风格的路径 表示任意的单个字符 *表示任意的0个或多个字符 **表示任意的一层或多层目录 注意在使用**时只能使用/**/xxx的方式 8. SpringMVC支持路径中的占位符重点 原始方式/deleteUser?id1 RESTful方式/deleteUser/1 SpringMVC路径中的占位符常用于RESTful风格中当请求路径中将某些数据通过路径的方式传输到服务器中就可以在相应的RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据在通过PathVariable注解将占位符所表示的数据赋值给控制器方法的形参 a th:href{/testRest/123/admin} 测试路径中的占位符--/testRest/aRequestMapping(/testRest/{id}/{username}) public String testRest(PathVariable(id) String id, PathVariable(username) String username){System.out.println(id:id,username:username);return success; } //最终输出的内容为--id:123, username:admin四、SpringMVC获取请求参数 1. 通过ServletAPI获取请求参数 将HttpServletRequest作为控制器方法的形参此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象在JSP中常用 /*** 通过ServletAPI获取请求参数* param request* return*/RequestMapping(/testParam)public String testParam(HttpServletRequest request){String username request.getParameter(username);String password request.getParameter(password);System.out.println(username:username,password:password);return target;}2. 通过控制器方法的形参获取请求参数 在控制器方法的形参位置设置和请求参数同名的形参当浏览器发送请求匹配到请求映射时在DispatcherServlet中就会将请求参数赋值给相应的形参 a th:href{/testParam(usernameadmin,password123456)}测试获取请求参数--/testParam/abr/*** 通过控制器方法的形参获取请求参数testParam* return*/RequestMapping(/testParam2)public String testParam2(String username, String password){System.out.println(username:username,password:password);return target;}注 若请求所传输的请求参数中有多个同名的请求参数此时可以在控制器方法的形参中设置字符串数组或者字符串类型的形参接收此请求参数 若使用字符串数组类型的形参此参数的数组中包含了每一个数据 若使用字符串类型的形参此参数的值为每个数据中间使用逗号拼接的结果 3. 使用RequestParam获取请求参数 RequestParam是将请求参数和控制器方法的形参创建映射关系 RequestParam注解一共有三个常用属性 value指定为形参赋值的请求参数的参数名 required设置是否必须传输此请求参数默认值为true defaultValue设置参数的默认值 若required属性设置为true时则当前请求必须传输value所指定的请求参数若没有传输该请求参数且没有设置defaultValue属性则页面报错400Required String parameter ‘xxx’ is not present若设置为false则当前请求不是必须传输value所指定的请求参数若没有传输则注解所标识的形参的值为null defaultValue不管required属性值为true或false当value所指定的请求参数没有传输或传输的值为时则使用默认值为形参赋值 /*** 使用RequestParam获取请求参数* return*/RequestMapping(/testParam3)public String testParam3(RequestParam(value username,required true,defaultValue ) String username,RequestParam(value username,required false,defaultValue 123)String password){System.out.println(username:username,password:password);return target;}4. RequestHeader RequestHeader是将请求头信息和控制器方法的形参创建映射关系 RequestHeader注解一共有三个属性value、required、defaultValue用法同RequestParam 5. CookieValue CookieValue是将cookie数据和控制器方法的形参创建映射关系 CookieValue注解一共有三个属性value、required、defaultValue用法同RequestParam 6. 通过POJO实体获取请求参数 可以在控制器方法的形参位置设置一个实体类类型的形参此时若浏览器传输的请求参数的参数名和实体类中的属性名一致那么请求参数就会为此属性赋值 package org.atguigu.pojo;/*** author : Sakura* className : User* description : TODO* createTime : 2023-08-18 18:05:27*/ public class User {private String username;private String password;private String sex;private Integer age;private String email;public User(String username, String password, String sex, Integer age, String email) {this.username username;this.password password;this.sex sex;this.age age;this.email email;}public User() {}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getEmail() {return email;}public void setEmail(String email) {this.email email;}Overridepublic String toString() {return User{ username username \ , password password \ , sex sex \ , age age , email email \ };} } form th:action{/testpojo} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbr性别input typeradio namesex value男男input typeradio namesex value女女br年龄input typetext nameagebr邮箱input typetext nameemailbrinput typesubmit /form/*** 通过POJO实体获取请求参数* param user* return*/RequestMapping(/testpojo)public String testPOJO(User user) {System.out.println(user);return target;} //最终结果--User{username419133email.ncu.edu.cn, password14124, sex??·, age33, email9875qq.com}7. 解决获取请求参数的乱码问题 解决获取请求参数的乱码问题可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter但是必须在web.xml中进行注册 !--配置springMVC的编码过滤器-- filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceResponseEncoding/param-nameparam-valuetrue/param-value/init-param /filter filter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern /filter-mappingSpringMVC中处理编码的过滤器一定要配置到其他过滤器之前否则无效 五、域对象共享数据 request、session和application的区别 HttpServerltRequestrequest HttpSession: session request.getSession() ServletContextapplicationContext reqest.getServletContext() / session.getServletContext() 1. 使用ServletAPI向request域对象共享数据 RequestMapping(/testServletAPI) public String testServletAPI(HttpServletRequest request){request.setAttribute(testScope, hello,servletAPI);return success; }2. 使用ModelAndView向request域对象共享数据 RequestMapping(/testModelAndView) public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图实现页面跳转*/ModelAndView mav new ModelAndView();//向请求域共享数据mav.addObject(testScope, hello,ModelAndView);//设置视图实现页面跳转mav.setViewName(success);return mav; }3. 使用Model向request域对象共享数据 RequestMapping(/testModel) public String testModel(Model model){model.addAttribute(testScope, hello,Model);return success; }在前端获取Model传递的值 4. 使用map向request域对象共享数据 RequestMapping(/testMap) public String testMap(MapString, Object map){map.put(testScope, hello,Map);return success; }5. 使用ModelMap向request域对象共享数据 RequestMapping(/testModelMap) public String testModelMap(ModelMap modelMap){modelMap.addAttribute(testScope, hello,ModelMap);return success; }6. Model、ModelMap、Map的关系 Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的 public interface Model{} public class ModelMap extends LinkedHashMapString, Object {} public class ExtendedModelMap extends ModelMap implements Model {} public class BindingAwareModelMap extends ExtendedModelMap {}7. 向session域共享数据 RequestMapping(/testSession) public String testSession(HttpSession session){session.setAttribute(testSessionScope, hello,session);return success; }8. 向application域共享数据 RequestMapping(/testApplication) public String testApplication(HttpSession session){ServletContext application session.getServletContext();application.setAttribute(testApplicationScope, hello,application);return success; }六、SpringMVC的视图 SpringMVC中的视图是View接口视图的作用渲染数据将模型Model中的数据展示给用户 SpringMVC视图的种类很多默认有转发视图和重定向视图 当工程引入JSTL的依赖转发视图会自动转换为JstlView 若使用的视图技术为Thymeleaf在SpringMVC的配置文件中配置了Thymeleaf的视图解析器由此视图解析器解析之后所得到的是ThymeleafView 1. ThymeleafView 当控制器方法中所设置的视图名称没有任何前缀时此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析视图名称拼接视图前缀和视图后缀所得到的最终路径会通过转发的方式实现跳转 RequestMapping(/testHello) public String testHello(){return hello; }2. 转发视图InternalResourceView SpringMVC中默认的转发视图是InternalResourceView SpringMVC中创建转发视图的情况 当控制器方法中所设置的视图名称以forward:为前缀时创建InternalResourceView视图此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析而是会将前缀forward:去掉剩余部分作为最终路径通过转发的方式实现跳转 例如forward:/“forward:/employee” RequestMapping(/testForward) public String testForward(){return forward:/testHello; }3. 重定向视图RedirectView SpringMVC中默认的重定向视图是RedirectView 当控制器方法中所设置的视图名称以redirect:为前缀时创建RedirectView视图此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析而是会将前缀redirect:去掉剩余部分作为最终路径通过重定向的方式实现跳转 例如redirect:/“redirect:/employee” RequestMapping(/testRedirect) public String testRedirect(){return redirect:/testHello; }注 重定向视图在解析时会先将redirect:前缀去掉然后会判断剩余部分是否以/开头若是则会自动拼接上下文路径 4. 视图控制器view-controller 当控制器方法中仅仅用来实现页面跳转即只需要设置视图名称时可以将处理器方法使用view-controller标签进行表示 !--path设置处理的请求地址view-name设置请求地址所对应的视图名称 -- mvc:view-controller path/testView view-namesuccess/mvc:view-controller注 当SpringMVC中设置任何一个view-controller时其他控制器中的请求映射将全部失效此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签 mvc:annotation-driven / 七、RESTful引入 1. RESTful简介 RESTRepresentational State Transfer表现层资源状态转移。 1.1 资源 资源是一种看待服务器的方式即将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念所以它不仅仅能代表服务器文件系统中的一个文件、数据库中的一张表等等具体的东西可以将资源设计的要多抽象有多抽象只要想象力允许而且客户端应用开发者能够理解。与面向对象设计类似资源是以名词为核心来组织的首先关注的是名词。一个资源可以由一个或多个URI来标识。URI既是资源的名称也是资源在Web上的地址。对某个资源感兴趣的客户端应用可以通过资源的URI与其进行交互。 1.2 资源的表述 资源的表述是一段对于资源在某个特定时刻的状态的描述。可以在客户端-服务器端之间转移交换。资源的表述可以有多种格式例如HTML/XML/JSON/纯文本/图片/视频/音频等等。资源的表述格式可以通过协商机制来确定。请求-响应方向的表述通常使用不同的格式。 1.3 状态转移 状态转移说的是在客户端和服务器端之间转移transfer代表资源状态的表述。通过转移和操作资源的表述来间接实现操作资源的目的。 2. RESTful的实现 具体说就是 HTTP 协议里面四个表示操作方式的动词GET、POST、PUT、DELETE。 它们分别对应四种基本操作GET 用来获取资源POST 用来新建资源PUT 用来更新资源DELETE 用来删除资源。 REST 风格提倡 URL 地址使用统一的风格设计从前到后各个单词使用斜杠分开不使用问号键值对方式携带请求参数而是将要发送给服务器的数据作为 URL 地址的一部分以保证整体风格的一致性。 操作传统方式REST风格查询操作getUserById?id1user/1–get请求方式保存操作saveUseruser–post请求方式删除操作deleteUser?id1user/1–delete请求方式更新操作updateUseruser–put请求方式 3. HiddenHttpMethodFilter 由于浏览器只支持发送get和post方式的请求那么该如何发送put和delete请求呢 SpringMVC 提供了 HiddenHttpMethodFilter 帮助我们将 POST 请求转换为 DELETE 或 PUT 请求 HiddenHttpMethodFilter 处理put和delete请求的条件 当前请求的请求方式必须为post 当前请求必须传输请求参数_method 满足以上条件HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值因此请求参数_method的值才是最终的请求方式 在web.xml中注册HiddenHttpMethodFilter filterfilter-nameHiddenHttpMethodFilter/filter-namefilter-classorg.springframework.web.filter.HiddenHttpMethodFilter/filter-class /filter filter-mappingfilter-nameHiddenHttpMethodFilter/filter-nameurl-pattern/*/url-pattern /filter-mapping注 目前为止SpringMVC中提供了两个过滤器CharacterEncodingFilter和HiddenHttpMethodFilter 在web.xml中注册时必须先注册CharacterEncodingFilter再注册HiddenHttpMethodFilter 原因 在 CharacterEncodingFilter 中通过 request.setCharacterEncoding(encoding) 方法设置字符集的 request.setCharacterEncoding(encoding) 方法要求前面不能有任何获取请求参数的操作 而 HiddenHttpMethodFilter 恰恰有一个获取请求方式的操作 String paramValue request.getParameter(this.methodParam);八、RESTful案例 新建SpringMVCDemo2模块架构如图 1. 准备工作 和传统 CRUD 一样实现对员工信息的增删改查。 搭建环境 pom.xml设置打包方式引入依赖 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.atguigu/groupIdartifactIdSpringMVCDemo/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdSpringMVCDemo2/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties!-- 设置打包方式为war包 --packagingwar/packagingdependencies!-- SpringMVC --dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.1/version/dependency!-- 日志 --dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.3/version/dependency!-- ServletAPI --dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/versionscopeprovided/scope/dependency!-- Spring5和Thymeleaf整合包 --dependencygroupIdorg.thymeleaf/groupIdartifactIdthymeleaf-spring5/artifactIdversion3.0.12.RELEASE/version/dependency/dependencies /projectweb.xml ?xml version1.0 encodingUTF-8? web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!-- 配置SpringMVC的前端控制器对浏览器发送的请求统一进行处理 --servletservlet-namespringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --init-paramparam-namecontextConfigLocation/param-name!-- classpath表示类路径默认为resources文件夹下 --param-valueclasspath:springMVC.xml/param-value/init-param!--作为框架的核心组件在启动过程中有大量的初始化操作要做而这些操作放在第一次请求时才执行会严重影响访问速度因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时--load-on-startup1/load-on-startup/servletservlet-mappingservlet-namespringMVC/servlet-name!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--url-pattern//url-pattern/servlet-mapping!--配置springMVC的编码过滤器--filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceResponseEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping!-- 开启Restful开发 --filterfilter-nameHiddenHttpMethodFilter/filter-namefilter-classorg.springframework.web.filter.HiddenHttpMethodFilter/filter-class/filterfilter-mappingfilter-nameHiddenHttpMethodFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping/web-appSpringMVC配置文件 springMVC.xml beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd!-- 自动扫描包 --context:component-scan base-packageorg.atguigu/!-- 配置Thymeleaf视图解析器 --bean idviewResolver classorg.thymeleaf.spring5.view.ThymeleafViewResolverproperty nameorder value1/property namecharacterEncoding valueUTF-8/property nametemplateEnginebean classorg.thymeleaf.spring5.SpringTemplateEngineproperty nametemplateResolverbean classorg.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver!-- 视图前缀 --property nameprefix value/WEB-INF/templates//!-- 视图后缀 --property namesuffix value.html/property nametemplateMode valueHTML5/property namecharacterEncoding valueUTF-8//bean/property/bean/property/bean!--处理静态资源例如html、js、css、jpg若只设置default-servlet-handler标签则只能访问静态资源其他请求则无法访问此时必须设置mvc:annotation-driven/解决问题--mvc:default-servlet-handler/!-- 开启MVC注解驱动 --mvc:annotation-drivenmvc:message-converters!-- 处理响应中文内容乱码 --bean classorg.springframework.http.converter.StringHttpMessageConverterproperty namedefaultCharset valueUTF-8/property namesupportedMediaTypeslistvaluetext/html/valuevalueapplication/json/value/list/property/bean/mvc:message-converters/mvc:annotation-driven/beans准备实体类 package org.atguigu.pojo;/*** author : Sakura* className : Employee* description : TODO* createTime : 2023-08-18 19:52:01*/ public class Employee {private Integer id;private String lastName;private String email;//1 male, 0 femaleprivate Integer gender;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName lastName;}public String getEmail() {return email;}public void setEmail(String email) {this.email email;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender gender;}public Employee(Integer id, String lastName, String email, Integer gender) {super();this.id id;this.lastName lastName;this.email email;this.gender gender;}public Employee() {} } 准备dao模拟数据 package org.atguigu.dao;import org.atguigu.pojo.Employee; import org.springframework.stereotype.Repository;import java.util.Collection; import java.util.HashMap; import java.util.Map;/*** author : Sakura* className : EmployeeDao* description : TODO* createTime : 2023-08-18 19:53:15*/ Repository public class EmployeeDao {private static MapInteger, Employee employees null;static{employees new HashMapInteger, Employee();employees.put(1001, new Employee(1001, E-AA, aa163.com, 1));employees.put(1002, new Employee(1002, E-BB, bb163.com, 1));employees.put(1003, new Employee(1003, E-CC, cc163.com, 0));employees.put(1004, new Employee(1004, E-DD, dd163.com, 0));employees.put(1005, new Employee(1005, E-EE, ee163.com, 1));}private static Integer initId 1006;public void save(Employee employee){if(employee.getId() null){employee.setId(initId);}employees.put(employee.getId(), employee);}public CollectionEmployee getAll(){return employees.values();}public Employee get(Integer id){return employees.get(id);}public void delete(Integer id){employees.remove(id);} }2. 功能清单 功能URL 地址请求方式访问首页√/GET查询全部数据√/employeeGET删除√/employee/2DELETE跳转到添加数据页面√/toAddGET执行保存√/employeePOST跳转到更新数据页面√/employee/2GET执行更新√/employeePUT 3. 具体功能访问首页 3.1 配置view-controller !--view-controller仅用于页面跳转path设置处理的请求地址view-name设置请求地址所对应的视图名称--mvc:view-controller path/ view-nameindex /3.2 创建页面 在templates文件夹下新建index.html内容如下 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8 titleTitle/title /head body h1首页/h1a th:href{/employee}查看员工信息/a /body /html4. 具体功能查询所有员工数据 4.1 控制器方法 Autowiredprivate EmployeeDao employeeDao;/*** 列表* param model* return*/RequestMapping(value /employee, method RequestMethod.GET)public String getEmployeeList(Model model){CollectionEmployee employeeList employeeDao.getAll();model.addAttribute(employeeList, employeeList);return employee_list;}4.2 创建employee_list.html 在templates文件夹下新建employee_list.html内容如下 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8titleEmployee Info/titlescript typetext/javascript th:src{/static/js/vue.js}/script /head bodytable border1 cellpadding0 cellspacing0 styletext-align: center; iddataTabletrth colspan5Employee Info/th/trtrthid/ththlastName/ththemail/ththgender/ththoptions(a th:href{/toAdd}add/a)/th/trtr th:eachemployee : ${employeeList}td th:text${employee.id}/tdtd th:text${employee.lastName}/tdtd th:text${employee.email}/tdtd th:text${employee.gender}/tdtda classdeleteA clickdeleteEmployee th:href{/employee/${employee.id}}delete/aa th:href{/employee/${employee.id}}update/a/td/tr/table /body /html5. 具体功能删除 5.1 创建处理delete请求方式的表单 在employee_list.html添加表单 !-- 作用通过超链接控制表单的提交将post请求转换为delete请求 -- form iddelete_form methodpost!-- HiddenHttpMethodFilter要求必须传输_method请求参数并且值为最终的请求方式 --input typehidden name_method valuedelete/ /form5.2 删除超链接绑定点击事件 在 employee_list.html 引入vue.js script typetext/javascript th:src{/static/js/vue.js}/script删除超链接 a classdeleteA clickdeleteEmployee th:href{/employee/${employee.id}}delete/a在employee_list.html添加如下内容实现通过vue处理点击事件 script typetext/javascriptvar vue new Vue({el:#dataTable,methods:{//event表示当前事件deleteEmployee:function (event) {//通过id获取表单标签var delete_form document.getElementById(delete_form);//将触发事件的超链接的href属性为表单的action属性赋值delete_form.action event.target.href;//提交表单delete_form.submit();//阻止超链接的默认跳转行为event.preventDefault();}}}); /script5.3 控制器方法 /*** 删除* param employeeId* return*/RequestMapping(value /employee/{id}, method RequestMethod.DELETE)public String deleteEmployee(PathVariable(id)Integer employeeId){employeeDao.delete(employeeId);return redirect:/employee;}6. 具体功能跳转到添加数据页面 6.1 配置view-controller mvc:view-controller path/toAdd view-nameemployee_add /6.2 创建employee_add.html 在templates文件夹下新建employee_add.html内容如下 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8titleAdd Employee/title /head bodyform th:action{/employee} methodpostlastName:input typetext namelastNamebremail:input typetext nameemailbrgender:input typeradio namegender value1maleinput typeradio namegender value0femalebrinput typesubmit valueaddbr /form/body /html7. 具体功能执行保存 7.1 控制器方法 /*** 添加* param employee* return*/RequestMapping(value /employee, method RequestMethod.POST)public String addEmployee(Employee employee){employeeDao.save(employee);return redirect:/employee;} 8. 具体功能跳转到更新数据页面 8.1 修改超链接 a th:href{/employee/${employee.id}}update/a8.2 控制器方法 /*** 跳转到更新页面* param employeeId* return*/RequestMapping(value /employee/{id}, method RequestMethod.GET)public ModelAndView toUpdateEmployee(PathVariable(id)Integer employeeId){Employee employee employeeDao.get(employeeId);ModelAndView modelAndView new ModelAndView(employee_update);modelAndView.addObject(employee, employee);return modelAndView;}8.3 创建employee_update.html 在templates文件夹下新建employee_update.html内容如下 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8titleUpdate Employee/title /head bodyform th:action{/employee} methodpostinput typehidden name_method valueputinput typehidden nameid th:value${employee.id}lastName:input typetext namelastName th:value${employee.lastName}bremail:input typetext nameemail th:value${employee.email}br!--th:field${employee.gender}可用于单选框或复选框的回显若单选框的value和employee.gender的值一致则添加checkedchecked属性--gender:input typeradio namegender value1 th:field${employee.gender}/maleinput typeradio namegender value0 th:field${employee.gender}/femalebrinput typesubmit valueupdate/br /form/body /html9. 具体功能执行更新 9.1 控制器方法 /*** 更新*/RequestMapping(value /employee, method RequestMethod.PUT)public String updateEmployee(Employee employee){employeeDao.save(employee);return redirect:/employee;}八、HttpMessageConverter HttpMessageConverter报文信息转换器将请求报文转换为Java对象或将Java对象转换为响应报文 HttpMessageConverter提供了两个注解和两个类型RequestBodyResponseBodyRequestEntity ResponseEntity 1. RequestBody RequestBody可以获取请求体需要在控制器方法设置一个形参使用RequestBody进行标识当前请求的请求体就会为当前注解所标识的形参赋值 form th:action{/testRequestBody} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbrinput typesubmit /formRequestMapping(/testRequestBody) public String testRequestBody(RequestBody String requestBody){System.out.println(requestBody:requestBody);return success; }输出结果 requestBody:usernameadminpassword123456 2. RequestEntity RequestEntity封装请求报文的一种类型需要在控制器方法的形参中设置该类型的形参当前请求的请求报文就会赋值给该形参可以通过getHeaders()获取请求头信息通过getBody()获取请求体信息 RequestMapping(/testRequestEntity) public String testRequestEntity(RequestEntityString requestEntity){System.out.println(requestHeader:requestEntity.getHeaders());System.out.println(requestBody:requestEntity.getBody());return success; }输出结果 requestHeader:[host:“localhost:8080”, connection:“keep-alive”, content-length:“27”, cache-control:“max-age0”, sec-ch-ua:“” Not A;Brand;v“99”, “Chromium”;v“90”, “Google Chrome”;v“90”, sec-ch-ua-mobile:“?0”, upgrade-insecure-requests:“1”, origin:“http://localhost:8080”, user-agent:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36”] requestBody:usernameadminpassword123 3. ResponseBody ResponseBody用于标识一个控制器方法可以将该方法的返回值直接作为响应报文的响应体响应到浏览器 RequestMapping(/testResponseBody) ResponseBody public String testResponseBody(){return success; }结果浏览器页面显示success而不是如之前一般去渲染success.html 4. SpringMVC处理JSON ResponseBody处理JSON的步骤 4.1 导入jackson的依赖 dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.12.1/version /dependency4.2 开启注解驱动 在SpringMVC的核心配置文件中开启mvc的注解驱动此时在HandlerAdaptor中会自动装配一个消息转换器MappingJackson2HttpMessageConverter可以将响应到浏览器的Java对象转换为Json格式的字符串 mvc:annotation-driven /4.3 使用ResponseBody注解 在处理器方法上使用ResponseBody注解进行标识 4.3 返回JSON格式字符串 将Java对象直接作为控制器方法的返回值返回就会自动转换为JSON格式的字符串 RequestMapping(/testResponseUser) ResponseBody public User testResponseUser(){return new User(1001,admin,123456,23,男); }使用Postman进行测试 5. SpringMVC处理ajax 5.1 HTML页面请求 div idappa th:href{/testAjax} clicktestAjaxtestAjax/abr /div5.2 通过vue和axios处理点击事件 script typetext/javascript th:src{/static/js/vue.js}/script script typetext/javascript th:src{/static/js/axios.min.js}/script script typetext/javascriptvar vue new Vue({el:#app,methods:{testAjax:function (event) {axios({method:post,url:event.target.href,params:{username:admin,password:123456}}).then(function (response) {alert(response.data);});event.preventDefault();}}}); /script5.3 控制器方法 /*** 测试ajax交互* param username* param password* return*/RequestMapping(/testAjax)ResponseBodypublic String testAjax(String username, String password){System.out.println(username:username,password:password);return hello,ajax;}6. RestController注解 RestController注解是springMVC提供的一个复合注解标识在控制器的类上就相当于为类添加了Controller注解并且为其中的每个方法添加了ResponseBody注解 即RestController Controller ResponseBody 7. ResponseEntity ResponseEntity用于控制器方法的返回值类型该控制器方法的返回值就是响应到浏览器的响应报文 九、文件上传和下载 1. 文件上传 文件上传要求form表单的请求方式必须为post并且添加属性enctypemultipart/form-data springMVC.xml添加如下代码 !--view-controller仅用于页面跳转path设置处理的请求地址view-name设置请求地址所对应的视图名称--mvc:view-controller path/toUpload view-nameuploadFile /在templates文件夹下新建 uploadFile.html !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8titleTitle/title /head bodyform th:action{/upload} methodpost enctypemultipart/form-datainput typefile nameavator 文件上传/inputbr/input typesubmit value提交/ /form /body /htmlSpringMVC中将上传的文件封装到MultipartFile对象中通过此对象可以获取文件相关信息 操作步骤 1.1 添加依赖 !-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -- dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.3.1/version /dependency1.2 在SpringMVC的配置文件中添加配置 !--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-- bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver /1.3 控制器方法 package org.atguigu.controller;import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.UUID;/*** author : Sakura* className : FileController* description : TODO* createTime : 2023-08-18 21:23:04*/ Controller public class FileController {/*** 文件上传* param multipartFile* param session* return*/RequestMapping(/upload)ResponseBodypublic String upload(RequestParam(avator) MultipartFile multipartFile, HttpSession session) {// 获取上传的文件的文件名String fileName multipartFile.getOriginalFilename();// 获取文件后缀名通过UUID处理文件重名问题String suffix fileName.substring(fileName.lastIndexOf(.));fileName UUID.randomUUID() suffix;// 获取服务器中photo目录的路径ServletContext servletContext session.getServletContext();String imagePath servletContext.getRealPath(/static/images/);// 文件上传目录不存在则创建File file new File(imagePath);if (!file.exists()){file.mkdirs();}// 文件上传String newFileName imagePath File.separator fileName;try {multipartFile.transferTo(new File(newFileName));} catch (IOException e) {throw new RuntimeException(e);}// 返回文件上传后的完整路径return fileName;} } 测试结果 2. 文件下载 使用ResponseEntity实现下载文件的功能 /*** 文件下载* param session* param fileName* return*/RequestMapping(/download)public ResponseEntitybyte[] testResponseEntity(HttpSession session, String fileName) {// 获取ServletContext对象ServletContext servletContext session.getServletContext();// 获取服务器中文件的真实路径String realPath servletContext.getRealPath(/static/images/ fileName);// 创建输入流InputStream inputStream null;// 创建ResponseEntity对象ResponseEntitybyte[] responseEntity null;try {inputStream new FileInputStream(realPath);// 创建字节数组byte[] bytes new byte[inputStream.available()];// 将流读到字节数组中inputStream.read(bytes);// 创建HttpHeaders对象设置响应头信息MultiValueMapString, String httpHeaders new HttpHeaders();// 设置要下载方式以及下载文件的名字httpHeaders.add(Content-Disposition, attachment;filename fileName);// 设置响应状态码HttpStatus httpStatus HttpStatus.OK;responseEntity new ResponseEntity(bytes, httpHeaders, httpStatus);} catch (IOException exception) {throw new RuntimeException(exception);} finally {// 关闭输入流try {if (inputStream ! null) {inputStream.close();}} catch (IOException e) {throw new RuntimeException(e);}}return responseEntity;} 测试在浏览器地址栏输入 http://localhost:8080/SpringMVC/download?fileName1baf7e5e-cc34-4662-9aa0-5d1bb48cd956.jpg 会把之前上传的图片进行下载。 图片被上传到了Tomcat的webapps文件夹其下每一个文件夹表示一个项目。 备注header中Content–Disposition的作用与使用方法 十、拦截器 1. 拦截器的配置 SpringMVC中的拦截器用于拦截控制器方法的执行SpringMVC中的拦截器需要实现HandlerInterceptor 修改EmployeeController.java RequestMapping(value /employee, method RequestMethod.GET)public String getEmployeeList(Model model){CollectionEmployee employeeList employeeDao.getAll();employeeList.forEach(System.out::println);model.addAttribute(employeeList, employeeList);return employee_list;}package org.atguigu.interceptor;import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** author : Sakura* className : MyInterceptor* description : 自定义interceptor* createTime : 2023-08-19 09:28:19*/ Component public class MyInterceptor implements HandlerInterceptor {/*** 控制器方法执行之前执行preHandle()其boolean类型的返回值表示是否拦截或放行返回true为放行即调用控制器方法返回false表示拦截即不调用控制器方法* param request current HTTP request* param response current HTTP response* param handler chosen handler to execute, for type and/or instance evaluation* return* throws Exception*/Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println(preHandle方法被执行...);return HandlerInterceptor.super.preHandle(request, response, handler);}/*** 控制器controller方法执行之后执行postHandle()* param request current HTTP request* param response current HTTP response* param handler the handler (or {link Object}) that started asynchronous* execution, for type and/or instance examination* param modelAndView the {code ModelAndView} that the handler returned* (can also be {code null})* throws Exception*/Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println(postHandle方法被执行...);HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);}/*** 处理完视图和模型数据渲染视图完毕之后执行afterComplation()* param request current HTTP request* param response current HTTP response* param handler the handler (or {link Object}) that started asynchronous* execution, for type and/or instance examination* param ex any exception thrown on handler execution, if any; this does not* include exceptions that have been handled through an exception resolver* throws Exception*/Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(afterCompletion方法被执行...);HandlerInterceptor.super.afterCompletion(request, response, handler, ex);} }SpringMVC的拦截器必须在SpringMVC的配置文件中进行配置 bean idmyInterceptor classorg.atguigu.interceptor.MyInterceptor/!-- 配置SpringMVC的拦截器 --mvc:interceptors!-- 以上两种配置方式都是对DispatcherServlet所处理的所有的请求进行拦截 --mvc:interceptor!-- 通过mvc:mapping设置需要拦截的请求--!-- mvc:mapping path/*// --mvc:mapping path/employee/!-- 通过mvc:exclude-mapping设置需要排除的请求即不需要拦截的请求--!-- mvc:exclude-mapping path/employee/ / --ref beanmyInterceptor //mvc:interceptor/mvc:interceptors2. 拦截器的三个抽象方法 SpringMVC中的拦截器有三个抽象方法 2.1 preHandle preHandle方法在业务处理器处理请求之前被调用在该方法中对用户请求request进行处理。如果决定该拦截器对请求进行拦截处理后还要调用其他拦截器或是业务处理器去进行处理则返回true如果程序员决定不需要再调用其他组件去处理请求则返回false。在该方法中可以进行权限管理日志事务等。 2.2 postHandle postHandle方法在业务处理器处理完请求后但DispatcherServlet向客户端返回响应前被调用在该方法中对用户请求request进行处理。在该方法中可以对请求域中的数据进行修改。 2.3 afterComplation 处理完视图和模型数据渲染视图完毕之后执行afterComplation()。同时afterComplation方法还会进行一些资源的处理操作比如释放资源等。 故三者执行顺序preHandle -- postHandle -- afterComplation 3. 多个拦截器的执行顺序 若每个拦截器的preHandle()都返回true ​ 此时多个拦截器的执行顺序和拦截器在SpringMVC的配置文件的配置顺序有关preHandle()会按照配置的顺序执行而postHandle()和afterComplation()会按照配置的反序执行 若某个拦截器的preHandle()返回了false ​ preHandle()返回false和它之前的拦截器的preHandle()都会执行postHandle()都不执行返回false的拦截器之前的拦截器的afterComplation()会执行 十一、异常处理器 1. 基于xml配置的异常处理 SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口HandlerExceptionResolver HandlerExceptionResolver接口的实现类有DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver使用方式 !-- SpringMVC提供的的异常处理器DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver --bean classorg.springframework.web.servlet.handler.SimpleMappingExceptionResolverproperty nameexceptionMappingsprops!--properties的键表示处理器方法执行过程中出现的异常properties的值表示若出现指定异常时设置一个新的视图名称跳转到指定页面此处如果出现ArithmeticException异常error会经过视图解析器处理跳转到error.html(error拼接前后缀)--prop keyjava.lang.ArithmeticExceptionerror/prop/props/property!--exceptionAttribute属性设置一个属性名将出现的异常信息在请求域中进行共享--property nameexceptionAttribute valueex //bean编写测试方法 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8 titleTitle/title /head body h1Error/h1 div idappspan th:text${errorInfo}/span/div /body /html/*** 测试ajax交互* param username* param password* return*/RequestMapping(/testAjax)ResponseBodypublic String testAjax(String username, String password){System.out.println(1/0); // ArithmeticException异常System.out.println(username:username,password:password);return hello,ajax;}控制台日志 10:22:39.932 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to org.atguigu.controller.EmployeeController#testAjax(String, String) 10:22:39.961 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view error based on mapping [java.lang.ArithmeticException] 10:22:39.961 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolved [java.lang.ArithmeticException: / by zero] to ModelAndView [viewerror; model{errorInfojava.lang.ArithmeticException: / by zero}] 10:22:39.961 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - Using resolved error view: ModelAndView [viewerror; model{errorInfojava.lang.ArithmeticException: / by zero}] 10:22:39.985 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK测试 2. 基于注解的异常处理 注释掉上面基于XML配置的异常处理改为使用注解方式进行一场处理。 package org.atguigu.controller;import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler;/*** author : Sakura* className : ExceptionController* description : TODO* createTime : 2023-08-22 21:04:52*/ // ControllerAdvice将当前类标识为异常处理的组件 ControllerAdvice public class ExceptionController {/*** param ex 当前请求处理中出现的异常对象* param model* return* ExceptionHandler用于设置所标识方法处理的异常*/ExceptionHandler(ArithmeticException.class)public String handleArithmeticException(Exception ex, Model model) {System.out.println(ExceptionController-handleArithmeticException: ex.getMessage());// 前端接收${errorInfo}model.addAttribute(errorInfo, ex);return error;} }十二、注解配置SpringMVC 使用配置类和注解代替web.xml和SpringMVC配置文件的功能 1. 创建初始化类代替web.xml 在Servlet3.0环境中容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类如果找到的话就用它来配置Servlet容器。 Spring提供了这个接口的实现名为SpringServletContainerInitializer这个类反过来又会查找实现WebApplicationInitializer的类并将配置的任务交给它们来完成。Spring3.2引入了一个便利的WebApplicationInitializer基础实现名为AbstractAnnotationConfigDispatcherServletInitializer当我们的类扩展了AbstractAnnotationConfigDispatcherServletInitializer并将其部署到Servlet3.0容器的时候容器会自动发现它并用它来配置Servlet上下文。 package org.atguigu.config;import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.filter.HiddenHttpMethodFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import javax.servlet.Filter;/*** author : Sakura* className : WebInit* description : TODO* createTime : 2023-08-22 21:38:37*/ public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {/*** 指定spring的配置类* return*/Overrideprotected Class?[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}/*** 指定SpringMVC的配置类* return*/Overrideprotected Class?[] getServletConfigClasses() {return new Class[]{WebConfig.class};}/*** 指定DispatcherServlet的映射规则即url-pattern* return*/Overrideprotected String[] getServletMappings() {return new String[]{/};}/*** 添加过滤器* return*/Overrideprotected Filter[] getServletFilters() {// 设置字符串filterCharacterEncodingFilter encodingFilter new CharacterEncodingFilter();encodingFilter.setEncoding(UTF-8);encodingFilter.setForceRequestEncoding(true);// 设置Restful风格的filterHiddenHttpMethodFilter hiddenHttpMethodFilter new HiddenHttpMethodFilter();return new Filter[]{encodingFilter, hiddenHttpMethodFilter};} }2. 创建SpringConfig配置类代替Spring的配置文件 package org.atguigu.config;import org.springframework.context.annotation.Configuration;/*** author : Sakura* className : SpringConfig* description : TODO* createTime : 2023-08-22 21:45:24*/ // Spring配置类 Configuration public class SpringConfig { } 3. 创建WebConfig配置类代替SpringMVC的配置文件 package org.atguigu.config;import org.atguigu.interceptor.MyInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver;import java.util.List; import java.util.Properties;/*** author : Sakura* className : WebConfig* description : TODO* createTime : 2023-08-22 21:46:01*/// SpringMVC配置类 Configuration ComponentScan(org.atguigu) // 设置包扫描组件 EnableWebMvc // 开启SpringMVC注解 public class WebConfig implements WebMvcConfigurer {/*** 使用默认的servlet处理资源* param configurer*/Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}/*** 配置文件上传解析器* return*/Beanpublic CommonsMultipartResolver multipartResolver(){return new CommonsMultipartResolver();}/*** 配置拦截器* param registry*/Overridepublic void addInterceptors(InterceptorRegistry registry) {MyInterceptor myInterceptor new MyInterceptor();registry.addInterceptor(myInterceptor).addPathPatterns(/**);}/*** view-controller配置试图控制* param registry*/Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController(/toUpload).setViewName(uploadFile);}/*** 使用HandlerExceptionResolver接口的实现类SimpleMappingExceptionResolver实现异常处理* param resolvers initially an empty list*/Overridepublic void configureHandlerExceptionResolvers(ListHandlerExceptionResolver resolvers) {SimpleMappingExceptionResolver simpleMappingExceptionResolver new SimpleMappingExceptionResolver();Properties properties new Properties();// 前者为要捕获的异常后者为映射的view页面properties.setProperty(java.lang.ArithmeticException, error);// 设置异常异常映射simpleMappingExceptionResolver.setExceptionMappings(properties);// 设置请求域共享异常信息的键simpleMappingExceptionResolver.setExceptionAttribute(errorInfo);resolvers.add(simpleMappingExceptionResolver);}/*** 配置生成模板解析器* return*/Beanpublic ITemplateResolver templateResolver(){WebApplicationContext applicationContext ContextLoader.getCurrentWebApplicationContext();// ServletContextTemplateResolver需要一个ServletContext作为构造参数可通过WebApplicationContext 的方法获得ServletContextTemplateResolver templateResolver new ServletContextTemplateResolver(applicationContext.getServletContext());templateResolver.setPrefix(/WEB-INF/templates/);templateResolver.setSuffix(.html);templateResolver.setCharacterEncoding(UTF-8);templateResolver.setTemplateMode(TemplateMode.HTML);return templateResolver;}/*** 生成模板引擎并为模板引擎注入模板解析器* param templateResolver* return*/Beanpublic SpringTemplateEngine templateEngine(ITemplateResolver templateResolver){SpringTemplateEngine templateEngine new SpringTemplateEngine();templateEngine.setTemplateResolver(templateResolver);return templateEngine;}/*** 生成Thymeleaf视图解析器并为解析器注入模板引擎* param templateEngine* return*/Beanpublic ViewResolver viewResolver(SpringTemplateEngine templateEngine){ThymeleafViewResolver viewResolvernew ThymeleafViewResolver();viewResolver.setCharacterEncoding(UTF-8);viewResolver.setTemplateEngine(templateEngine);return viewResolver;} } 4. 测试功能 RequestMapping(/) public String index(){return index; }/*** 测试ajax交互* param username* param password* return*/ RequestMapping(/testAjax) ResponseBody public String testAjax(String username, String password){System.out.println(1/0); // 异常Java代码System.out.println(username:username,password:password);return hello,ajax; }十三、SpringMVC执行流程 1. SpringMVC常用组件 DispatcherServlet前端控制器不需要工程师开发由框架提供 ​ 作用统一处理请求和响应整个流程控制的中心由它调用其它组件处理用户的请求 HandlerMapping处理器映射器不需要工程师开发由框架提供 ​ 作用根据请求的url、method等信息查找Handler即控制器方法 Handler处理器需要工程师开发例如xxxController ​ 作用在DispatcherServlet的控制下Handler对具体的用户请求进行处理 HandlerAdapter处理器适配器不需要工程师开发由框架提供 ​ 作用通过HandlerAdapter对处理器控制器方法进行执行 ViewResolver视图解析器不需要工程师开发由框架提供 ​ 作用进行视图解析得到相应的视图例如ThymeleafView、InternalResourceView、RedirectView分别对应thymeleaf、forward和redirect View视图不需要工程师开发由框架提供但页面需要工程师开发 ​ 作用将模型数据通过页面展示给用户 SpringMVC中HandlerMapping和HandlerAdapter详解适配器模式 2. DispatcherServlet初始化过程 IDEA按快捷键 ctrlaltU 查看DispatcherServlet的继承关系图。 各类简述 Servlet定义init方法未实现 GenericServlet实现Servlet实现init方法但init方法体为空 HttpServlet继承GenericServlet HttpServletBean继承HttpServlet重写init方法方法内最后调用了initServletBean方法initServletBean方法体为空 FrameworkServlet继承HttpServletBean重写initServletBean方法方法内调用了initWebApplicationContext方法用于创建并初始化WebApplicationContext上下文。initWebApplicationContext方法内又调用了onRefresh方法方法体为空 DispatcherServlet继承FreameworkServlet重写onRefresh方法方法内仅调用了initStrategies方法initStrategies方法用于初始化DispatcherServlet的文件上传解析器、处理器映射器、处理器适配器、处理器异常解析器、视图解析器等。 DispatcherServlet初始化过程 DispatcherServlet 本质上是一个 Servlet所以天然的遵循 Servlet 的生命周期。所以宏观上是 Servlet 生命周期来进行调度。 2.1 初始化WebApplicationContext 所在类org.springframework.web.servlet.FrameworkServlet protected WebApplicationContext initWebApplicationContext() {WebApplicationContext rootContext WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext wac null;if (this.webApplicationContext ! null) {// A context instance was injected at construction time - use itwac this.webApplicationContext;if (wac instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac (ConfigurableWebApplicationContext) wac;if (!cwac.isActive()) {// The context has not yet been refreshed - provide services such as// setting the parent context, setting the application context id, etcif (cwac.getParent() null) {// The context instance was injected without an explicit parent - set// the root application context (if any; may be null) as the parentcwac.setParent(rootContext);}configureAndRefreshWebApplicationContext(cwac);}}}if (wac null) {// No context instance was injected at construction time - see if one// has been registered in the servlet context. If one exists, it is assumed// that the parent context (if any) has already been set and that the// user has performed any initialization such as setting the context idwac findWebApplicationContext();}if (wac null) {// No context instance is defined for this servlet - create a local one// 创建WebApplicationContextwac createWebApplicationContext(rootContext);}if (!this.refreshEventReceived) {// Either the context is not a ConfigurableApplicationContext with refresh// support or the context injected at construction time had already been// refreshed - trigger initial onRefresh manually here.synchronized (this.onRefreshMonitor) {// 刷新WebApplicationContextonRefresh(wac);}}if (this.publishContext) {// Publish the context as a servlet context attribute.// 将IOC容器在应用域共享String attrName getServletContextAttributeName();getServletContext().setAttribute(attrName, wac);}return wac; }2.2 创建WebApplicationContext 所在类org.springframework.web.servlet.FrameworkServlet protected WebApplicationContext createWebApplicationContext(Nullable ApplicationContext parent) {Class? contextClass getContextClass();if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {throw new ApplicationContextException(Fatal initialization error in servlet with name getServletName() : custom WebApplicationContext class [ contextClass.getName() ] is not of type ConfigurableWebApplicationContext);}// 通过反射创建 IOC 容器对象ConfigurableWebApplicationContext wac (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);wac.setEnvironment(getEnvironment());// 设置父容器wac.setParent(parent);String configLocation getContextConfigLocation();if (configLocation ! null) {wac.setConfigLocation(configLocation);}configureAndRefreshWebApplicationContext(wac);return wac; }2.3 DispatcherServlet初始化策略 FrameworkServlet创建WebApplicationContext后刷新容器调用onRefresh(wac)此方法在DispatcherServlet中进行了重写调用了initStrategies(context)方法初始化策略即初始化DispatcherServlet的各个组件 所在类org.springframework.web.servlet.DispatcherServlet protected void initStrategies(ApplicationContext context) {initMultipartResolver(context);initLocaleResolver(context);initThemeResolver(context);initHandlerMappings(context);initHandlerAdapters(context);initHandlerExceptionResolvers(context);initRequestToViewNameTranslator(context);initViewResolvers(context);initFlashMapManager(context); }3. DispatcherServlet调用组件处理请求 DispatcherServlet继承FrameworkServletFrameworkServlet重写HttpServlet中的service()和doXxx()。当需要处理请求时FrameworkServlet的service方法会根据请求的方法类型调用相应的doXXX方法。这些doXXX方法最后又都调用了 调用了DispatcherServlet的processRequest方法。 3.1 processRequest() FrameworkServlet重写HttpServlet中的service()和doXxx()这些方法中调用了processRequest(request, response) 所在类org.springframework.web.servlet.FrameworkServlet protected final void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {long startTime System.currentTimeMillis();Throwable failureCause null;LocaleContext previousLocaleContext LocaleContextHolder.getLocaleContext();LocaleContext localeContext buildLocaleContext(request);RequestAttributes previousAttributes RequestContextHolder.getRequestAttributes();ServletRequestAttributes requestAttributes buildRequestAttributes(request, response, previousAttributes);WebAsyncManager asyncManager WebAsyncUtils.getAsyncManager(request);asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());initContextHolders(request, localeContext, requestAttributes);try {// 执行服务doService()是一个抽象方法在DispatcherServlet中进行了重写doService(request, response);}catch (ServletException | IOException ex) {failureCause ex;throw ex;}catch (Throwable ex) {failureCause ex;throw new NestedServletException(Request processing failed, ex);}finally {resetContextHolders(request, previousLocaleContext, previousAttributes);if (requestAttributes ! null) {requestAttributes.requestCompleted();}logResult(request, response, failureCause, asyncManager);publishRequestHandledEvent(request, response, startTime, failureCause);} }3.2 doService() 所在类org.springframework.web.servlet.DispatcherServlet Override protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {logRequest(request);// Keep a snapshot of the request attributes in case of an include,// to be able to restore the original attributes after the include.MapString, Object attributesSnapshot null;if (WebUtils.isIncludeRequest(request)) {attributesSnapshot new HashMap();Enumeration? attrNames request.getAttributeNames();while (attrNames.hasMoreElements()) {String attrName (String) attrNames.nextElement();if (this.cleanupAfterInclude || attrName.startsWith(DEFAULT_STRATEGIES_PREFIX)) {attributesSnapshot.put(attrName, request.getAttribute(attrName));}}}// Make framework objects available to handlers and view objects.request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver);request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver);request.setAttribute(THEME_SOURCE_ATTRIBUTE, getThemeSource());if (this.flashMapManager ! null) {FlashMap inputFlashMap this.flashMapManager.retrieveAndUpdate(request, response);if (inputFlashMap ! null) {request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap));}request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager);}RequestPath requestPath null;if (this.parseRequestPath !ServletRequestPathUtils.hasParsedRequestPath(request)) {requestPath ServletRequestPathUtils.parseAndCache(request);}try {// 处理请求和响应doDispatch(request, response);}finally {if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {// Restore the original attribute snapshot, in case of an include.if (attributesSnapshot ! null) {restoreAttributesAfterInclude(request, attributesSnapshot);}}if (requestPath ! null) {ServletRequestPathUtils.clearParsedRequestPath(request);}} }3.3 doDispatch() 所在类org.springframework.web.servlet.DispatcherServlet protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest request;HandlerExecutionChain mappedHandler null;boolean multipartRequestParsed false;WebAsyncManager asyncManager WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv null;Exception dispatchException null;try {processedRequest checkMultipart(request);multipartRequestParsed (processedRequest ! request);// Determine handler for the current request./*mappedHandler调用链包含handler、interceptorList、interceptorIndexhandler浏览器发送的请求所匹配的控制器方法interceptorList处理控制器方法的所有拦截器集合interceptorIndex拦截器索引控制拦截器afterCompletion()的执行用注解RequestMapping定义的Handler用的是RequestMappingHandlerMapping上面的其他两种用的是BeanNameUrlHandlerMapping静态资源的请求用的是SimpleUrlHandlerMapping。*/mappedHandler getHandler(processedRequest);if (mappedHandler null) {noHandlerFound(processedRequest, response);return;}// Determine handler adapter for the current request.// 通过控制器方法创建相应的处理器适配器调用所对应的控制器方法HandlerAdapter ha getHandlerAdapter(mappedHandler.getHandler());// Process last-modified header, if supported by the handler.String method request.getMethod();boolean isGet GET.equals(method);if (isGet || HEAD.equals(method)) {long lastModified ha.getLastModified(request, mappedHandler.getHandler());if (new ServletWebRequest(request, response).checkNotModified(lastModified) isGet) {return;}}// 调用拦截器的preHandle()if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// Actually invoke the handler.// 由处理器适配器调用具体的控制器方法最终获得ModelAndView对象mv ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);// 调用拦截器的postHandle()mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException ex;}catch (Throwable err) {// As of 4.3, were processing Errors thrown from handler methods as well,// making them available for ExceptionHandler methods and other scenarios.dispatchException new NestedServletException(Handler dispatch failed, err);}// 后续处理处理模型数据和渲染视图processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException(Handler processing failed, err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {// Instead of postHandle and afterCompletionif (mappedHandler ! null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}} }3.4 processDispatchResult() private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,Nullable HandlerExecutionChain mappedHandler, Nullable ModelAndView mv,Nullable Exception exception) throws Exception {boolean errorView false;if (exception ! null) {if (exception instanceof ModelAndViewDefiningException) {logger.debug(ModelAndViewDefiningException encountered, exception);mv ((ModelAndViewDefiningException) exception).getModelAndView();}else {Object handler (mappedHandler ! null ? mappedHandler.getHandler() : null);mv processHandlerException(request, response, handler, exception);errorView (mv ! null);}}// Did the handler return a view to render?if (mv ! null !mv.wasCleared()) {// 处理模型数据和渲染视图render(mv, request, response);if (errorView) {WebUtils.clearErrorRequestAttributes(request);}}else {if (logger.isTraceEnabled()) {logger.trace(No view rendering, null ModelAndView returned.);}}if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {// Concurrent handling started during a forwardreturn;}if (mappedHandler ! null) {// Exception (if any) is already handled..// 调用拦截器的afterCompletion()mappedHandler.triggerAfterCompletion(request, response, null);} }4. SpringMVC的执行流程 SringMVC执行流程 springmvc执行流程如下 客户端请求提交到DispatcherServlet 2DispatcherServlet接收到请求后、将提交的信息交给处理器映射器(HandlerMapping)HandlerMapping根据用户的url请求、匹配该url的Handler(Controller)并返回一个执行链DispatcherServlet调用HandlerAdapter(处理器适配器)HandlerAdapter经过适配调用具体的处理器(Controller)扫描Controller扫描完成后检查是否有异常没有的话返回一个ModelAndViewHandlerAdapter将Controller扫描结果(ModelAndView)返回给DispatcherServletDispatcherServlet将ModelAndView请求试图解析器(ViewReslover)进行解析ViewReslover解析后返回具体的ViewDispatcherServlet将view进行渲染试图(即将模型数据填充到视图中)DispatcherServlet将页面响应给个用户 补充说明 用户向服务器发送请求请求被SpringMVC 前端控制器 DispatcherServlet捕获。DispatcherServlet对请求URL进行解析得到请求资源标识符URI判断请求URI对应的映射 ​ a) 不存在 ​ i. 再判断是否配置了mvc:default-servlet-handler ​ ii. 如果没配置则控制台报映射查找不到客户端展示404错误 ​ iii. 如果有配置则访问目标资源一般为静态资源如JS,CSS,HTML找不到客户端也会展示404错误 ​ b) 存在则继续执行下面的流程 根据该URI调用HandlerMapping获得该Handler配置的所有相关的对象包括Handler对象以及Handler对象对应的拦截器最后以HandlerExecutionChain执行链对象的形式返回。 DispatcherServlet 根据获得的Handler选择一个合适的HandlerAdapter。 如果成功获得HandlerAdapter此时将开始执行拦截器的preHandler(…)方法【正向】 提取Request中的模型数据填充Handler入参开始执行Handler方法处理请求。在填充Handler的入参过程中根据你的配置Spring将帮你做一些额外的工作 ​ a) HttpMessageConveter 将请求消息如Json、xml等数据转换成一个对象将对象转换为指定的响应信息 ​ b) 数据转换对请求消息进行数据转换。如String转换成Integer、Double等 ​ c) 数据格式化对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等 ​ d) 数据验证 验证数据的有效性长度、格式等验证结果存储到BindingResult或Error中 Handler执行完成后向DispatcherServlet 返回一个ModelAndView对象。 此时将开始执行拦截器的postHandle(…)方法【逆向】。 根据返回的ModelAndView此时会判断是否存在异常如果存在异常则执行HandlerExceptionResolver进行异常处理选择一个适合的ViewResolver进行视图解析根据Model和View来渲染视图。 渲染视图完毕执行拦截器的afterCompletion(…)方法【逆向】。 将渲染结果返回给客户端。
http://www.hkea.cn/news/14327458/

相关文章:

  • 做网站这么做优享揭阳网站建设
  • jsp商务网站建设找网络公司做网站要注意这4个细节
  • 无锡网站开发定制开发wordpress lamp
  • 著名建筑网站手机网站设计公司可去亿企邦
  • 怎么建立博客网站wordpress装修
  • 个人如何建设网站猎聘网招聘网页版
  • 东门网站建设网站佣金怎么做会计分录
  • 域名解析错误无法上网搜外seo视频 网络营销免费视频课程
  • 文字网站居中基于 wordpress
  • 网站设计上市公司太平洋建设集团招标网站
  • 将网站发布到微信小程序怎么做百度小程序可以根据网站的要求做吗
  • asp.net网站和空网站有什么区别关键词优化价格
  • 网站建设费用要摊销嘛wordpress ckplayer
  • 免费网站制作软件有哪些什么是wordpress程序
  • 网站建设与管理大作业总结商用图片做公司网站可以吗
  • 怎么做pdf电子书下载网站好用心 做网站送女友
  • 北京市住房及城乡建设部网站wordpress 下拉式菜单
  • 长沙建站模板大全如何防止网站被攻击
  • 武威网站怎么做seo模板网站缺点
  • 计算机应用技术网站开发方向松江品划网站建设
  • 企业网站建站元素企业网站托管后果
  • 网站建设可以自己弄吗知乎单页手机网站源码
  • 报电子商务( 网站建设与运营)wordpress编辑器哪个好用
  • 房产网站建设方案的论文杭州市住房和城乡建设部网站
  • 做一个手机网站多少钱专业网站建设价格
  • 企业网站产品优化怎么做如何做自己的游戏网站
  • 网站建设类外文翻译911制品厂麻花
  • 网站建设 目标免费手机端网站模板下载安装
  • 如何开发网站建设业务西安关键词排名优化
  • 服务器怎么建设网站青岛网站建站团队