當(dāng)前位置:
首頁 >
【struts2+hibernate+spring项目实战】统一异常处理(ssh)
發(fā)布時(shí)間:2025/3/20
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【struts2+hibernate+spring项目实战】统一异常处理(ssh)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、概述
在項(xiàng)目中總會(huì)出現(xiàn)各種異常、bug,為了使得用戶體驗(yàn)更好,當(dāng)系統(tǒng)出現(xiàn)異常的時(shí)候,我們需要有我們的處理方式,使得用戶能夠理解系統(tǒng)出現(xiàn)了什么問題。
二、異常類
首先我們需要編寫一個(gè)異常類
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);}}三、異常處理攔截器
在有了異常類之后,當(dāng)出現(xiàn)異常的時(shí)候,我們需要有攔截器來攔截,然后轉(zhuǎn)發(fā)到異常頁面。
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) {//記錄日志//發(fā)送日志到程序員郵箱//報(bào)警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("對不起,服務(wù)器有點(diǎn)累了,請聯(lián)系管理員!");ServletActionContext.getContext().getSession().put("flag", "yes");System.out.println(e.getStackTrace());return "systemerror";}}}關(guān)于攔截器的配置請查看:ssh項(xiàng)目實(shí)戰(zhàn)----用戶登錄校驗(yàn)(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>系統(tǒng)繁忙</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>系統(tǒng)有點(diǎn)累了</h5><span id="error-link"></span><h2>請檢查網(wǎng)絡(luò),返回主頁,稍后再試!</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 贛南師范大學(xué) <a href="page_toIndex"title="國家臍橙工程技術(shù)研究中心" target="_blank">國家臍橙工程技術(shù)研究中心</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>總結(jié)
以上是生活随笔為你收集整理的【struts2+hibernate+spring项目实战】统一异常处理(ssh)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【struts2+hibernate+s
- 下一篇: 【struts2+hibernate+s