本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

SpringMVC(8)之全局异常处理

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


1、创建一个消息通知页:

<!-- WEB-INF/jsp/msg.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>消息页</title>
</head>
<body>
${msg}
</body>
</html>

2、编写自定义的异常处理器:

// com.zze.springmvc.web.util.MyExceptionResolver
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("msg");
        modelAndView.addObject("msg", "服务器异常!");
        return modelAndView;
    }
}

3、注册全局异常处理器到 IoC 容器:

<bean class="com.zze.springmvc.web.util.MyExceptionResolver"/>
# SpringMVC  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×