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>

  

  测试结果:正常登录

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

  测试结果:测试年龄异常

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

SpringMVC07SelfException 自定义异常的更多相关文章

  1. Java异常之自定义异常

    哎呀,妈呀,又出异常了!俗话说:"代码虐我千百遍,我待代码如初恋". 小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常.Java异 ...

  2. JAVA自定义异常

    创建自定义异常是为了表示应用程序的一些错误类型,为代码可能发生的一个或多个问题提供新含义. 可区分代码运行时可能出现的相似问题的一个或多个错误,或给出应用程序中一组错误的特定含义. //自定义异常类需 ...

  3. Web API应用架构在Winform混合框架中的应用(2)--自定义异常结果的处理

    在上篇随笔<Web API应用架构在Winform混合框架中的应用(1)>中我介绍了关于如何在Winfrom里面整合WebAPI,作为一个新型数据源的接入方式,从而形成了三种不同的数据提供 ...

  4. SQL Server 抛出自定义异常,由C#程序俘获之并进行相应的处理

    最近一直在找可以自定义异常,并用C#程序捕获并进行相应的处理,试了很多方法都没有成功.今天终于找到了不错的方法.所以转载并分享给大家. 摘自:http://www.cnblogs.com/scottc ...

  5. Java自定义异常类

    用户可以根据自己的需要定义自己的异常类,定义异常类只需要继承Exception类即可 //================================================= // Fi ...

  6. Android Java 自定义异常

    1.自定义异常 package com; public class ZeroException extends Exception { private static final long serial ...

  7. PHP 错误与异常 笔记与总结(16 )自定义异常处理器

    可以使用自定义异常处理器来处理所有未捕获的异常(没有用 try/catch 捕获的异常). set_exception_handler():设置一个用户定义的异常处理函数,当一个未捕获的异常发生时所调 ...

  8. PHP 错误与异常 笔记与总结(13 )自定义异常类

    针对不同的异常,进行不同的处理,可以通过自定义异常类记录特定的异常信息来处理不同类型的异常.自定义异常类通过继承基类(Exception),对基类进行扩展. 自定义异常类 <?php heade ...

  9. [转]如何编写和应用Java的自定义异常类

    编写自定义异常类实际上是继承一个API标准异常类,用新定义的异常处理信息覆盖原有信息的过程.常用的编写自定义异常类的模式如下:   public class CustomException exten ...

随机推荐

  1. BluetoothFindNextRadio 函数

    BOOL BluetoothFindNextRadio( HBLUETOOTH_RADIO_FIND hFind, HANDLE* phRadio ); BluetoothFindNextRadio找 ...

  2. Java探索之旅(2)——GUI输入输出与代码的规范性

    1.知识点概叙 ① 定名常量:关键字final,类似C++ const定义,一般用大写:final double PI=3.1415926 ② 5/2=2:5.0/2=2.5://通常意义的除法,至少 ...

  3. C/C++常用数学函数

    math.h/cmath(C++)数学函数库 1 三角函数    double sin (double);    double cos (double);    double tan (double) ...

  4. hdu1057

    #include <iostream> #include <string> #include <cstring> using namespace std; int ...

  5. C#----接口与多继承

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...

  6. <base target="_self"/>标签的用法

    语法 <base target="value"> 属性值 值 描述 _blank 在新窗口中打开被链接文档. _self 默认.在相同的框架中打开被链接文档. _par ...

  7. 使用 jquery.webcam 进行asp.net 拍照

    HTML 代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index. ...

  8. Note: Eleos: ExitLess OS Services for SGX Enclaves

    Eleos increased I/O and memory intensive SGX program execution performance with In-enclave system ca ...

  9. OpenStack基础知识-virtualenv工具详解

    1.virtualenv介绍 virtualenv通过创建一个单独的虚拟化python运行环境,将我们所需的依赖安装进去,不同项目之间相互不干扰,从而解决不同的项目之间依赖不同,造成的冲突问题 2.安 ...

  10. node -- 安装及快速开始

    下载并安装 node下载地址:https://nodejs.org/en/download/ 安装就绪后,打开命令行,操作如下: shift+右键/Win+r->cmd 检测是否安装成功: no ...