spring对于非ajax提交的请求可以有三种处理方式:一种是将异常映射为HTTP状态码,通过状态码将不同的错误状态定义到指定的错误页面。一种是通过@ExceptionHandler处理指定的异常类型。还有一种方式是通过实现HandlerExceptionResolver自定义一个处理异常的类。
非Ajax提交异常处理
一:将异常映射为HTTP状态码
1 首先在异常的地方抛出自定义异常 throw new NotFoundException()
@RequestMapping("login/{testId}")
public String exceptionTest(@PathVariable(value="testId") String testId,Model model){
System.out.println(testId);
if(true){
throw new NotFoundException();
}
return "home/other";
}
2 新建一个异常类
NotFoundException
@ResponseStatus(value=HttpStatus.NOT_FOUND,reason="resource not found")
public class NotFoundException extends RuntimeException{
}
类中指定了该异常对应的HTTP状态码
3 在web.xml文件中配置不同的错误码对应的错误页面
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
4 在/WEB-INF/views/error文件夹下新建404.jsp文件,并输出错误提示信息。但是这种处理异常的方式无法显示错误的详信息。下面我们将介绍第二种处理异常的方式,其可以展示具体的异常信息。
二:通过ExceptionHandler注解,定义一个方法处理某一类异常,通常我们会建一个BaseException类所有的controller都继承该类,那么在所有的controller里面抛出了指定的异常,就可以根据异常类去找指定的异常处理方法。
1 新建一个BaseException类
public class BaseException {
@ExceptionHandler(RuntimeException.class)
public String RuntimeException(HttpServletRequest request,Exception e){
request.setAttribute("exception", e);
return "error/404";
}
}
上面的 RuntimeException 可以定义多个方法处理不同的异常。这里将异常信息存入request中,以便在error/404.jsp里面获取详细的异常信息。
2 新建一个Controller (每个Controller类都继承BaseException类)
@Controller
public class LoginController extends BaseException{
@RequestMapping("exceptionHandlerText")
public String exceptionHandlerText(HttpServletRequest request,Model model){
int i = 5/0;
return "home/other";
}
}
3 新建一个获取异常信息的ExceptionUtils.java
public class ExceptionUtils {
/**
* 在request中获取异常类
* @param request
* @return
*/
public static Throwable getException(HttpServletRequest request){
Throwable ex = null;
if (request.getAttribute("exception") != null) {
ex = (Throwable) request.getAttribute("exception");
}
return ex;
}
}
4 在/WEB-INF/views/error文件夹下新建404.jsp文件,并输出错误提示信息
<%@page import="com.zsq.cn.common.exceptions.ExceptionUtils"%>
<%@page contentType="text/html;charset=UTF-8" isErrorPage="true"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Throwable ex = ExceptionUtils.getException(request);
StringBuilder sb = new StringBuilder("错误信息:");
if (ex != null) {
sb.append(ex.getMessage());
}
%>
<h2>404</h2>
<div class="errorMessage">
<%=sb.toString()%>
<br />
</div>
<a href="javascript:" οnclick="history.go(-1);" class="btn">返回上一页</a>
三:实现HandlerExceptionResolver,自定义一个处理异常的类
1 新建一个类 实现 HandlerExceptionResolver
public class UnifiedExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
request.setAttribute("exception", ex);
return new ModelAndView("erroe/404");
}
}
2 新建一个controller
public class LoginController{
@RequestMapping("exceptionHandlerText")
public String exceptionHandlerText(HttpServletRequest request,Model model){
int i = 5/0;
return "home/other";
}
}
3 在/WEB-INF/views/error文件夹下新建404.jsp文件,并输出错误提示信息
<%@page import="com.zsq.cn.common.exceptions.ExceptionUtils"%>
<%@page contentType="text/html;charset=UTF-8" isErrorPage="true"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Throwable ex = ExceptionUtils.getException(request);
StringBuilder sb = new StringBuilder("错误信息:");
if (ex != null) {
sb.append(ex.getMessage());
}
%>
<h2>404</h2>
<div class="errorMessage">
<%=sb.toString()%>
<br />
</div>
<a href="javascript:" οnclick="history.go(-1);" class="btn">返回上一页</a>
</body>
</html>
4 :将UnifiedExceptionResolver.java作为bean注入到容器中(三种方式:xml、java、@Component注解)
对于Ajax提交,上面最后两种处理方式都可以实现异常的处理。
一:实现HandlerExceptionResolver,自定义一个处理异常的类 处理Ajax提交
1 实现HandlerExceptionResolver,自定义一个处理异常的类
public class UnifiedExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
try (PrintWriter out = response.getWriter()){
out.write("错误信息:"+ex.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
2 新建一个controller
@Controller
public class LoginController extends BaseException{
@RequestMapping("exceptionHandlerText")
@ResponseBody
public String exceptionHandlerText(HttpServletRequest request,Model model){
int i = 5/0;
return "home/other";
}
}
3 在页面请求 controller中的映射
$.post('exceptionHandlerText',function(dd){
console.log(dd);
});
4 :将UnifiedExceptionResolver.java作为bean注入到容器中(三种方式:xml、java、@Component注解)