日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/20 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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>

總結

以上是生活随笔為你收集整理的【struts2+hibernate+spring项目实战】统一异常处理(ssh)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。