SpringMVC07SelfException 自定义异常

  1.配置web.xml文件

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

  2.1:用户异常类

public class UserInfoException extends Exception {
    public UserInfoException() {
    }
    public UserInfoException(String message) {
        super(message);
    }
}

  2.2:用户名异常

public class NameException extends UserInfoException {
    public NameException() {
    }
    public NameException(String message) {
        super(message);
    }
}

  2.3:年龄异常

public class AgeException extends UserInfoException {
    public AgeException() {
    }
    public AgeException(String message) {
        super(message);
    }
}

  3.异常解析器

import cn.happy.exceptions.AgeException;
import cn.happy.exceptions.NameException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//处理程序异常解析器
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    //解析异常
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/error.jsp");           //默认异常页面
        if (ex instanceof NameException) {
            mv.setViewName("/error/NameException.jsp");
        } else if (ex instanceof AgeException) {
            mv.setViewName("/error/AgeException.jsp");
        }
        return mv;
    }
}

  4.处理器

import cn.happy.exceptions.AgeException;
import cn.happy.exceptions.NameException;
import cn.happy.exceptions.UserInfoException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstController {
    @RequestMapping("/first")
    public String doFirst(String name, int age) throws UserInfoException {
        if (!"admin".equals(name)) {
            throw new NameException("用户名错误");
        } else if (age > 60) {
            throw new AgeException("年龄错误");
        }
        return "/index.jsp";
    }
}

  5.映射器 

<?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.happy"/> <!--注解驱动--> <mvc:annotation-driven/> <!--自定义异常处理器处理--> <bean class="cn.happy.hanlderexception.MyHandlerExceptionResolver"/> </beans>

  6.1:视图NameException.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>用户名错误</h2>
</body>
</html>

  6.2:视图AgeException.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>年龄错误</h2>
</body>
</html>

  6.3:视图login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/first" method="post">
    用户名:<input name="name"/>
    年龄:<input name="age"/>
    <input type="submit" value="登录"/>
</form>
</body>
</html>

  6.4:视图error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>ErrorPage出错了</h2>
${ex.localizedMessage}
</body>
</html>

  6.5:视图index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

   

  测试结果:正常登录

  测试结果:测试用户名异常

  测试结果:测试年龄异常

  测试结果:测试直接在网址进入页面

原文地址:https://www.cnblogs.com/Chenghao-He/p/7788126.html