• 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>



  • 相关阅读:
    电信生命周期说明
    find in linux 2 微信公众号
    GDB中应该知道的几个调试方法 2 微信公众号
    linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: rg), 且有vim插件的 2 微信公众号
    linux下的 c 和 c++ 开发工具及linux内核开发工具 2 微信公众号
    linux下命令行发送邮件的软件:mutt 微信公众号
    腺样体肿大的综合治疗考虑 微信公众号
    打呼噜治疗方法 微信公众号
    vim 操作 2 微信公众号
    nginx之外的web 服务器caddy 微信公众号
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11954902.html
Copyright © 2020-2023  润新知