1.ResponseStatusExceptionResolver类
2.DefaultHandlerExceptionResolver类
3.SimpleMappingExceptionResolver类
ResponseStatusExceptionResolver类:
下面实现一个功能:
后台登录账户密码,正确返回正常页面,错误返回错误页面。
UserInfoController.java:
package com.jd.userinfo;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.jd.exception.UnauthorizedException;
import com.jd.vo.UserInfo;
@Controller
public class UserInfoController {
@RequestMapping("/userinfo/toView.do")
public String toView(String userName) {
if("Tim".equals(userName)) {
return "/userinfo/to";
}
throw new UnauthorizedException();
}
}
异常:
package com.jd.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code=HttpStatus.UNAUTHORIZED,reason="权限不够,不允许访问资源")
public class UnauthorizedException extends RuntimeException{
private static final long serialVersionUID = 1L;
}
该视图解析器用来解析@ResponseStatus标注的异常类,并把异常对应的HTTP状态码返回给客户端。
index.jsp修改为:
<a href="./userinfo/toView.do?userName=Tim">链接</a>
当用户名不对时返回:
DefaultHandlerExceptionResolver类:
该解析器用于解析程序出现的NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException、 MissingPathVariableException、MissingServletRequestParameterException、ServletRequestBindingException、ConversionNotSupportedException、TypeMismatchException、HttpMessageNotReadableException、HttpMessageNotWritableException、MethodArgumentNotValidException、MissingServletRequestPartException、BindException、NoHandlerFoundException和AsyncRequestTimeoutException异常,并把对应的HTTP状态码返回给客户端。
index.jsp:
<a href="./userinfo/toView.do">链接</a>
UserInfoController.java:
@RequestMapping(value="/userinfo/toView.do",method = RequestMethod.POST)
public String toView(String userName) {//该Handler方法尽接收POST请求
return "to";
}
流程:
1、客户端发起http://127.0.0.1/cassini/userinfo/toView.do请求;
2、由于该请求为GET请求,而服务器端处理该请求的Handler方法尽接收POST请求,所以发生HttpRequestMethodNotSupportedException异常;
3、执行DefaultHandlerExceptionResolver类内handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,HttpServletRequest request, HttpServletResponse response, Object handler)方法。
运行结果:
SimpleMappingExceptionResolver类:
该视图解析器用于服务器端当发生异常时根据发生的异常类型跳转到指定的视图以显示异常信息
UserInfoController.java:
@RequestMapping("/userinfo/toView.do")
public String toView(int i) {
System.out.println(1/i);// 该异常不能置于try-catch语句块中
return "to";
}
index.jsp:
<a href="./userinfo/toView.do?i=0">链接</a>
application.xml
<!-- 配置SimpleMappingExceptionResolver视图解析器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 配置异常属性名:exceptionAttribute默认值为exception,此时jsp页面通过${exception}显示异常信息;添加<property name="exceptionAttribute" value="ex"></property>配置后,则通过${ex}显示异常信息 -->
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<!-- 服务器端异常均指向error.jsp页面 -->
<prop key="java.lang.Throwable">error</prop>
</props>
</property>
</bean>
流程:
1、客户端发起http://localhost:8080/moon/userinfo/toView.do?i=0请求;
2、服务器端发生ArithmeticException异常,SimpleMappingExceptionResolver解析器处理该异常,request转发至error.jsp页面;