【struts2+hibernate+spring项目实战】统一异常处理(ssh)
2018-01-21 11:52
495 查看
一、概述
在项目中总会出现各种异常、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>
相关文章推荐
- 【struts2+hibernate+spring项目实战】统一异常处理(ssh)
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- 使用Spring MVC统一异常处理实战
- 使用Spring MVC统一异常处理实战
- 使用Spring MVC统一异常处理实战
- Spring Cloud实战小贴士:Zuul统一异常处理(二)
- 使用Spring MVC统一异常处理实战
- SSH项目中异常处理与日志记录
- 使用Spring MVC统一异常处理实战
- 使用Spring MVC统一异常处理实战
- ssh项目异常用处理方式
- 使用Spring MVC统一异常处理实战
- 【SSH项目实战】国税协同平台-9.全局异常映射
- 使用Spring MVC统一异常处理实战
- SSH项目常见异常处理之[org.hibernate.LazyInitializationException: could not initialize proxy - no Session]
- Spring Cloud实战小贴士:Zuul统一异常处理(一)
- 【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示
- 使用Spring MVC统一异常处理实战
- 使用Spring MVC统一异常处理实战