1.SpringMVC的引言 为了使Spring可插入的MVC架构,SpringFrameWork在Spring基础上开发SpringMVC框架,从而在使用Spring进行WEB开发时可以选择使用Spring的SpringMVC框架作为web开发的控制器框架。==注:底层使用的还是servlet==
2.为什么是SpringMVC?
可以和spring框架无缝整合
运行效率高于struts2框架
注解式开发更高效
3.SpringMVC的特点 SpringMVC 轻量级,典型MVC框架 ,在整个MVC架构中充当控制器框架,相对于之前学习的struts2框架,SpringMVC运行更快,其注解式开发更高效灵活 。
4.SpringMVC与Struts2运行流程对比
5.第一个环境搭建 5.1思路
5.2代码实现
新建Maven-webapp项目,添加java、resources目录以及test路径下java、resources目录
引入相关依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <dependency > <groupId > org.springframework</groupId > <artifactId > spring-core</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context-support</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aop</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-expression</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-tx</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > <version > 2.5</version > <scope > provided</scope > </dependency > <dependency > <groupId > jstl</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency >
编写springmvc.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?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" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package ="com.study.controller" /> <mvc:annotation-driven /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/" /> <property name ="suffix" value =".jsp" /> </bean > </beans >
注意: 这里还要加载springmvc配置文件位置,通过在servlet写init-param标签,还是contextConfigLocation属性,value用来加载springmvc配置文件。
创建控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.study.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping(value = "/find") public String find () { System.out.println("调用了find方法" ); return "index" ; } @RequestMapping(value = "/save") public String save () { System.out.println("调用了save方法" ); return "index" ; } }
@Controller: 该注解用来在类上标识这是一个控制器组件类,并创建这个类实例
@RequestMapping:
修饰范围 : 用在方法或者类上
注解作用: 用来指定类以及类中方法的请求路径
注解详解 :
用在类上相当于struts2中namespace在访问类中方法必须先加入这个路径
用在方法上相当于action标签的name属性用来表示访问这个方法的路径
部署项目在tomcat服务器上进行测试
启动项目进行测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 D:\Software_Development\IDEA_code\apache-tomcat-8.5.78\bin\catalina.bat run [2022-04-20 04:42:56,645] Artifact springmvc01:war exploded: Waiting for server connection to start artifact deployment... Using CATALINA_BASE: "C:\Users\cjn\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_springmvc01" Using CATALINA_HOME: "D:\Software_Development\IDEA_code\apache-tomcat-8.5.78" Using CATALINA_TMPDIR: "D:\Software_Development\IDEA_code\apache-tomcat-8.5.78\temp" Using JRE_HOME: "D:\Software_Development\JDK" Using CLASSPATH: "D:\Software_Development\IDEA_code\apache-tomcat-8.5.78\bin\bootstrap.jar;D:\Software_Development\IDEA_code\apache-tomcat-8.5.78\bin\tomcat-juli.jar" Using CATALINA_OPTS: "" ...... Connected to server [2022-04-20 04:42:58,218] Artifact springmvc01:war exploded: Artifact is being deployed, please wait... ...... [2022-04-20 04:42:59,430] Artifact springmvc01:war exploded: Artifact is deployed successfully [2022-04-20 04:42:59,430] Artifact springmvc01:war exploded: Deploy took 1,212 milliseconds ... 调用了find方法 调用了save方法
访问路径 :
http://localhost:8888/springmvc01/hello/find
或
http://localhost:8888/springmvc01/hello/save
与此同时,控制台输出“调用了xxx方法”
6.SpringMVC中跳转方式 6.0 概述
或
6.1 跳转方式
说明 : 跳转方式有两种,一种是forward ,一种是redirect 。
forward跳转,一次请求,地址栏不变
redirect跳转,多次请求,地址栏改变
6.2 编写代码启动tomcat服务器进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 package com.study.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("forwardAndRedirect") public class ForwardAndRedirectController { @RequestMapping("test") public String test () { System.out.println("测试forward跳转到页面" ); return "index" ; } @RequestMapping("test1") public String test1 () { System.out.println("测试redirect跳转到页面" ); return "redirect:/index.jsp" ; } @RequestMapping("test2") public String test2 () { System.out.println("测试forward跳转到相同controller类中的不同方法" ); return "forward:/forwardAndRedirect/test" ; } @RequestMapping("test3") public String test3 () { System.out.println("测试redirect跳转到相同controller类中的不同方法" ); return "redirect:/forwardAndRedirect/test" ; } @RequestMapping("test4") public String test4 () { System.out.println("测试forward跳转到不同controller类中的不同方法" ); return "forward:/hello/find" ; } @RequestMapping("test5") public String test5 () { System.out.println("测试redirect跳转到不同controller类中的不同方法" ); return "redirect:/hello/find" ; } }
7. SpringMVC中参数接收
接收参数语法说明:springmvc中使用控制器方法参数来收集客户端的请求参数,因此在接收请求参数时直接在需要的控制器方法中声明即可,springmvc可以自动根据指定类型完成类型的转换操作。
7.1 接收零散类型参数
如: 八种基本类型 + String + 日期类型
a.前台传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 # GET 方式传递参数 http://localhost:8080/springmvc_day1/param/test?name=zhangsan&age=19&sex=true&salary=11.11&bir=2012/12/12 # POST 方式传递参数 <h1>测试参数接收</h1> <form action="${pageContext.request.contextPath}/param/test" method="post"> 用户名: <input type="text" name="name"/> <br> 年龄: <input type="text" name="age"/> <br> 性别: <input type="text" name="sex"> <br> 工资: <input type="text" name="salary"> <br> 生日: <input type="text" name="bir"> <br> <input type="submit" value="提交"/> </form>
b.后台控制器接收
1 2 3 4 5 6 7 8 9 10 11 12 13 @Controller @RequestMapping("/param") public class ParamController { @RequestMapping("/test") public String test(String name, Integer age, Boolean sex,Double salary,Date bir){ System.out.println("姓名: "+name); System.out.println("年龄: "+age); System.out.println("性别: "+sex); System.out.println("工资: "+salary); System.out.println("生日: "+bir); return "index"; } }
注意:springmvc在接收日期类型参数时日期格式必须为yyyy/MM/dd HH:mm:ss
7.2 接收对象类型参数 a.前台传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 # GET 方式请求参数传递 http://localhost:8080/springmvc_day1/param/test1?name=zhangsan&age=19&sex=true&salary=11.11&bir=2012/12/12 # POST 方式请求参数传递 <h1>测试对象类型参数接收</h1> <form action="${pageContext.request.contextPath}/param/test1" method="post"> 用户名: <input type="text" name="name"/> <br> 年龄: <input type="text" name="age"/> <br> 性别: <input type="text" name="sex"> <br> 工资: <input type="text" name="salary"> <br> 生日: <input type="text" name="bir"> <br> <input type="submit" value="提交"/> </form>
注意:在接收对象类型参数时和struts2接收不同,springmvc直接根据传递参数名与对象中属性名一致自动封装对象
b.后台控制器接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // 1.定义对象 public class User { private String name; private Integer age; private Double salary; private Boolean sex; private Date bir; } // 2.控制器中接收 @RequestMapping("/test1") public String test1(User user){ System.out.println("接收的对象: "+user); return "index"; } // 2.2的特殊情况 @RequestMapping("/test1") //都赋值 public String test1(User user ,String name){ System.out.println("接收的对象: "+user); return "index"; }
7.3 接收数组类型参数 a.前台传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 # GET 方式请求参数传递 http://localhost:8080/springmvc_day1/param/test2?names=zhangsan&names=lisi&names=wangwu # POST 方式请求参数传递 <h1>测试对象类型参数接收</h1> <form action="${pageContext.request.contextPath}/param/test2" method="post"> 爱好: <br> 看书: <input type="checkbox" name="names"/> 看电视:<input type="checkbox" name="names"/> 吃饭: <input type="checkbox" name="names"/> 玩游戏: <input type="checkbox" name="names"/> <input type="submit" value="提交"/> </form>
b.后台控制器接收
1 2 3 4 5 6 7 @RequestMapping("/test2") public String test2(String[] names){ for (String name : names) { System.out.println(name); } return "index"; }
注意:接收数组类型数据时前台传递多个key一致自动放入同一个数组中
7.4 接收集合类型参数
说明:springmvc不支持直接将接收集合声明为控制器方法参数进行接收,如果要接收集合类型参数必须使用对象封装要接收接收类型才可以,推荐放入vo对象中接收集合类型,即新创建vo包,包中自定义集合
7.4.1 list集合
a.前台传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 # GET 方式请求参数传递 http://localhost:8080/springmvc_day1/param/test3?lists=zhangsan&lists=lisi&lists=wangwu # POST 方式请求参数传递 <h1>测试对象类型参数接收</h1> <form action="${pageContext.request.contextPath}/param/test3" method="post"> 爱好: <br> 看书: <input type="checkbox" name="lists"/> 看电视:<input type="checkbox" name="lists"/> 吃饭: <input type="checkbox" name="lists"/> 玩游戏: <input type="checkbox" name="lists"/> <input type="submit" value="提交"/> </form>
b.后台控制器接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // 1.封装接收集合类型对象---->在spring mvc中用来接收集合类型参数 public class CollectionVO { private List<String> lists; public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } } // 2.控制器中接收集合类型参数 @RequestMapping("/test3") public String test3(CollectionVO collectionVO){ collectionVO.getLists().forEach(name-> System.out.println(name)); return "index"; }
7.4.2map集合
a.前台传递参数
1 2 访问路径:http: 注意:在url中对参数进行赋值的时候要给CollectionVO中的集合赋值而不是collectionVO赋值
b.后台控制器接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class CollectionVO { private Map<String,String> maps; public Map<String, String> getMaps () { return maps; } public void setMaps (Map<String, String> maps) { this .maps = maps; } } @RequestMapping("test4") public String test4 (CollectionVO collectionVO) { System.out.println("用来测试map集合类型参数接收" ); collectionVO.getMaps().forEach((k,v)-> System.out.println("k=" + k+",v=" +v)); return "index" ; }
7.5 代码实测
User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 package com.study.entity; import java.util.Date; public class User { private String name; private Integer age; private Boolean sex; private Double salary; private Date bir; public User () { } public User (String name, Integer age, Boolean sex, Double salary, Date bir) { this .name = name; this .age = age; this .sex = sex; this .salary = salary; this .bir = bir; } public String getName () { return name; } public void setName (String name) { this .name = name; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } public Boolean getSex () { return sex; } public void setSex (Boolean sex) { this .sex = sex; } public Double getSalary () { return salary; } public void setSalary (Double salary) { this .salary = salary; } public Date getBir () { return bir; } public void setBir (Date bir) { this .bir = bir; } @Override public String toString () { return "User{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + ", salary=" + salary + ", bir=" + bir + '}' ; } }
CollectionVO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.study.vo; import com.study.entity.User; import java.util.List; import java.util.Map; public class CollectionVO { private List<String> lists; private Map<String,String> maps; public List<String> getLists () { return lists; } public void setLists (List<String> lists) { this .lists = lists; } public Map<String, String> getMaps () { return maps; } public void setMaps (Map<String, String> maps) { this .maps = maps; } }
ParamController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 package com.study.controller; import com.study.entity.User; import com.study.vo.CollectionVO; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; @Controller @RequestMapping("param") public class ParamController { @RequestMapping("test") public String test (String name, Integer age, Boolean sex, Double salary, Date bir) { System.out.println("用来测试零散类型参数接收" ); System.out.println("name = " + name); System.out.println("age = " + age); System.out.println("sex = " + sex); System.out.println("salary = " + salary); System.out.println("bir = " + bir); return "index" ; } @RequestMapping("test1") public String test1 (User user,String name) { System.out.println("用来测试对象类型的参数接收" ); System.out.println("user = " + user); System.out.println("name = " + name); return "index" ; } @RequestMapping("test2") public String test2 (String[] arrays) { System.out.println("用来测试数组类型的参数接收" ); for (String array : arrays) { System.out.println("array = " + array); } return "index" ; } @RequestMapping("test3") public String test3 (CollectionVO collectionVO) { System.out.println("用来测试list集合类型参数接收" ); collectionVO.getLists().forEach(list -> System.out.println("list = " + list)); return "index" ; } @RequestMapping("test4") public String test4 (CollectionVO collectionVO) { System.out.println("用来测试map集合类型参数接收" ); collectionVO.getMaps().forEach((k,v)-> System.out.println("k=" + k+",v=" +v)); return "index" ; } }
如果map传递参数时出现400错误,如下所示:
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 如果测试时出现HTTP状态 400-错误的请求,是因为日志显示请求地址中包含了不合法字符. tomcat高版本严格按照RFC 3986规范解析地址。该规范只允许包含a-z A-Z 0-9 - _ . ~ 以及所有保留字符 ! * ’ ( ) ; : @ & = + $ , / ? # [ ] 解决方案:在使用的tomcat文件夹中找到conf,打开后对server.xml进行编辑,在 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/> 后面加上relaxedPathChars="|{}[],%" relaxedQueryChars="|{}[],%",其余不用修改,即: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" relaxedPathChars="|{}[],%" relaxedQueryChars="|{}[],%"/> 保存后,重启tomcat即可解决。 参考链接:https://blog.51cto.com/u_15196075/2765608
7.6 表单传递接收参数中文乱码解决方案 7.6.1概述
7.6.2问题
param.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>测试参数接收</title> </head> <body> <h1>测试对象和零散类型参数接收</h1> <form action="${pageContext.request.contextPath}/param/test1" method="post" > 用户姓名:<input type="text" name="name" ><br> 用户年龄:<input type="text" name="age" ><br> 用户性别:<input type="text" name="sex" ><br> 用户收入:<input type="text" name="salary" ><br> 用户生日:<input type="text" name="bir" ><br> <input type="submit" value="提交" > </form> </body> </html>
测试路径:http://localhost:8888/springmvc01/param.jsp
测试结果:
1 2 3 4 5 6 7 8 9 1.英文状态 用来测试对象类型的参数接收 user = User{name='xiaosan', age=20, sex=false, salary=1000.0, bir=Mon Dec 12 00:00:00 CST 2022} name = xiaosan 2.中文状态 用来测试对象类型的参数接收 user = User{name='?°?è??', age=22, sex=true, salary=800.0, bir=Thu Mar 15 00:00:00 CST 2018} name = ?°?è??
注意:在使用springmvc接收客户端的请求参数的过程中有时会出现中文乱码问题,这是因为springmvc并没有对对象请求参数进行编码控制,如果需要控制需要自行指定 。
7.6.3解决具体实现
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # 1.针对于GET方式中文乱码解决方案:tomcat8以前会出现,server.xml配置文件加URIEncoding="UTF-8"即可解决,8之后不会出现 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/> # 2.针对POST方式中文乱码解决方案: web.xml中配置字符Filter <!--配置post请求方式中文乱码的Filter--> <filter> <filter-name>charset</filter-name> <!--自定义的Filter必须配合自定义的类使用--> <!-- <filter-class>com.study.filter.CharacterEncodingFilter</filter-class>--> <!--spring框架提供的Filter ,下边类就不用写了--> <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>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
自定义编码Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.study.filter;import javax.servlet.*;import java.io.IOException;public class CharacterEncodingFilter implements Filter { private String encoding; @Override public void init (FilterConfig filterConfig) throws ServletException { this .encoding = filterConfig.getInitParameter("encoding" ); System.out.println(encoding); } @Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding(encoding); servletResponse.setCharacterEncoding(encoding); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy () { } }
7.7 参数接收总结
8.SpringMVC中数据传递机制
其实数据传递机制学完就可整合了 这里学完收参,传递数据,跳转我们都会了,就剩调用业务,结合mybatis和spring即可完成。
==springmvc是一个控制器,对于一个控制器我们就关注三件事:收集数据,调业务对象,流程跳转,==调用业务对象和流程跳转这两步里其实还隐藏了一步===》数据传递过程
8.0概述
8.1 数据传递机制
1 2 3 4 5 6 # 1.数据怎么存 Servlet 作用域 Struts2 作用域 SpringMVC 作用域 # 2.数据怎么取 Servlet EL表达式 Struts2 EL表达式 SpringMVC EL表达式 # 3.数据怎么展示 Servlet JSTL标签 Struts2 JSTl标签 SpringMVC JSTL标签
8.2 使用forward跳转传递数据
1 2 3 4 5 6 # 1.使用servlet中原始的request作用域传递数据 request.setAttribute("key",value); # 2.使用是springmvc中封装的Model和ModelMap对象(底层对request作用域封装) model.addAttribute(key,value); modelMap.addAttribute(key,value);
8.3 使用Redirect跳转传递数据
1 2 3 4 5 6 # 1.使用地址栏进行数据传递 url?name=zhangsan&age=21 # 2.使用session作用域 session.setAttribute(key,value); session.getAttribute(key);
8.4如何在springmvc控制器方法获取request对象,response对象
直接做为控制器方法参数声明即可
8.5 代码测试
AttributeController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package com.study.controller;import com.study.entity.User;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.Arrays;import java.util.Date;import java.util.List;@Controller @RequestMapping("attribute") public class AttributeController { @RequestMapping("test") public String test (Model model,HttpServletRequest request, HttpServletResponse response) { String name = "茶白" ; model.addAttribute("name" ,name); User user = new User ("光头强" ,50 ,true ,12.12 ,new Date ()); model.addAttribute("user" ,user); User user1 = new User ("熊大" ,30 ,true ,6.06 ,new Date ()); User user2 = new User ("熊二" ,20 ,true ,3.03 ,new Date ()); List<User> users = Arrays.asList(user1, user2); model.addAttribute("users" ,users); return "attribute" ; } @RequestMapping("test1") public String test1 (HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException { String name = "小猫" ; User user = new User ("小猪" , 3 , false , 0.1234 , new Date ()); User user1 = new User ("小狗" , 4 , false , 1.2345 , new Date ()); User user2 = new User ("小兔" , 5 , false , 2.3456 , new Date ()); List<User> users = Arrays.asList(user1, user2); request.getSession().setAttribute("user" ,user); request.getSession().setAttribute("users" ,users); return "redirect:/attribute.jsp?name=" + URLEncoder.encode(name,"UTF-8" ); } }
attribute.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>测试数据传递</title> </head> <body> <h1>用来测试request作用域传递数据</h1> 获取request作用域数据:${requestScope.name}<br> 获取request作用域数据:${name}<br> <hr color="red" > name:${requestScope.user.name}<br> age:${requestScope.user.age}<br> sex:${requestScope.user.sex}<br> salary:${requestScope.user.salary}<br> <!--bir: ${user.bir}<br> 要格式化的话就要借助c标签 ,别忘了引依赖jstl--> bir:<fmt:formatDate value="${requestScope.user.bir}" pattern="yyyy-MM-dd" /></h3> <hr> <c:forEach items="${requestScope.users}" var ="user" > name:${user.name}<br> age: ${user.age}<br> sex: ${user.sex}<br> salary: ${user.salary}<br> bir: <fmt:formatDate value="${user.bir}" /><br> <hr> </c:forEach> <br> <h1>测试使用redirect跳转传递数据</h1> 获取地址栏数据:${param.name}<br> <hr color="red" > name: ${sessionScope.user.name}<br> age: ${sessionScope.user.age}<br> sex: ${sessionScope.user.sex}<br> salary: ${sessionScope.user.salary}<br> bir: ${sessionScope.user.bir}<br> <hr> <c:forEach items="${sessionScope.users}" var ="user" > name:${user.name}<br> age: ${user.age}<br> sex: ${user.sex}<br> salary: ${user.salary}<br> bir: <fmt:formatDate value="${user.bir}" /><br> <hr> </c:forEach> </body> </html>
测试结果:
(1)http://localhost:8888/springmvc01/attribute/test
(2)http://localhost:8888/springmvc01/attribute/test1
9.SpringMVC处理静态资源拦截
10.Spring+SpringMVC+MyBatis整合编程
整合思路
代码实现:
1.新建maven-webapp工程,添加java、resources目录及test目录下的java、resources
2.引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 <dependency > <groupId > org.springframework</groupId > <artifactId > spring-core</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context-support</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aop</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-expression</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-tx</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > <version > 2.5</version > <scope > provided</scope > </dependency > <dependency > <groupId > jstl</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.2.8</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 1.3.3</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.38</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > 1.1.17</version > </dependency > <dependency > <groupId > org.apache.logging.log4j</groupId > <artifactId > log4j-core</artifactId > <version > 2.10.0</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-log4j12</artifactId > <version > 1.7.25</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.62</version > </dependency
3.Spring+Mybatis整合
3.1 新建表
3.2 新建实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 package com.study.entity;import java.util.Date;public class User { private String id; private String name; private Integer age; private Date bir; @Override public String toString () { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", bir=" + bir + '}' ; } public User () { } public User (String id, String name, Integer age, Date bir) { this .id = id; this .name = name; this .age = age; this .bir = bir; } public String getId () { return id; } public void setId (String id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } public Date getBir () { return bir; } public void setBir (Date bir) { this .bir = bir; } }
3.3 新建DAO接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.study.dao; import com.study.entity.User; import java.util.List; public interface UserDAO { List<User> selectAllUsers () ; void insertUser (User user) ; }
3.4 新建Mapper配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.study.dao.UserDAO" > <select id ="selectAllUsers" resultType ="User" > select id,name,age,bir from t_user </select > <insert id ="insertUser" parameterType ="User" > insert into t_user values(#{id},#{name},#{age},#{bir}) </insert > </mapper >
3.5 新建Service接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package com.study.service; import com.study.entity.User; import java.util.List; public interface UserService { List<User> selectAllUsers () ; void insertUser (User user) ; }
3.6 新建ServiceImpl实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.study.service; import com.study.dao.UserDAO; import com.study.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.UUID; @Service("userService") @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; @Override @Transactional(propagation = Propagation.SUPPORTS) public List<User> selectAllUsers () { return userDAO.selectAllUsers(); } @Override public void insertUser (User user) { user.setId(UUID.randomUUID().toString()); userDAO.insertUser(user); } }
3.7 引入spring.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <?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:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <context:component-scan base-package ="com.study" /> <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" > <property name ="driverClassName" value ="com.mysql.jdbc.Driver" /> <property name ="url" value ="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8& useSSL=false" /> <property name ="username" value ="root" /> <property name ="password" value ="123456" /> </bean > <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> <property name ="mapperLocations" value ="classpath:com/study/mapper/*.xml" /> <property name ="typeAliasesPackage" value ="com.study.entity" /> </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="sqlSessionFactoryBeanName" value ="sqlSessionFactory" /> <property name ="basePackage" value ="com.study.dao" /> </bean > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" /> </bean > <tx:annotation-driven transaction-manager ="transactionManager" /> </beans >
3.8 引入log4j.properties
1 2 3 4 5 6 7 log4j.rootLogger=ERROR,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.conversionPattern=[%p] %d{yyyy-MM-dd} %m%n log4j.logger.com.study.dao=DEBUG log4j.logger.org.springframework=ERROR
3.9 测试Service方法是否可以成功调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.study.test; import com.study.entity.User; import com.study.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class TestUserService { public static void main (String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext ("spring.xml" ); UserService userService = (UserService) context.getBean("userService" ); List<User> users = userService.selectAllUsers(); for (User user : users) { System.out.println("user = " + user); } } }
测试结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [DEBUG] 2022-04-23 ==> Preparing: select id,name,age,bir from t_user [DEBUG] 2022-04-23 ==> Parameters: [DEBUG] 2022-04-23 <== Total: 18 user = User{id='1', name='张三', age=10, bir=Fri Apr 01 00:00:00 CST 2022} user = User{id='2', name='李四', age=20, bir=Sat Apr 02 00:00:00 CST 2022} user = User{id='2d237778-835d-477b-9721-fe6f575064b9', name='波比', age=18, bir=Sun Feb 02 00:00:00 CST 2020} user = User{id='2f278828-6422-432d-ab1b-a727b33c23bc', name='小胖墩', age=3, bir=Mon Apr 11 21:43:57 CST 2022} user = User{id='3', name='王五', age=30, bir=Sun Apr 03 00:00:00 CST 2022} user = User{id='4', name='灰太狼', age=60, bir=Sun Apr 10 09:56:17 CST 2022} user = User{id='5', name='小超超', age=36, bir=Sun Apr 10 09:55:41 CST 2022} user = User{id='586a5f71-836c-4171-a038-4620fe0f9354', name='葫芦娃', age=5, bir=Tue Apr 12 09:45:17 CST 2022} user = User{id='6', name='小呆呆', age=60, bir=Sun Apr 10 09:55:43 CST 2022} user = User{id='7', name='小超超', age=36, bir=Sun Apr 10 09:55:44 CST 2022} user = User{id='7a1a00aa-1507-461a-95c0-50110d6f4333', name='唐三', age=20, bir=Fri Apr 15 00:00:00 CST 2022} user = User{id='8', name='胖嘟嘟', age=50, bir=Sun Apr 10 09:55:39 CST 2022} user = User{id='8a3e8ab8-5abc-44e1-9aec-31f3978d97f2', name='小胖墩', age=3, bir=Tue Apr 12 10:23:33 CST 2022} user = User{id='9', name='茶白', age=25, bir=Sun Apr 10 09:55:52 CST 2022} user = User{id='985cd035-dedc-4e84-973a-a5fdaa2b8475', name='喜洋洋', age=8, bir=Tue Oct 10 00:00:00 CST 2000} user = User{id='a6be9263-f05f-415f-855e-33843b22e71d', name='皮卡丘', age=10, bir=Thu Apr 14 00:00:00 CST 2022} user = User{id='cd280176-68b2-4ceb-b781-ba2f5e1fbeff', name='小胖墩', age=3, bir=Tue Apr 12 10:21:18 CST 2022} user = User{id='e3863fd3-173c-46f4-a753-c72f7c92ad56', name='小猫咪', age=3, bir=Mon Apr 11 21:36:31 CST 2022}
4.Spring+SpringMVC整合
4.1 配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <!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 > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring.xml</param-value > </context-param > <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.xml</param-value > </init-param > </servlet > <servlet-mapping > <servlet-name > springmvc</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > <filter > <filter-name > charset</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 > charset</filter-name > <url-pattern > /*</url-pattern > </filter-mapping > </web-app >
4.2 引入springmvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" > <context:component-scan base-package ="com.study.controller" /> <mvc:annotation-driven /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/" /> <property name ="suffix" value =".jsp" /> </bean > <mvc:default-servlet-handler /> </beans >
4.3 新建Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.study.controller; import com.study.entity.User; import com.study.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.util.List; @Controller @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping("selectAllUsers") public String selectAllUsers (HttpServletRequest request) { List<User> users = userService.selectAllUsers(); request.setAttribute("users" ,users); return "selectAllUsers" ; } @RequestMapping("insertUser") public String insertUser (User user) { try { userService.insertUser(user); return "redirect:/user/selectAllUsers" ; } catch (Exception e) { e.printStackTrace(); return "redirect:/insertUser.jsp" ; } } }
4.4 添加jsp文件
selectAllUsers.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>用来展示所有用户信息</title> <script src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js" ></script> <script> $(function(){ alert(); }) </script> </head> <body> <h1>展示用户列表</h1> <c:forEach items="${requestScope.users}" var ="user" > ${user.id} ==== ${user.name} ==== ${user.age} ==== ${user.bir} <br> </c:forEach> <a href="${pageContext.request.contextPath}/insertUser.jsp" >添加用户信息</a> </body> </html>
insertUser.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>添加用户信息</title> </head> <body> <h1>添加用户信息</h1> <form action="${pageContext.request.contextPath}/user/insertUser" method="post" > 姓名:<input type="text" name="name" /> <br> 年龄:<input type="text" name="age" /> <br> 生日:<input type="text" name="bir" /> <br> <input type="submit" value="保存用户信息" > </form> </body> </html>
5.部署tomcat服务器测试
测试结果:
6.项目目录结构:
11.文件上传
文件上传: 指将用户本地计算机中的文件上传到服务器上。
1.springmvc中文件上传流程:
2.代码实现
新建maven-webapp工程,添加java、resources目录
pom.xml引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 <dependency > <groupId > org.springframework</groupId > <artifactId > spring-core</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context-support</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aop</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-expression</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-tx</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 4.3.2.RELEASE</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > <version > 2.5</version > <scope > provided</scope > </dependency > <dependency > <groupId > jstl</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > commons-fileupload</groupId > <artifactId > commons-fileupload</artifactId > <version > 1.4</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.62</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.9.0</version > </dependency >
配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <!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 > <filter > <filter-name > charset</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 > charset</filter-name > <url-pattern > /*</url-pattern > </filter-mapping > <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.xml</param-value > </init-param > </servlet > <servlet-mapping > <servlet-name > springmvc</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > </web-app >
引入springmvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" > <context:component-scan base-package ="com.study.controller" /> <mvc:annotation-driven /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/" /> <property name ="suffix" value =".jsp" /> </bean > <mvc:default-servlet-handler /> <bean id ="multipartResolver" class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name ="maxUploadSize" value ="2097152" /> </bean > </beans >
开发上传页面upload.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>测试文件上传</title> </head> <body> <h1>文件上传</h1> <!--向/file/upload提交请求--> <form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data" > <input type="file" name="img" /> <input type="submit" value="上传文件" > </form> </body> </html>
部署到tomcat服务器上进行测试
访问测试页面
开发控制器FileController 处理上传后文件img的接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.study.controller;import org.apache.commons.io.FilenameUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.time.LocalDate;import java.util.UUID;@Controller @RequestMapping("file") public class FileController { @RequestMapping("upload") public String upload (MultipartFile img, HttpServletRequest request) throws IOException { System.out.println("文件原始名称为:" + img.getOriginalFilename()); System.out.println("文件大小:" + img.getSize()); System.out.println("文件类型:" + img.getContentType()); String realPath = request.getSession().getServletContext().getRealPath("/upload" ); System.out.println("上传路径为:" + realPath); img.transferTo(new File (realPath,img.getOriginalFilename())); return "index" ; } }
访问路径:http://localhost:8888/springmvc02/upload.jsp,选择文件后进行上传
上传成功后跳转到index.jsp页面:
控制台输出文件信息、target目录下出现upload本地上传的文件:
3.springmvc中文件上传细节处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package com.study.controller;import org.apache.commons.io.FilenameUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.time.LocalDate;import java.util.UUID;@Controller @RequestMapping("file") public class FileController { @RequestMapping("upload") public String upload (MultipartFile img, HttpServletRequest request) throws IOException { System.out.println("文件原始名称为:" + img.getOriginalFilename()); System.out.println("文件大小:" + img.getSize()); System.out.println("文件类型:" + img.getContentType()); String realPath = request.getSession().getServletContext().getRealPath("/upload" ); System.out.println("上传路径为:" + realPath); String originalFileName = img.getOriginalFilename(); String extension = FilenameUtils.getExtension(originalFileName); String newFileName = UUID.randomUUID().toString().replace("-" ,"" ) + "." + extension; System.out.println("文件新名称为: " + newFileName); LocalDate now = LocalDate.now(); File dateDir = new File (realPath, now.toString()); if (!dateDir.exists()) dateDir.mkdirs(); System.out.println("日期目录为:" + dateDir.getName()); img.transferTo(new File (dateDir,newFileName)); return "index" ; } }
12.文件下载
文件下载: 即将服务器上的文件下载到当前用户访问的计算机。
12.1思路
12.2代码实现
webapp目录下新建下载down目录并放入可下载文件aa.txt
开发下载页面upload.jsp
1 2 <h1>文件下载</h1> <a href="" >aa.txt</a> <!--href="" 里边的链接是向控制器发请求的链接-->
此时已经可以访问页面,即下载页面已经有了
写一个控制器用来处理文件下载
1 2 3 4 5 6 7 8 9 @Controller @RequestMapping("file") public class FileController { @RequestMapping("download") public void download (String fileName) { } }
完善upload.jsp 向控制器发请求
1 2 <h1>文件下载</h1> <a href="${pageContext.request.contextPath}/file/download?fileName=aa.txt" >aa.txt</a>
完善控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @Controller @RequestMapping("file") public class FileController { @RequestMapping("download") public void download (String fileName,HttpServletRequest request,HttpServletResponse response) throws IOException { System.out.println("下载文件的名称: " +fileName); String realPath = request.getSession().getServletContext().getRealPath("/down" ); System.out.println("绝对路径:" + realPath); FileInputStream is = new FileInputStream (new File (realPath, fileName)); response.setContentType("text/plain;charset=UTF-8" ); ServletOutputStream os = response.getOutputStream(); int len; byte [] b = new byte [1024 ]; while (true ){ len = is.read(b); if (len == -1 ) break ; os.write(b,0 ,len); } is.close(); os.close(); } }
改为以附件形式下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 @Controller @RequestMapping("file") public class FileController { @RequestMapping("download") public void download (String fileName,HttpServletRequest request,HttpServletResponse response) throws IOException { System.out.println("下载文件的名称: " +fileName); String realPath = request.getSession().getServletContext().getRealPath("/down" ); System.out.println("绝对路径:" + realPath); FileInputStream is = new FileInputStream (new File (realPath, fileName)); response.setContentType("text/plain;charset=UTF-8" ); ServletOutputStream os = response.getOutputStream(); response.setHeader("content-disposition" ,"attachment;fileName=" +fileName); int len; byte [] b = new byte [1024 ]; while (true ){ len = is.read(b); if (len == -1 ) break ; os.write(b,0 ,len); } is.close(); os.close(); } }
12.3文件下载细节处理
完善upload.jsp页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <h1 > 文件下载</h1 > <ul > <li > a.txt <a href ="${pageContext.request.contextPath}/file/download?fileName=a.txt" > 在线打开</a > <a href ="${pageContext.request.contextPath}/file/download?fileName=a.txt&openStyle=attach" > 附件下载</a > </li > <li > b.rar <a href ="${pageContext.request.contextPath}/file/download?fileName=b.rar" > 在线打开</a > <a href ="${pageContext.request.contextPath}/file/download?fileName=b.rar&openStyle=attach" > 附件下载</a > </li > <li > c.png <a href ="${pageContext.request.contextPath}/file/download?fileName=c.png" > 在线打开</a > <a href ="${pageContext.request.contextPath}/file/download?fileName=c.png&openStyle=attach" > 附件下载</a > </li > <li > 自我介绍.txt <a href ="${pageContext.request.contextPath}/file/download?fileName=自我介绍.txt" > 在线打开</a > <a href ="${pageContext.request.contextPath}/file/download?fileName=自我介绍.txt&openStyle=attach" > 附件下载</a > </li > </ul >
添加测试文件:
完善控制器FileController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @RequestMapping("download") public void download (String openStyle, String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException { openStyle = openStyle == null ? "inline" :"attachment" ; System.out.println("下载文件的名称为:" + fileName); String realPath = request.getSession().getServletContext().getRealPath("/download" ); System.out.println("绝对路径:" + realPath); FileInputStream fis = new FileInputStream (new File (realPath, fileName)); response.setContentType("text/plain;charset=UTF-8" ); response.setHeader("content-disposition" ,openStyle + ";fileName=" + URLEncoder.encode(fileName,"UTF-8" )); ServletOutputStream sos = response.getOutputStream(); IOUtils.copy(fis,sos); IOUtils.closeQuietly(fis); IOUtils.closeQuietly(sos); }
注意: 下载时必须设置响应的头信息,指定文件以何种方式保存,另外,下载文件的控制器不能存在返回值,代表响应只用来下载文件信息。 一个请求只能对应一个响应。
启动服务器进行测试
访问路径:http://localhost:8888/springmvc02/upload.jsp
若文件可以在线打开,点击“在线打开”后,文件在浏览器页面上进行显示;
文件不能在线打开,则点击“在线打开”后文件会以附件形式下载;
点击“附件下载”后文件均可以以附件形式进行下载
13.springmvc与Ajax的集成(@ResponseBody注解使用)
Ajax:异步处理、局部更新数据、在处理响应的时候只认json格式的字符串
fastjson:阿里巴巴提供的转json格式工具
springmvc提供的@ResponseBody注解:为了进一步方便控制器与ajax集成,springmvc提供了@responseBody注解用在方法的返回值上,代表可以将方法的返回值转换为json格式字符串并响应到前台,省去了通过第三方工具转换json的过程。
1.思路
2.代码实现
引入相关依赖
1 2 3 4 5 6 <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.9.0</version > </dependency >
开发实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 package com.study.entity; import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; public class User { private String id; private String name; private Integer age; @JsonFormat(pattern = "yyyy-MM-dd") private Date bir; @Override public String toString () { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", bir=" + bir + '}' ; } public User () { } public User (String id, String name, Integer age, Date bir) { this .id = id; this .name = name; this .age = age; this .bir = bir; } public String getId () { return id; } public void setId (String id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } public Date getBir () { return bir; } public void setBir (Date bir) { this .bir = bir; } }
开发控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 package com.study.controller; import com.alibaba.fastjson.JSONObject; import com.study.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; @Controller @RequestMapping("json") public class JsonController { @RequestMapping("test") @ResponseBody public Map<String,String> test () { Map<String,String> map = new HashMap <>(); map.put("message" ,"测试成功" ); return map; } @RequestMapping("findAll") public void findAll (HttpServletResponse response) throws IOException { List<User> users = new ArrayList <>(); users.add(new User (UUID.randomUUID().toString(),"熊大" ,20 ,new Date ())); users.add(new User (UUID.randomUUID().toString(),"熊二" ,10 ,new Date ())); users.add(new User (UUID.randomUUID().toString(),"光头强" ,50 ,new Date ())); String s = JSONObject.toJSONStringWithDateFormat(users, "yyyy-MM-dd" ); response.setContentType("application/json;charset=UTF-8" ); response.getWriter().println(); } @RequestMapping("showAll") public @ResponseBody List<User> showAll () { List<User> users = new ArrayList <>(); users.add(new User (UUID.randomUUID().toString(),"熊大" ,20 ,new Date ())); users.add(new User (UUID.randomUUID().toString(),"熊二" ,10 ,new Date ())); users.add(new User (UUID.randomUUID().toString(),"光头强" ,50 ,new Date ())); return users; } }
开发json.jsp页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en" > <head> <meta charset="UTF-8" > <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv="X-UA-Compatible" content="ie=edge" > <title>测试springmvc与ajax集成</title> <!--这里引了jquery:简化了JavaScript编写,自己下载复制到webapp/js包下--> <script src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js" ></script> <script> $(function(){ $("#btn" ).click(function () { <%--$.get("${pageContext.request.contextPath}/json/showAll" ,function(res){--%> <%-- console.log(res)--%> <%--},"JSON" );--%> $.get("${pageContext.request.contextPath}/json/showAll" ,function(res){ $.each(res,function(i,user){ var ul= $("<ul/>" ); var idLi = $("<li/>" ).text(user.id); var nameLi = $("<li/>" ).text(user.name); var ageLi = $("<li/>" ).text(user.age); var birLi = $("<li/>" ).text(user.bir); ul.append(idLi).append(nameLi).append(ageLi).append(birLi); $("#bd" ).append(ul); }) },"JSON" ); }); }) </script> </head> <body id="bd" > <button id="btn" >显示一群人</button> </body> </html>
启动服务器进行测试
访问路径:http://localhost:8888/springmvc02/json/test
访问路径:http://localhost:8888/springmvc02/json.jsp
访问路径:http://localhost:8888/springmvc02/json/findAll
访问路径:http://localhost:8888/springmvc02/json/showAll
14.SpringMVC中拦截器
1 作用 类似于javaweb中的Filter,用来对控制器请求进行拦截,可以将多个Controller中执行的共同代码放入拦截器中执行,减少Controller类中代码的冗余。
2 特点 拦截器器只能拦截Controller的请求,不能拦截jsp、静态资源等相关请求
拦截器可中断用户的请求轨迹
请求先经过拦截器,之后还会经过拦截器
3 自定义拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.study.interceptors;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println(((HandlerMethod)o).getMethod().getName()); System.out.println("===========1=============" ); return true ; } @Override public void postHandle (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println(modelAndView); System.out.println("===========3=============" ); } @Override public void afterCompletion (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { if (e!=null ){ System.out.println(e.getMessage()); } System.out.println("===========4=============" ); } }
4.开发控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 @RequestMapping("interceptor") public String testInterceptor () { System.out.println("===========2=============" ); throw new RuntimeException ("程序出错啦!" ); }
5.springmvc.xml中配置拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <bean id ="myInterceptor" class ="com.study.interceptors.MyInterceptor" > </bean > <mvc:interceptors > <mvc:interceptor > <mvc:mapping path ="/json/*" /> <mvc:exclude-mapping path ="/json/showAll" /> <ref bean ="myInterceptor" /> </mvc:interceptor > </mvc:interceptors >
注意:/*代表拦截所有请求路径
6 启动服务测试拦截器
访问路径:http://localhost:8888/springmvc02/json/interceptor
控制台输出结果:
(1)正常执行时
1 2 3 4 5 6 7 testInterceptor ===========1============= ===========2============= ModelAndView: reference to view with name 'index'; model is {} ===========3============= ===========4=============
(2)出错时
1 2 3 4 5 6 testInterceptor ===========1============= ===========2============= 程序出错啦! ===========4=============
访问路径:http://localhost:8888/springmvc02/json/findAll
控制台输出结果:
1 2 3 4 5 findAll ===========1============= null ===========3============= ===========4=============
访问路径:http://localhost:8888/springmvc02/json/showAll
控制台无输出结果
15.SpringMVC全局异常处理
1.作用
当控制器中某个方法在运行过程中突然发生运行时异常时,为了增加用户体验不能出现500错误代码,应该给用户良好展示错误界面,全局异常处理就能更好解决这个问题。
2 控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RequestMapping("interceptor") public String testInterceptor () { System.out.println("===========2=============" ); throw new UserNameNotFoundException ("保存失败,请稍后再试......" ); }
3 自定义异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.study.exceptions;public class UserNameNotFoundException extends RuntimeException { public UserNameNotFoundException (String message) { super (message); } }
4 全局异常处理开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.study.handlerexception; import com.study.exceptions.UserNameNotFoundException; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GlobalExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { System.out.println("进入全局异常处理器获取的异常信息为:" + e.getMessage()); ModelAndView modelAndView = new ModelAndView (); if (e instanceof UserNameNotFoundException){ modelAndView.setViewName("redirect:/login.jsp" ); }else { modelAndView.setViewName("redirect:/error.jsp" ); } modelAndView.addObject("msg" ,e.getMessage()); return modelAndView; } }
5 springmvc.xml配置全局异常处理
1 2 <bean class ="com.study.handlerexception.GlobalExceptionResolver" />
6 jsp页面
login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 用户登录</title > </head > <body > <h1 > 用户登录界面</h1 > <h1 > 登录失败:${param.msg}!</h1 > </body > </html >
error.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta name ="viewport" content ="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http-equiv ="X-UA-Compatible" content ="ie=edge" > <title > 全局错误页面</title > </head > <body id ="bd" > <h1 > 系统出现错误: ${param.msg}!</h1 > </body > </html >
7 启动服务进行测试
访问路径:http://localhost:8888/springmvc02/json/interceptor
控制台输出结果:
1 2 3 4 5 6 7 testInterceptor ===========1============= ===========2============= null 进入全局异常处理器获取的异常信息为:保存失败,请稍后重试...... ===========4=============