zoukankan      html  css  js  c++  java
  • SSM-SpringMVC-23:SpringMVC中初探异常解析器

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

    本篇博客要讲的是异常解析器,SimpleMappingExceptionResolver简单映射异常解析器

    可以处理系统的异常

    如果出现系统异常,跳转到某个页面的时候,就可以使用它

    案例如下:

      一,创建俩个jsp页面:

        success.jsp(处理方法想要跳转的页面,但是抱歉,中途插入了异常)

    <%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
    <html>
    <body>
    <%--<img src="image/1.jpg">--%>
    <h2>Success!</h2>
    </body>
    </html>

        error.jsp(一会出现异常跳转的页面)

    <%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h2>ERROR</h2>
    <p>${ex.message}</p>
    服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
    </body>
    </html>

      二,准备处理器和处理方法(我在里面模拟了个算数异常,除零)

    package cn.dawn.day15exception;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by Dawn on 2018/3/28.
     */
    @Controller
    public class SysExceptionController {
        @RequestMapping("/sysExceptionbyzero")
        public String sysExceptionbyzero(){
            int a=5/0;
            return "success";
        }
    }

      三,在自己定义的xml中配置一道(SImpleMappingExceptionResolver)

    <?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="cn.dawn.day15exception"></context:component-scan>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/day15/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <!--默认出现异常跳转的页面-->
            <property name="defaultErrorView" value="error"></property>
            <!--这个可以注入异常对象,就像catch参数里的(Exception ex)一样-->
            <property name="exceptionAttribute" value="ex"></property>
        </bean>
    
    </beans>

      四,web.xml中央调度器修改上下文配置位置为上面那个xml的位置

      五,启动tomcat,拉出来跑一下

  • 相关阅读:
    mysql_pw 指令 数据库创建过程
    node.js+mysql环境搭建
    MySQL 学习
    express 应用创建及app.js详解
    .NET MD5加密解密代码
    Axure 部件的交互样式
    easyUI -messager -消息框
    Window01
    linkbutton
    easyUi-datagrid 真分页 + 工具栏添加控件
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8672268.html
Copyright © 2011-2022 走看看