zoukankan      html  css  js  c++  java
  • SpringMVC(十)系统异常处理器

    系统异常处理器  SimpleMappingExceptionResolver

    案例:一个处理器方法中出项异常,想让他跳转到一个自定义的错误页面

    package demo12Exception;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by mycom on 2018/3/28.
     */
    @Controller
    public class SysExceptionController {
    
        @RequestMapping("/first")
        public String doFour(Model model){
            int num=5/0;
            return "success";
        }
    }

    在配置文件中需要一个异常处理器

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
    
        <context:component-scan base-package="demo12Exception"></context:component-scan>
    
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--异常处理器-->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="defaultErrorView" value="error"></property>
            <property name="exceptionAttribute" value="ex"></property>
        </bean>
        <!--注解驱动--><!--<mvc:annotation-driven/>-->
    
    
    
    </beans>

    错误页面

    <%--
      Created by IntelliJ IDEA.
      User: mycom
      Date: 2018/3/26
      Time: 11:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>出错了!!!!</h2>
    ${ex.message}
    </body>
    </html>
  • 相关阅读:
    去除网页图片缝隙
    只有定位的盒子有z-index
    clearfix
    2018CSS特效集锦牛逼
    c#spinLock使用
    redis 五种数据结构详解(string,list,set,zset,hash)
    C# StackExchange.Redis 用法总结
    Markdown 编辑器
    C# Task任务详解及其使用方式
    MongoDB学习笔记——MongoDB 连接配置
  • 原文地址:https://www.cnblogs.com/my-123/p/8671578.html
Copyright © 2011-2022 走看看