欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 -- 异常处理

Spring MVC -- 异常处理