侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringMVC(8)之全局异常处理

zze
zze
2017-12-21 / 0 评论 / 0 点赞 / 363 阅读 / 1247 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

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"/>
0

评论区