2011年9月5日星期一

自定义异常处理类

packagecom.capinfo.webapp.handler;

 

importorg.apache.commons.logging.Log;

importorg.apache.commons.logging.LogFactory;

importorg.springframework.web.servlet.HandlerExceptionResolver;

importorg.springframework.web.servlet.ModelAndView;

 

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjava.util.HashMap;

importjava.util.Map;

 

/**

 * Created by IntelliJ IDEA.

 * User: liuzhaochun

 * Date: 11-8-12

 * Time: 下午5:18

 * 自定义异常处理类.

 */

public classMyHandlerExceptionResolver implements HandlerExceptionResolver {

    private Log log =LogFactory.getLog(getClass());

 

    public ModelAndViewresolveException(HttpServletRequest request,

                                        HttpServletResponse httpServletResponse,

                                         Object o, Exception ex) {

        log.warn("Handle exception: "+ ex.getClass().getName());

 

        Map model = newHashMap();

        model.put("ex",ex.getClass().getSimpleName());

        model.put("error",ex.getMessage());

        return newModelAndView("error", model);

    }

}


Jsp:error.jsp

<%@page language="java" pageEncoding="UTF-8"contentType="text/html;charset=UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

    <%@ includefile="/include/meta.jsp"%> 

</head>

 

<bodyid="error">

    <div id="page">

        <div id="content"class="clearfix">

            <div id="main">

                <h1>哦!</h1>

                ${ex}</br>

                ${error}

            </div>

        </div>

    </div>

</body>

</html>

没有评论:

发表评论