一、概述
在项目中总会出现各种异常、bug,为了使得用户体验更好,当系统出现异常的时候,我们需要有我们的处理方式,使得用户能够理解系统出现了什么问题。
二、异常类
首先我们需要编写一个异常类
package org.sihai.qualitycontrol.utils.exception;
public class AppException extends RuntimeException{
public AppException() {
super();
}
public AppException(String message, Throwable cause) {
super(message, cause);
}
public AppException(String message) {
super(message);
}
public AppException(Throwable cause) {
super(cause);
}
}
三、异常处理拦截器
在有了异常类之后,当出现异常的时候,我们需要有拦截器来拦截,然后转发到异常页面。
package org.sihai.qualitycontrol.utils.interceptor;
import org.apache.struts2.ServletActionContext;
import org.sihai.qualitycontrol.utils.exception.AppException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ExceptionInterceptor extends AbstractInterceptor{
public String intercept(ActionInvocation invocation) throws Exception {
try {
return invocation.invoke();
} catch (AppException e) {
//记录日志
//发送日志到程序员邮箱
//报警
ActionSupport as = (ActionSupport) invocation.getAction();
as.addActionError(as.getText(e.getMessage()));
ServletActionContext.getContext().getSession().put("flag", "yes");
return "systemerror";
} catch (Exception e) {
ActionSupport as = (ActionSupport) invocation.getAction();
as.addActionError("对不起,服务器有点累了,请联系管理员!");
ServletActionContext.getContext().getSession().put("flag", "yes");
System.out.println(e.getStackTrace());
return "systemerror";
}
}
}
关于拦截器的配置请查看:ssh项目实战----用户登录校验(struts拦截器)
四、异常处理页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>系统繁忙</title>
<style>
body,html {
margin: 0;
padding: 0;
height:100%;
}
#content {
min-height:100%;
position: relative;
}
#footer {
position: absolute;
bottom: 0;
padding: 10px 0;
width: 100%;
}
</style>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- FONT AWESOME CSS -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<!--GOOGLE FONT -->
<link href='http://fonts.googleapis.com/css?family=Nova+Flat'
rel='stylesheet' type='text/css'>
<!-- custom CSS here -->
<link href="css/404.css" rel="stylesheet" />
</head>
<body>
<div class="container" id="content">
<div class="row pad-top text-center">
<s:if test="#session.flag != 'yes'">
<div class="col-md-6 col-md-offset-3 text-center">
<h1>hi,不好意思</h1>
<h5>系统有点累了</h5>
<span id="error-link"></span>
<h2>请检查网络,返回主页,稍后再试!</h2>
</div>
</s:if>
<s:else>
<div class="col-md-6 col-md-offset-3 text-center">
<h2><s:actionerror /></h2>
</div>
</s:else>
</div>
<div class="row text-center" id="footer">
<div class="col-md-8 col-md-offset-2">
<h3>
<i class="fa fa-lightbulb-o fa-5x"></i>
</h3>
<a href="page_toIndex" class="btn btn-primary">主页</a> <br />
<br />Copyright © 2017 赣南师范大学 <a href="page_toIndex"
title="国家脐橙工程技术研究中心" target="_blank">国家脐橙工程技术研究中心</a>
</div>
</div>
</div>
<!--Core JavaScript file -->
<script src="js/jquery.js"></script>
<!--bootstrap JavaScript file -->
<script src="js/bootstrap.min.js"></script>
<!--Count Number JavaScript file -->
<script src="js/countUp.js"></script>
<!--Custom JavaScript file -->
<script src="js/custom.js"></script>
</body>
</html>
如果想获取更多源码或者视频教程,欢迎关注我的微信公众号
好好学java好好学java
,在公众号里,回复:java基础、html5、javaEE基础、struts2、spring、redis、luncene、oracle
等,将可获得以上的优质视频教程及源码。
网友评论