springmvc如何进行异常处理
异常处理
局部处理(直接写在处理器中)
@ExceptionHandler
public ModelAndView error(Exception exception) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", exception.getMessage());
mv.setViewName("forward:../error.jsp");
return mv;
}
全局处理(新建一个类,用来接收获得的所有的异常,以此避免在每一个类中进行异常处理)
代码如下:
package com.zhiyou100.wyf.controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler
public ModelAndView error(Exception exception) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", exception.getMessage());
mv.setViewName("forward:../error.jsp");
return mv;
}
}
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"></script>
</head>
<body>
${error }
</body>
</html>
以上是 springmvc如何进行异常处理 的全部内容, 来源链接: utcz.com/z/355271.html