Spring MVC -- 异常处理
程序员文章站
2024-03-15 18:19:36
...
1.自定义一个异常处理器。当系统出现任何异常都会跳转到当前页面。
自定义异常处理器:
package com.allen.spring;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ModelAndView mv = new ModelAndView("error");
if(ex instanceof IllegalArgumentException){
mv.addObject("errorType","arguments is illegal");
}
mv.addObject("message",ex.getMessage());
ByteArrayOutputStream out = new ByteArrayOutputStream();
ex.printStackTrace(new PrintStream(out,true));
mv.addObject("stack",out.toString());
return mv;
}
}
controller:
package com.allen.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class RequestMappingControl {
@RequestMapping("/hello.do")
public ModelAndView helloWorld1(HttpServletRequest request, HttpServletResponse response){
ModelAndView mv = new ModelAndView("user");
if(request.getParameter("name").length() < 2){
throw new IllegalArgumentException("argument count is less than 2, please check !");
}
return mv;
}
}
error 页面:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/11/13
Time: 21:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<b> you see this page because system have fatal error!!! </b>
<h3>error Message: ${message}</h3>
<h4>errorType : ${errorType}</h4>
<h5>stack : ${stack}</h5>
</body>
</html>
需要不自定义的异常处理器加入到spring bean管理。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.allen.spring.RequestMappingControl"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
</bean>
<bean class="com.allen.spring.MyExceptionResolver"/>
</beans>
结果测试:
推荐阅读
-
Spring Mvc controller单元测试
-
Spring MVC -- 异常处理
-
javascript基础知识(21) 异常处理
-
$Proxy0 cannot be cast to com.performer.service.impl.PoeticJuggler 博客分类: Spring $Proxy0 cannot be cast toaop异常
-
$Proxy0 cannot be cast to com.performer.service.impl.PoeticJuggler 博客分类: Spring $Proxy0 cannot be cast toaop异常
-
Java编程思想 第十二章:通过异常处理错误
-
SSH项目管理--Spring注入异常解决方法 博客分类: Spring
-
Spring MVC静态资源处理 博客分类: Spring springmvcstatic静态mapping
-
异常解决——获取Spring上下文AppplicationContextAware时applicationContext为null
-
异常处理(二)---------Field * in * required a bean of type '*' that could not be found.