key=value&key=value&key=value;
request.getParameter();
底层: form表单是一个大请求体:
request.getParameter(); 失效
每一个组件都是一个小的请求体:
------------------------------------------------------
username=lisi
-------------------------------------------------------
age=21
------------------------------------------------------
file=xxx.jpg
------------------------------------------------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.etimegroupId>
<artifactId>day0413artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>5.2.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
dependencies>
project>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:application.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.etime">context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:default-servlet-handler>mvc:default-servlet-handler>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
beans>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
message.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
message
测试消息
@Controller
@RequestMapping("/upload")
public class TestController {
@PostMapping("test01")
public String test01(MultipartFile photo){
// 文件存放位置
String path = "D:"+ File.separator+"upload";
File file = new File(path);
// 文件不存在就创建
if (!file.exists()) {
file.mkdirs();
}
// 获取上传的文件名
String originalFilename = photo.getOriginalFilename();
// 上传
try {
/*File参数:
参数1:父文件对象
参数2:子文件名
*/
photo.transferTo(new File(file,originalFilename));
} catch (IOException e) {
e.printStackTrace();
}
return "message";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
编写控制器代码
@PostMapping("test02")
public String test02(MultipartFile[] photo){
// 文件存放位置
String path = "D:"+ File.separator+"upload";
File file = new File(path);
// 文件不存在就创建
if (!file.exists()) {
file.mkdirs();
}
for (MultipartFile f : photo) {
// 获取上传的文件名
String originalFilename = f.getOriginalFilename();
// 上传
try {
/*File参数:
参数1:父文件对象
参数2:子文件名
*/
f.transferTo(new File(file,originalFilename));
} catch (IOException e) {
e.printStackTrace();
}
}
return "message";
}
dao层、service层、controller层 出现都通过 throws Exception 向上抛出,最后由 springmvc 前端控制器交由异常处理器进行异常处理。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
测试
@Controller
@RequestMapping("/exceptionController")
public class ExceptionController {
@GetMapping("test")
public String test(){
System.out.println("异常处理 进来了");
int a = 10 / 0;
return "message";
}
}
测试原生报错异常
(1)引入servlet-api依赖
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
(2) 编写自定义处理器代码
@Component
public class MyException implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, Exception e) {
e.printStackTrace();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("message");
return modelAndView;
}
}
(3)编写处理异常的页面message
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
message
测试消息,出错了
区别 | 过滤器(Filter) | 拦截器(Interceptor) |
---|---|---|
使用场景 | 过滤器 是Servlet规范的一部分,任何 JavaWeb 工程都可以使用。 | 拦截器 是SpringMVC 框架自己的,只有使用了SpringMVC框架的工程才能用。 |
作用 | 过滤器 在web.xml中 url-pattern标签中配置了 /* 后,可对所用要访问的资源进行拦截。资源:html、css、js、image | 拦截器 只会拦截拦截访问的控制器方法,若访问的是 jsp、html、css、image或者js是不会进行拦截的。拦截器也是AOP思想的具体应用。 |
总结 | 拦截资源: /* : js、css、html、image | 拦截资源:/**:控制器方法、jsp |
public interface HandlerInterceptor {
// 控制层执行器方法前 的拦截器
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*
true:表示继续执行控制层控制器方法
false:表示方法结束,不会执行控制层执行器方法,也就是说不会进入控制层
*/
return true;
}
// 控制层方法返回时 的拦截器
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
// 控制器方法结束后 的拦截器
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
public class MyInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("控制层执行器方法 前 的控制器");
// 注意返回值是 true,还是true
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("控制层执行器方法 返回值 的控制器");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
System.out.println("控制层执行器方法 结束后 的控制器");
}
}
@GetMapping("test03")
public String test03(){
System.out.println("拦截器测试方法 test03 进入拦截器");
return "message";
}
@GetMapping("test04")
public String test04(){
System.out.println("拦截器测试方法 test04 进入拦截器");
return "message";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test01
测试3
测试4
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/testController/test04"/>
<bean class="com.etime.interceptor.MyInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
点击测试04,而preHandle() 这个方法的返回值是 false,但是在application.xml文件中配置了不拦截test04的信息,故它没有被拦截,而进入了Controller层执行方法。
(1)控制层执行器方法
@GetMapping("test03")
public String test03(){
System.out.println("拦截器测试方法 test03");
return "message";
}
@GetMapping("test04")
public String test04(){
System.out.println("拦截器测试方法 test04");
return "message";
}
(2)编写拦截器代码
public class FirstInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("控制层执行器 前 的拦截器1");
/*
true表示继续执行控制层执行器方法,
false表示方法结束,不会执行控制层执行器方法
*/
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("控制层执行器方法 返回值 时的拦截器1");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("控制层执行器方法 结束后的拦截器1");
}
}
public class SecondInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("控制层执行器 前 的拦截器2");
/*
true表示继续执行控制层执行器方法,
false表示方法结束,不会执行控制层执行器方法
*/
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("控制层执行器方法 返回值 时的拦截器2");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("控制层执行器方法 结束后的拦截器2");
}
}
(3)配置拦截器application.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/testController/test04"/>
<bean class="com.etime.interceptor.FirstInterceptor">bean>
mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/testController/test04"/>
<bean class="com.etime.interceptor.SecondInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
(4)编写测试页面test01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
test01
测试3
测试4
(5)测试
结果截图:
拦截器执行过程:
(1)登录页面login.jsp bianxie
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
login
(2)控制层执行器方法login编写
@Controller
@RequestMapping("/userController")
@SessionAttributes("username")
public class UserController {
@PostMapping("login")
public ModelAndView login(String username,String password){
System.out.println("username = " + username);
ModelAndView modelAndView = new ModelAndView();
// 模拟登录成功
boolean res = true;
if (res){
modelAndView.addObject("username",username);
modelAndView.setViewName("message");
}else {
System.out.println("密码或用户名错误");
}
return modelAndView;
}
}
(3)拦截器编写
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
System.out.println("loginInterceptor username = "+username);
if (username != null && username != ""){
System.out.println("已登录");
response.sendRedirect("../message.jsp");
return true;
}else {
System.out.println("未登录");
response.sendRedirect("../login.jsp");
return false;
}
}
}
(4)注册拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/userController/login"/>
<bean class="com.etime.interceptor.LoginInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
(5)测试
edirect(“…/message.jsp”);
return true;
}else {
System.out.println(“未登录”);
response.sendRedirect(“…/login.jsp”);
return false;
}
}
}
(4)注册拦截器
```xml
(5)测试