SpringMVC(十二)自定义异常处理器 HandlerExceptionResolver(接口)

自定义异常处理器和系统异常处理器的提升版可以实现相同的功能,但是使用的方法不同,自定义异常处理器可以不用在配置文件中配置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";
    }
}

配置文件中

"1.0" encoding="UTF-8"?>
"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">

    
    base-package="demo14SelfException">

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        "prefix" value="/error/">
        "suffix" value=".jsp">
    

    
    class="demo14SelfException.MyHandlerExceptionResolver">



需要的页面

登录页面

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


    Title


登录

"${pageContext.request.contextPath}/first" method="post"> 用户名:"text" name="name" value="${name}"/> 年龄:"text" name="age" value="${age}"/> 出生日期:"text" name="birthday"/> "submit" value="提交">

普通错误页面

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


    Title


出错了!!!!

${ex.message}

成功页面

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


    Title


  ${username}登录成功!

姓名错误

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


    Title


姓名不符合 ${ex.message}

年龄错误

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


    Title


年龄不符合 ${ex.message}

 

转载于:https://www.cnblogs.com/my-123/p/8681132.html

你可能感兴趣的:(java,测试,开发工具)