ssm架构的Javaweb项目中,错误处理的两种方式
springmvc中错误处理
1、创建错误处理的类
public class AnnotationHandlerMethodExceptionResolver extends ExceptionHandlerExceptionResolver {
private String defaultErrorView;
private String defaultJsonErrorView;
public String getDefaultErrorView() {
return defaultErrorView;
}
public void setDefaultErrorView(String defaultErrorView) {
this.defaultErrorView = defaultErrorView;
}
public String getDefaultJsonErrorView() {
return defaultJsonErrorView;
}
public void setDefaultJsonErrorView(String defaultJsonErrorView) {
this.defaultJsonErrorView = defaultJsonErrorView;
}
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
if (handlerMethod == null) {
return null;
}
Method method = handlerMethod.getMethod();
if (method == null) {
return null;
}
ModelAndView returnValue = super.doResolveHandlerMethodException(request, response, handlerMethod, exception);
ResponseBody responseBodyAnn = AnnotationUtils.findAnnotation(method, ResponseBody.class);
if (responseBodyAnn != null) {
if (null != returnValue) {
if(returnValue.getViewName() == null){
returnValue.setViewName(defaultJsonErrorView);
}
} else {
returnValue = new ModelAndView(defaultJsonErrorView);
}
} else {
if (null != returnValue) {
if(returnValue.getViewName() == null){
returnValue.setViewName(defaultErrorView);
}
} else {
returnValue = new ModelAndView(defaultErrorView);
}
}
returnValue.addObject("ex", exception);
return returnValue;
}
}
2、在springmvc.xml中添加配置
<bean id="handlerExceptionResolver" class="xxx.xxx.AnnotationHandlerMethodExceptionResolver">
<!-- 错误页面 -->
<property name="defaultErrorView" value="/err/err"/>
<property name="defaultJsonErrorView" value="/err/errjson"/>
</bean>
3、错误页
WEB-INF/jsp/err/err.jsp
<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.text.*"%>
<%
if (request.getAttribute("base") == null) {
request.setAttribute("base", request.getContextPath());
}
if (exception!=null) {
exception.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="yipeng">
<meta name="description" content="错误页">
<title>404错误</title>
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/dist/css/AdminLTE.min.css">
</head>
<body class="skin-blue" style="width: 100%;text-align: center;background: #eceff6;overflow: hidden;">
<div style="width: 354px;margin: auto; margin-top:158px;">
<img src="${base}/image/error.jpg">
<h3 style="margin-top:35px;">
<i class="fa fa-warning text-red"></i> ! 您无权限或是出现错误了
</h3>
<form class='search-form' style="margin-top:51px;">
<div style="width: 100%;text-align: center;">
<button style="width:84px;margin: auto;" type="button" class="btn btn-danger"
οnclick="{top.location.href='${base}/work/home/logout.do'}">重新登陆</button>
</div>
</form>
</div>
</body>
</html>
WEB-INF/jsp/err/err.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.* " %>
<%
response.setContentType("application/json;charset=utf-8");
Exception ex = (Exception)request.getAttribute("ex");
if (ex != null) {
ex.printStackTrace();
}
%>
{"status":"falied", "message": "<%=ex!=null?ex.getMessage():"" %>"}
web.xml中错误配置
1、在web.xml中添加配置
<!-- 设置错误页面 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error/err.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/err.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/err.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error/err.jsp</location>
</error-page>
错误页也可以根据不同错误,配置不同的页面。比如404,500等配置不同错误页面。
2、错误页 /webapp/error/err.jsp
<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.text.*"%>
<%
if (request.getAttribute("base") == null) {
request.setAttribute("base", request.getContextPath());
}
if (exception!=null) {
exception.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="yipeng">
<meta name="description" content="错误页">
<title>404错误</title>
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="${base}/js/AdminLTE-2.4.3/dist/css/AdminLTE.min.css">
</head>
<body class="skin-blue" style="width: 100%;text-align: center;background: #eceff6;overflow: hidden;">
<div style="width: 354px;margin: auto; margin-top:158px;">
<img src="${base}/image/error.jpg">
<h3 style="margin-top:35px;">
<i class="fa fa-warning text-red"></i> ! 您无权限或是出现错误了
</h3>
<form class='search-form' style="margin-top:51px;">
<div style="width: 100%;text-align: center;">
<button style="width:84px;margin: auto;" type="button" class="btn btn-danger"
οnclick="{top.location.href='${base}/work/home/logout.do'}">重新登陆</button>
</div>
</form>
</div>
</body>
</html>
测试错误页
这两种方式都有必要,经过springmvc处理产生的错误自然走springmvc的错误处理,但是没有经过springmvc产生的错误就可以又web.xml中配置的来处理。
springmvc错误测试:在controller的方法中添加 int a = 1/0; 然后访问这个路径。
web.xml错误测试:将默认首页删除,然后ip端口项目名直接访问项目,就会走到这个错误页。