1. 程式人生 > >SpringMVC(十二)自定義異常處理器 HandlerExceptionResolver(接口)

SpringMVC(十二)自定義異常處理器 HandlerExceptionResolver(接口)

pin org ota admin pack property framework ase exception

自定義異常處理器和系統異常處理器的提升版可以實現相同的功能,但是使用的方法不同,自定義異常處理器可以不用在配置文件中配置name多東西,只需要一個異常處理器就可以,有需要的話也可以配置一個視圖解析器,但是包掃描器是必須的

先定義一個類讓他實現 HandlerExceptionResolver 接口

package demo14SelfException;

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

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

/** * Created by mycom on 2018/3/30. */ public class MyHandlerExceptionResolver implements HandlerExceptionResolver { /** * 解析異常 * @param httpServletRequest * @param httpServletResponse * @param o * @param ex * @return */ public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) { ModelAndView mv
=new ModelAndView(); mv.addObject("ex",ex);//保存的數據,在頁面上用EL表達式展示錯誤信息 //如果不滿足下面兩個條件,就走這個默認的頁面 mv.setViewName("error"); //判斷異常類型 是用戶名異常就到用戶名異常類中是年齡異常就到年齡異常類中 if(ex instanceof NameException){ //指定特定的異常頁面 mv.setViewName("nameException"); }
if(ex instanceof AgeException){ mv.setViewName("ageException"); } //最後返回ModelAndView return mv; } }

在這個類中需要兩個異常類NameException和AgeException

package demo14SelfException;

/**
 * Created by mycom on 2018/3/30.
 */
public class NameException extends Exception {
    public NameException() {
        super();
    }

    public NameException(String message) {
        super(message);
    }
}
package demo14SelfException;

/**
 * Created by mycom on 2018/3/30.
 */
public class AgeException extends Exception {
    public AgeException() {
        super();
    }

    public AgeException(String message) {
        super(message);
    }
}

在異常控制器中

package demo14SelfException;

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

/**
 * Created by mycom on 2018/3/30.
 */
@Controller
public class ExceptionController {

    @RequestMapping("/first")
    public String doFirst(String name,int age) throws Exception {
        //根據異常的不同返回不同的頁面
        if(!name.equals("admin")){
            //如果用戶名輸入不正確,走姓名異常這個類
            throw new NameException("用戶名異常");
        }
        if(age>60){
            throw new AgeException("年齡不符合");
        }
        //最後成功的話就走成功的頁面
        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="demo14SelfException"></context:component-scan>

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/error/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--異常處理器  自己定義的類-->
    <bean class="demo14SelfException.MyHandlerExceptionResolver"></bean>



</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>
<form action="${pageContext.request.contextPath}/first" method="post">
    用戶名:<input type="text" name="name" value="${name}"/>
    年齡:<input type="text" name="age" value="${age}"/>
    出生日期:<input type="text" name="birthday"/>
    <input type="submit" value="提交">
</form>
</body>
</html>

普通錯誤頁面

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

成功頁面

<%--
  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>
  ${username}登錄成功!
</body>
</html>

姓名錯誤

<%--
  Created by IntelliJ IDEA.
  User: mycom
  Date: 2018/3/30
  Time: 12:35
  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>
姓名不符合 ${ex.message}
</body>
</html>

年齡錯誤

<%--
  Created by IntelliJ IDEA.
  User: mycom
  Date: 2018/3/30
  Time: 12:35
  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>
年齡不符合 ${ex.message}
</body>
</html>

SpringMVC(十二)自定義異常處理器 HandlerExceptionResolver(接口)