• MVC异常处理


    处理局部异常

      控制器:

    @Controller
    @RequestMapping("/ex")
    public class ExceptionController {
    
        @ExceptionHandler
        public ModelAndView exceptionHandler(Exception ex){
            ModelAndView mv=new ModelAndView();
            //保存异常变量
            mv.addObject("ex",ex);
            //添加错误页面
            mv.setViewName("error");
            System.out.println("出现了异常!");
            return mv;
        }
    View Code
    @RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
    public String toIndex() throws Exception {
        if (1==1){
            throw new Exception("我是异常哈哈哈哈");
        }
        return "index";
    }
    View Code

    页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%--<c:forEach items="${error}" var="item">
        <span>${item.defaultMessage}</span>
    </c:forEach>--%>
    ${error.defaultMessage}
    
    </body>
    </html>
    View Code

      结果

    处理全局异常

      ExceptionHandler类:

    /*全局异常处理*/
    @ControllerAdvice
    public class ExceptionHandler {
    
        @org.springframework.web.bind.annotation.ExceptionHandler
        public ModelAndView except(Exception e){
            String message = e.getMessage();    //获取异常信息
            ModelAndView mv=new ModelAndView("404");
            mv.addObject("message",message);
            return mv;
        }
    }
    View Code

      控制器:

    @RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
    public String toIndex() throws Exception {
        if (1==1){
            throw new Exception("我是异常哈哈哈哈");
        }
        return "index";
    }
    View Code

    自定义异常

      spring-mvc文件配置:

    <!--异常处理-->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="error"></property>
        <property name="exceptionAttribute" value="ex"></property>
    </bean>
    View Code

      控制器:

    @RequestMapping("/getex")
    @ResponseBody
    public String getex(){
        int num=5/0;
        return "index";
    }
    View Code

      结果:

    自定义异常类

      spring-mvc文件配置:

    <!--异常处理-->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="error"></property>
        <property name="exceptionAttribute" value="ex"></property>
        <property name="exceptionMappings">
            <props>
                <prop key="com.cmy.exception.UserName">UserName</prop>
                <prop key="com.cmy.exception.Password">Password</prop>
            </props>
        </property>
    </bean>
    View Code

      控制器:

    @RequestMapping("/getex01")
    public String getex01(String userName,String password) throws Exception {
        if (!userName.equals("admin")){
            throw new Exception("用戶民不正確!!!");
        }
        if (!password.equals("admin")){
            throw  new Exception("密碼不正確!!!");
        }
        return "index";
    }
    View Code

      UserName.class异常类:

    package com.cmy.exception;
    
    public class UserName extends Exception {
    
        public UserName() {
        }
    
        public UserName(String message) {
            super(message);
        }
    }
    View Code

      Password.class异常类:

    package com.cmy.exception;
    
    //自定义异常类
    public class Password extends Exception {
    
        public Password() {
        }
    
        public Password(String message) {
            super(message);
        }
    }
    View Code

      页面:

      结果:

    自定义异常类实现(HandelExceptionResolver接口)

      spring-mvc.xml文件配置:

    <bean class="com.cmy.exception.HandelException">
    View Code

      HandelException.class异常类:

    public class HandelException implements HandlerExceptionResolver {
    
        @Override
        public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
            ModelAndView mv=new ModelAndView();
            mv.addObject("ex",e);
            mv.setViewName("error");
            if (e instanceof  UserName){
                mv.setViewName("UserName");
            }
            if (e instanceof  Password){
                mv.setViewName("Password");
            }
    
            return mv;
        }
    }
    View Code

      控制器:

    @RequestMapping("/getex01")
    public String getex01(String userName,String password) throws Exception {
        if (!userName.equals("admin")){
            throw new Exception("用戶民不正確!!!");
        }
        if (!password.equals("admin")){
            throw  new Exception("密碼不正確!!!");
        }
        return "index";
    }
    View Code
  • 相关阅读:
    java-Date、String、Calendar转化
    java -日期
    eclipse安装ADT
    Echarts-axislabel文字过长导致显示不全或重叠
    Echarts-柱状图柱图宽度设置
    Echarts-画叠加柱状图,双折线图
    Echarts-画堆积柱状图,折线图
    Echarts-画柱状,折线图
    windows系统激活-使用微软官方公布的kms client setup key安装或安装后使用slmgr导入
    Office 2013 Pro Plus Vol激活
  • 原文地址:https://www.cnblogs.com/wnwn/p/11834295.html
Copyright © 2020-2023  润新知