【SSH项目实战】国税协同平台-9.全局异常映射

上次我们的校验工作已经完成,下面我们谈谈系统异常的处理。

假设我们在UserAction的listUI方法中加入一句错误代码:
//列表页面
public String listUI(){
	int i=1/0;
	userList=userService.findObjects();
	return "listUI";
}
然后我们来访问我们的我用户列http://localhost/HpuTax/tax/user_listUI.action,那么便会看到这种错误界面:


为了让用户不看到这种错误界面,转化为友好界面,我们就要对系统的异常进行包装处理。

系统可能出现的异常:(异常分类)
dao: 比较致命的操作异常,是会有很大可能影响整次的请求
service: 业务层的异常信息需要自定义
action: 控制层的异常信息需要自定义

我们在核心代码包中创建我们的全局异常以及各层异常处理类:
SysException.java:
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);
	}
	
}

ServiceException.java:
package cn.edu.hpu.tax.core.exception;

public class ServiceException extends SysException{


	public ServiceException() {
		//业务层的默认错误信息
		super("业务操作错误!");
	}


	public ServiceException(String message) {
		super(message);
	}
	
}

ActionException.java:
package cn.edu.hpu.tax.core.exception;

public class ActionException extends SysException{

	public ActionException() {
		super("请求发生错误!");
	}

	public ActionException(String message) {
		super(message);
	}
	
}

我们下面来使用一下这些异常类,我们在UserServiceImpl中故意出一个错误:
@Override
public List<User> findObjects() throws ServiceException{
	try {
		int i = 1 / 0;
	} catch (Exception e) {
		throw new ServiceException("service 出错!"+e.getMessage());
	}
	return userDao.findObjects();
}

然后在Action层捕获异常:
//列表页面
public String listUI() throws SysException{
	try {
		userList=userService.findObjects();
	} catch (ServiceException e) {
		throw new ActionException("action 出现异常!"+e.getMessage());
	}
	return "listUI";
}

接下来我们配置总的全局异常处理配置,我们在struts的配置文件struts.xml中加入以下代码:
<!-- 配置全局结果集异常映射 -->
<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>

然后编写error.jsp界面:
<%@ 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>

然后让我们的user的struts配置中使用我们的异常处理(把extends从struts-default改为base-default):
<package name="user-action" namespace="/tax" extends="base-default">

然后我们查看用户列表来测试一下:


发现弹出了我们自定义的错误界面,并且阐述了哪几层出现错误,最终错误的原因。

转载请注明出处:http://blog.csdn.net/acmman/article/details/49454059

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

光仔December

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

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

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

打赏作者

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

抵扣说明:

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

余额充值