//列表页面 public String listUI(){ int i=1/0; userList=userService.findObjects(); return "listUI"; }然后我们来访问我们的我用户列http://localhost/HpuTax/tax/user_listUI.action,那么便会看到这种错误界面:
package cn.edu.hpu.tax.core.exception; public abstract class SysException extends Exception{ //错误信息 private String errorMsg; public String getErrorMsg() { return errorMsg; } public void setErrorMsg(String errorMsg) { this.errorMsg = errorMsg; } //下面几个全是不同的构造方法 public SysException() { super(); // TODO Auto-generated constructor stub } public SysException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } public SysException(String message, Throwable cause) { super(message, cause); errorMsg=message; } public SysException(String message) { super(message); errorMsg=message; } public SysException(Throwable cause) { super(cause); } }
package cn.edu.hpu.tax.core.exception; public class ServiceException extends SysException{ public ServiceException() { //业务层的默认错误信息 super("业务操作错误!"); } public ServiceException(String message) { super(message); } }
package cn.edu.hpu.tax.core.exception; public class ActionException extends SysException{ public ActionException() { super("请求发生错误!"); } public ActionException(String message) { super(message); } }
@Override public List<User> findObjects() throws ServiceException{ try { int i = 1 / 0; } catch (Exception e) { throw new ServiceException("service 出错!"+e.getMessage()); } return userDao.findObjects(); }
//列表页面 public String listUI() throws SysException{ try { userList=userService.findObjects(); } catch (ServiceException e) { throw new ActionException("action 出现异常!"+e.getMessage()); } return "listUI"; }
<!-- 配置全局结果集异常映射 --> <package name="base-default" extends="struts-default"> <!-- 全局返回结果 --> <global-results> <result name="sysError">/WEB-INF/jsp/error.jsp</result> <result name="input">/WEB-INF/jsp/error.jsp</result> </global-results> <!-- 全局异常映射 --> <global-exception-mappings> <exception-mapping result="sysError" exception="cn.edu.hpu.tax.core.exception.SysException"/> <exception-mapping result="input" exception="java.lang.Exception"/> </global-exception-mappings> </package>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>系统异常信息</title> </head> <body> <img src="<%=request.getContextPath() %>/images/common/error.jpg"> <br> <s:if test="exception.errorMsg != '' && exception.errorMsg != null"> <s:property value="exception.errorMsg"/> </s:if> <s:else> 操作失败!<s:property value="exception.message"/> </s:else> </body> </html>
<package name="user-action" namespace="/tax" extends="base-default">
发现弹出了我们自定义的错误界面,并且阐述了哪几层出现错误,最终错误的原因。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49454059