首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

spring mvc的异常控制机制

2012-11-13 
spring mvc的错误控制机制spring mvc的错误控制机制以前我处理错误的时候都是采用的全部try catch,不漏过

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

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

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

<bean id="exceptionResolver"
   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了。需要自己控制提示信息和走向
1 楼 java_newclass_eye 2010-12-20   您好,请问如何将ftplet 添加到 发ftpserver classpath中,如何将 fetlet class文件 发布到 ftpserver中 ,非常感谢, 刚刚接触ftp。 2 楼 sillycat 2010-12-20   ftp在http://www.open-open.com有个开源的实现吧。你可以去搜索下。
或者参考下这个:
http://sillycat.iteye.com/blog/563904

热点排行