分享知识-快乐自己:Spring中的(三种)异常处理机制

2021-06-22 05:03

阅读:583

YPE web->

标签:配置   title   自定义异常   ann   编写   and   erro   ESS   eof   

案例目录结构:

技术分享图片

Web.xml 配置:

span>app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web ApplicationencodingFilterclass>org.springframework.web.filter.CharacterEncodingFilterclass>
    encodingUTF-8forceEncodingtrueencodingFilter/*  mvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath*:Spring-*.xml1mvc/

第一种:使用Spring自带的异常处理机制:

ControllerWelcome:控制层:

package controller;
import exception.NameException;
import exception.PwdException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @author asus
 */
@Controller
@RequestMapping("/user")
public class ControllerWelcome{
    @RequestMapping("/name")
    public String nameException() {
        System.out.println("用户名异常!!!");
        if (true) {
            throw new NameException("用户名异常");
        }
        return "Welcome";
    }
    @RequestMapping("/pwd")
    public String pwdException() {
        System.out.println("密码异常!!!");
        if (true) {
            throw new PwdException("密码异常");
        }
        return "Welcome";
    }
    @RequestMapping("/other")
    public String otherException() {
        System.out.println(5 / 0);
        return "Welcome";
    }
}

定义的异常类:NameException  (OtherException、PwdException;结构都一样 都继承 RuntimeException 实现三个方法)这里列举一个

package exception;

/**
 * 密码异常
 * @author asus
 */
public class PwdException extends RuntimeException{

    public PwdException() {
        super();
    }

    public PwdException(String message) {
        super(message);
    }

    public PwdException(String message, Throwable cause) {
        super(message, cause);
    }
}

定义的 exception下的 jsp 页面同样这里列举一个:(其他两个只是换了一下 h1的内容做了一下区分而已)

NameException.jsp:

Title

用户名异常

Spring-Mvc.xml 配置:


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    package="controller"/>
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        

Spring-excption.xml 配置:


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        exception/NameExceptionexception/PwdException

 提示:按住  SimpleMappingExceptionResolver 进来我们的底层查看,发现有个一常量值,就是用来获取我们编写的错误信息的(存放在了request作用域下)。如下所示

技术分享图片

 Welcome.jsp :


Title
${exception}

 例如要访问:localhost:8080/user/name:这时页面就会跳转 Welcome.jsp 并且显示 用户名异常

 

第二种:自定义异常处理机制:

在上述中的 exception 包下添加我们的自定义的类:MyExceptionResolve 并且继承 AbstractHandlerExceptionResolver

MyExceptionResolve :

package exception;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

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

/**
 * 自定义异常处理机制
 * @author asus
 */
public class MyExceptionResolve extends AbstractHandlerExceptionResolver {
    /**
     * @param request:请求
     * @param response:响应
     * @param handler:执行的某一Controller 控制器
     * @param ex:异常信息
     * @return
     */
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //默认为其他异常页面
        ModelAndView modelAndView=new ModelAndView("exception/OtherException");
        //判断是什么类型的异常
        if(ex instanceof NameException)
        {
            modelAndView.setViewName("exception/NameException");
        }else if(ex instanceof PwdException)
        {
            modelAndView.setViewName("exception/PwdException");
        }
        modelAndView.addObject("exception",ex);
        return modelAndView;
    }
}

修改我们的配置文件:Spring-excption.xml 配置:


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    
    class="exception.MyExceptionResolve"/>
    

还是按照上述的 方式运行页面即可 查看效果......

 

第三种:使用注解实现异常处理机制:

 为了方便阅读:专门提升了一个编写注解方式的类:BaseControllerEx

package exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
 * 使用注解实现异常处理机制
 * @author asus
 */
@Controller
public class BaseControllerEx {
    @ExceptionHandler(NameException.class)
    public ModelAndView nameEx(NameException ex)
    {
        ModelAndView modelAndView=new ModelAndView("exception/NameException");
        modelAndView.addObject("exception",ex.getMessage());
        return modelAndView;
    }
    @ExceptionHandler(PwdException.class)
    public ModelAndView nameEx(PwdException ex)
    {
        ModelAndView modelAndView=new ModelAndView("exception/PwdException");
        modelAndView.addObject("exception",ex.getMessage());
        return modelAndView;
    }
}

注解的方式就不需要 Spring-excption.xml 配置了,所以直接干掉就可以或者注释掉配置

这时我们只需要稍微改动一下我们的 ControllerWelcome 类去继承我们的注解类:

技术分享图片

运行方式同上述一致......

 

分享知识-快乐自己:Spring中的(三种)异常处理机制

标签:配置   title   自定义异常   ann   编写   and   erro   ESS   eof   

原文地址:https://www.cnblogs.com/mlq2017/p/9680920.html


评论


亲,登录后才可以留言!