首页 > 编程语言 > 详细

SpringMVC4.x学习系列之全局异常处理

时间:2016-05-31 10:29:42      阅读:289      评论:0      收藏:0      [点我收藏+]
程序编码方式:
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class ExceptionControllerAdvice {
    
    private Logger log = LogManager.getLogger("ExceptionControllerAdvice");

    @ExceptionHandler
    public String expAdvice(HttpServletRequest req, Exception ex){
        req.setAttribute("ex", ex.getMessage());
        log.error(ex);
        //返回错误提示页面,实际开发时可根据不同的异常类型区别对待。
        return "errors/error";
    }
}
XML配置方式:
 <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
    <property name="exceptionMappings">  
        <props>  
            <prop key="java.lang.Exception">errors/error</prop>
            <prop key="java.lang.Throwable">errors/error</prop>
        </props>  
    </property>      
    <property name="defaultErrorView" value="errors/error"/>
</bean>

 

SpringMVC4.x学习系列之全局异常处理

原文:http://www.cnblogs.com/dar521lin/p/5544885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!