• Spring MVC学习06异常解析器


    自定义Exception类:

    1 package com.yas.ex;
    2 
    3 public class MyException1 extends Exception {
    4     public MyException1(String msg){
    5         super(msg);
    6     }
    7 }
    1 package com.yas.ex;
    2 
    3 public class MyException2 extends Exception{
    4     public MyException2(String msg){
    5         super(msg);
    6     }
    7 }
    1 package com.yas.ex;
    2 
    3 public class MyException3 extends Exception{
    4     public MyException3(String msg){
    5         super(msg);
    6     }
    7 }

    定义异常解析器:

     1 package com.yas.resolver;
     2 
     3 import com.yas.ex.MyException1;
     4 import com.yas.ex.MyException2;
     5 import com.yas.ex.MyException3;
     6 import org.springframework.web.servlet.HandlerExceptionResolver;
     7 import org.springframework.web.servlet.ModelAndView;
     8 
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 //异常解析器
    13 //任何一个hanlder中抛出异常时
    14 public class MyExeceptionResolver implements HandlerExceptionResolver {
    15     @Override
    16     public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    17         ModelAndView modelAndView = new ModelAndView();
    18         if(e instanceof MyException1){
    19             modelAndView.setViewName("error1");
    20         }else if(e instanceof MyException2){
    21             modelAndView.setViewName("error2");
    22         }else if (e instanceof MyException3){
    23             modelAndView.setViewName("error3");
    24         }
    25         return modelAndView;
    26     }
    27 }

    在mvc.xml文件中进行注册:

    <bean class="com.yas.resolver.MyExeceptionResolver"></bean>

    定义用于跳转的Controller类:

     1 package com.yas.controller;
     2 
     3 import com.yas.ex.MyException1;
     4 import com.yas.ex.MyException2;
     5 import com.yas.ex.MyException3;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 import org.springframework.web.bind.annotation.ResponseBody;
    10 
    11 @Controller
    12 public class ExceptionController {
    13 
    14     @RequestMapping("/error1")
    15     public String exception1(){
    16         return "error1";
    17     }
    18 
    19     @RequestMapping("/error2")
    20     public String exception2(){
    21         return "error2";
    22     }
    23 
    24     @RequestMapping("/error3")
    25     public String exception3(){
    26         return "error3";
    27     }
    28 
    29     @RequestMapping("/ex/test1")
    30     public String test1(@RequestParam("id") Integer id) throws Exception {
    31         if(id==1){
    32             throw new MyException1("异常1");
    33         }else if(id==2){
    34             throw new MyException2("异常2");
    35         }else if(id==3){
    36             throw new MyException3("异常3");
    37         }
    38         return "success";
    39     }
    40 }

    新建异常跳转的页面:

    error1.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    error1
    </body>
    </html>

    error2.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    error2
    </body>
    </html>

    error3.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    error3
    </body>
    </html>
  • 相关阅读:
    Asp.Net Core 进阶(一) —— 读取appsettings.json
    chrome控制台模拟hover、focus、active等状态,方便调试
    windows server 注意windows的temp目录
    (转)大公司里怎样开发和部署前端代码?
    排序算法——二分插入排序
    排序算法——归并排序
    排序算法——冒泡排序
    排序算法——插入排序
    排序算法——快速排序
    linux安装和配置 mysql、redis 过程中遇到的问题记录
  • 原文地址:https://www.cnblogs.com/asenyang/p/15467233.html
Copyright © 2020-2023  润新知