spring mvc的错误控制机制

本文介绍了一种改进的SpringMVC错误处理方法,通过配置exceptionResolver实现异常映射,自动跳转到指定错误页面,并展示了DaoException和ManagerException的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring mvc的错误控制机制
以前我处理错误的时候都是采用的全部try catch,不漏过一个Exception,主要那个时候在淘宝
如果一个错误没有抓住抛到页面去了后果比较严重。所以在dao,manager,action等层都留下了很多
try catch的代码,也是一个不小的工作量了。

昨天和同事争论了很久,他提出了这样的错误控制方式,今天正巧我又看到一片文章,试了试,这样
的确很爽。所以纪录一下。哈哈。

我首先在spring-servlet.xml里面增加了一个exceptionResolver,配置和jspresolver在前缀后缀上有些
关系,所以一起贴出来:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix" value="/template/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>error/defaultError</value>
</property>
<property name="exceptionMappings">
<props>
<prop
key="com.sillycat.plugin.commons.exceptions.ManagerException">
error/managerError
</prop>
<prop
key="com.sillycat.plugin.commons.exceptions.DaoException">
error/daoError
</prop>
</props>
</property>
</bean>

这样处理就是当遇到ManagerException时会自动转向error/managerError页面
当遇到DaoException时会自动转向 error/daoError页面

页面如下,templete/jsp/error/managerError.jsp:
<%@ page contentType="text/html;charset=GBK" language="java"
pageEncoding="GBK"%>
<%@ include file="/template/jsp/include/taglibs.jsp"%>
<%@ page import="java.util.Enumeration,java.util.Iterator"%>
<script>
function showErr(){
var isHidde = document.all.isHidde.value;
//alert(isHidde);
if( isHidde == "true" ){
document.all.errdiv.style.display='block';
document.all.isHidde.value= 'false';
document.all.showbtn.value="隐藏错误信息";
}else{
document.all.errdiv.style.display='none';
document.all.isHidde.value= 'true';
document.all.showbtn.value="显示错误信息";
}
}
</script>
<html>
<head>
<title>this is failure</title>
</head>
<body onload="showErr()">
出错啦。。。。。。。。。。。。。。。
<br />
<c:set value="${exception}" var="ee" />
<jsp:useBean id="ee" type="java.lang.Exception" />
<%=ee.getMessage()%><br />
<input type="hidden" id="isHidde" value="false" />
<input type="button" id="showbtn" onclick="showErr();" />
<br>


<table id="errdiv" align="center" bgcolor="black">
<tr>
<td>
<font color="green"> <%
ee.printStackTrace(new java.io.PrintWriter(out));
%> </font>
</td>
</tr>
</table>


</body>
</html>

DaoException修改为集成自runtimeException,DaoException.java:
package com.sillycat.plugin.commons.exceptions;


public class DaoException extends RuntimeException {

private static final long serialVersionUID = 1L;

public DaoException() {
super();
}

public DaoException(String msg) {
super(msg);
}

public DaoException(Throwable cause) {
super(cause);
}

public DaoException(String msg, Throwable cause) {
super(msg, cause);
}

}

ManagerException.java如下:
package com.sillycat.plugin.commons.exceptions;

public class ManagerException extends RuntimeException {

private static final long serialVersionUID = 1L;

// error code
public String errorCode;

public ManagerException() {
super();
}

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

public ManagerException(String message, Throwable cause) {
super(message, cause);
}

public ManagerException(Throwable cause) {
super(cause);

}

public ManagerException(String errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public ManagerException(String errorCode, String message, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
}

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

}

这样配置和设计后,以后Dao层这样书写,UserDaoIbatisImpl.java:
package com.sillycat.core.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sillycat.core.dao.UserDao;
import com.sillycat.core.model.User;
import com.sillycat.plugin.commons.base.BaseSqlMapClientDaoSupport;
import com.sillycat.plugin.commons.exceptions.DaoException;

public class UserDaoIbatisImpl extends BaseSqlMapClientDaoSupport implements
UserDao {

private static final String LOAD_ALL_USERS = "getUser";

public List getAll() {
List list = null;
try {
list = getSqlMapClientTemplate().queryForList(LOAD_ALL_USERS, null);
} catch (Exception e) {
throw new DaoException(e);
}
return list;
}

public User get(Integer id) {
return null;
}

public void save(User o) {

}

public void delete(Integer id) {

}

}

在manager层这样调用,不抓dao层的错误
public List getAllUser() {
if(true){
throw new ManagerException("0","test");
}
List list = userIbatisDao.getAll();
return list;
}
当DaoException抛出时会走daoError.jsp页面。

如果Controller层没有抓ManagerException.jsp,页面也会统一走向managerError.jsp
如果这样书写
protected void onList(HttpServletRequest request,
HttpServletResponse response, ModelAndView mav) throws Exception {

List list = null;
try {
list = userManager.getAllUser();
} catch (ManagerException e) {
System.out.println("aaaaaaaaaaaaaaaaaaaaaaa");
}
mav.addObject("items", list);
}
就不会走managerError.jsp了。需要自己控制提示信息和走向
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值