发表于: 2019-05-28 19:49:10

1 602


今天完成的事情:

    完成了接口类的大致编写。



目前未完成的还有:

1.逻辑判空;

2.全局和事物具体异常抛出

3.shiro权限管理(可选)




2.在springmvc上配置全局异常(配置文件的方式)

在spring-servlet.xml(springmvc的配置文件)中配置

 <!-- Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver-全局异常配置 start -->     

      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

       <!--   默认的错误信息页面 -->

        <property name="defaultErrorView" value="error"/>

        <property name="exceptionMappings">

            <props>

                <prop key="java.lang.NullPointerException">commons/error/error_spring_null</prop>

                <prop key="java.lang.ArithmeticException">commons/error/error_spring_math</prop>

            </props>

        </property>

    </bean>   

     <!-- 全局异常配置 end -->     


TestException .java 异常测试类

@Controller

@RequestMapping(value="/exception")

public class TestException {

@RequestMapping("/exception/{id}")

    @ResponseBody

    public Object hello(@PathVariable String id)  {

        if (id.equals("1")) {//NullPointerException控制值异常

            throw new NullPointerException("空指针异常");

        } else if (id.equals("2")) {//数学运算异常

            int value = 1 / 0;

            return "java.lang.ArithmeticException";

        } else {

            return "ok";

        }

    }


代码分析:

http://127.0.0.1:8080/maven-springmvc-spring-springjdbc/exception/exception/1

输入1到浏览器,程序出现空指针异常;

http://127.0.0.1:8080/maven-springmvc-spring-springjdbc/exception/exception/2

输入2到浏览器,程序出现空指针异常

http://127.0.0.1:8080/maven-springmvc-spring-springjdbc/exception/exception/3

输入其它到浏览器,程序正常运行

错误展示页面和页面对应错误信息

error.jsp :默认异常处理页面

error_spring_404.jsp :对应404错误

error_spring_null.jsp :对应空指针异常

1.输入http://127.0.0.1:8080/maven-springmvc-spring-springjdbc/exception/exception/1

结果分析:spring-servlet.xml中接收到空指针异常,指向error_spring_null.jsp页面

2.输入http://127.0.0.1:8080/maven-springmvc-spring-springjdbc/exception/exception/2

结果分析:spring-servlet.xml中接收到数学算术运算异常,指向error_spring_math.jsp页面

总结点

如果同时使用web.xml和spring-servlet.xml配置同样的异常,会出现什么结果,web.xml 和spring-servlet.xml配置会不会冲突?

这涉及到web.xml中元素的加载顺序

元素节点的加载顺序与它们在 web.xml 文件中的先后顺序无关,通常是

ServletContext -> listener -> filter -> servlet---->------>error-page

web.xml中部分代码段

<!-- springmvc -->

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 自定义spring mvc的配置文件名称和路径 -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springMVC/spring-servlet.xml</param-value>

</init-param>

<!-- 自定义启动顺序,让这个Servlet随Servlet容器一起启动 -->

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- 异常处理 -->

<!-- 接收空指针错误 -->

<error-page>

<exception-type>java.lang.NullPointerException</exception-type>

<location>/WEB-INF/views/commons/error/error_web_null.jsp</location>

</error-page>

<!-- 404 页面不存在错误 -->

<error-page>

<error-code>404</error-code>

<location>/WEB-INF/views/commons/error/error_web_404.jsp</location>

</error-page>

<!-- 接收405错误 -->

<error-page>

<error-code>405</error-code>

<location>/WEB-INF/views/commons/error/error405.jsp</location>

</error-page>

<!--  500 服务器内部错误 -->

<error-page>

<error-code>500</error-code>

<location>/WEB-INF/views/commons/error/error500.jsp</location>

</error-page>


结果显而易见,spring-servlet.xml的加载顺序比error-page的快,异常会当有相同配置的错误时,会优先spring-servlet.xml中配置。只有当spring-servlet.xml没有处理的错误异常,才会在web.xml的error-page节点中查找对应异常。

spring-servlet.xml采用配置文件的方式的缺点:配置文件可以根据抛出的异常类的类信息配置错误的页面。但是这样我们无法手动对其定制异常。

故出现第三种方式

3.第三种方式采用自定义异常类,定制异常集中处理

ExceptionUtil .java 自定义的异常处理类

//自己手动编写Java代码来实现定制异常信息处理

package com.util.exception;

/**

 * 所属类别:工具类

 * 用途:自定义的异常类

 * @author yilei

 * version:1.0

 */

public class ExceptionUtil extends RuntimeException {

public ExceptionUtil(String string) {

super(string);

}

}


在spring-servlet.xml ,相比于第二种方法多了com.util.exception.ExceptionUtil,可以配置自定义异常处理类进行接收异常

     <!-- Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver-全局异常配置 start -->     

      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

       <!--  默认的错误信息页面-->

        <property name="defaultErrorView" value=">commons/error/error"/>

        <property name="exceptionMappings">

            <props>

                <!-- 自定义的异常类 -->

                <prop key="com.util.exception.ExceptionUtil">commons/error/error_spring_ExceptionUtil</prop> 

                <!-- 空指针异常 -->

                <prop key="java.lang.NullPointerException">commons/error/error_spring_null</prop>

                <!-- 数学算术异常 -->

                <prop key="java.lang.ArithmeticException">commons/error/error_spring_math</prop>

            </props>

        </property>

    </bean>    

     <!-- 全局异常配置 end -->   


error_spring_ExceptionUtil.jsp 自定义异常处理jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

error_spring_ExceptionUtil.jsp<br>

spirngmvc配置异常-自定义异常

</body>

</html>


TestException.java 进行异常测试的类,throw new ExceptionUtil(“自定义有异常”);这段代码用于抛出自定义异常类,springmvc中配置自动跳转到commons/error/error_spring_ExceptionUtil页面

package com.controller.test.exception;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.util.exception.ExceptionUtil;

@Controller

@RequestMapping(value = "/exception")

public class TestException   {

@RequestMapping("/exception/{id}")

@ResponseBody

public Object hello(@PathVariable String id) {

        if(id.equals("0")){

        throw new ExceptionUtil("自定义有异常");

        }else if (id.equals("1")) {// NullPointerException控制值异常

throw new NullPointerException("空指针异常");

} else if (id.equals("2")) {// 数学运算异常

int value = 1 / 0;

return "java.lang.ArithmeticException";

} else {

return "ok";

}

}

}

通过博客学习了

https://blog.csdn.net/qq_29914837/article/details/82697089

上的异常处理方式。


明天计划的事情:

   1.完成逻辑判空
遇到的问题:

    有么有什么拦截器,拦截所有传入值,不允许为空 !
收获:

    熟悉了接口的编写。


返回列表 返回列表
评论

    分享到