【struts2+hibernate+spring项目实战】统一异常处理(ssh)

一、概述

在项目中总会出现各种异常、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 &copy; 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello-java-maker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值