• springmvc中的异常处理方法


    //1、自定义异常处理类       2、编写异常处理器    3、配置异常处理器

    package com.hope.exception;

    /**
    * 异常处理类
    * @author newcityman
    * @date 2019/11/28 - 23:10
    */
    public class SysExecepiton extends Exception {
    private String message;

    @Override
    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public SysExecepiton(String message) {
    this.message = message;
    }
    }


    package com.hope.exception;

    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;

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


    /**
    * 异常处理器
    *
    * @author newcityman
    * @date 2019/11/28 - 23:21
    */
    public class SysExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
    SysExecepiton e = null;
    if (ex instanceof SysExecepiton) {
    e = (SysExecepiton) ex;
    } else {
    e = new SysExecepiton("系统维护中.......");
    }
    ModelAndView mv = new ModelAndView();
    mv.addObject("errorMsg", e.getMessage());
    mv.setViewName("error");
    return mv;
    }
    }


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!--指定spring扫描的包-->
    <context:component-scan base-package="com.hope"></context:component-scan>
    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 配置文件解析器对象,处理文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760"></property>
    </bean>
    <!-- 指定前端控制器哪些资源不拦截-->
    <!--前端控制器,哪些静态资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!--配置异常处理器-->
    <bean id="sysExceptionResolver" class="com.hope.exception.SysExceptionResolver"></bean>
    <!--开启springmvc框架注解的支持,包含开启处理器映射器和处理器适配器-->
    <mvc:annotation-driven />
    </beans>

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    ${errorMsg}
    </body>
    </html>



  • 相关阅读:
    等保测评(一)
    一个基于RNN的实体抽取工具
    如何画UML类图
    mysql存储过程整理
    记一次mysql事务未提交导致锁未释放的问题
    开启·元宇宙·区块链金融
    Nacos启动报错:[db-load-error]load jdbc.properties error
    使用Bazel编译TypeScript
    Win10上Docker无法正常启动 出现install WSL2 kernel update的情况
    VSCode调试PHP环境配置 phpstudy vscode xdebug调试
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11954902.html
Copyright © 2020-2023  润新知