处理局部异常

  控制器:

@Controller
@RequestMapping("/ex")
public class ExceptionController { @ExceptionHandler
public ModelAndView exceptionHandler(Exception ex){
ModelAndView mv=new ModelAndView();
//保存异常变量
mv.addObject("ex",ex);
//添加错误页面
mv.setViewName("error");
System.out.println("出现了异常!");
return mv;
}
@RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
public String toIndex() throws Exception {
if (==){
throw new Exception("我是异常哈哈哈哈");
}
return "index";
}

页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--<c:forEach items="${error}" var="item">
<span>${item.defaultMessage}</span>
</c:forEach>--%>
${error.defaultMessage} </body>
</html>

  结果

处理全局异常

  ExceptionHandler类:

/*全局异常处理*/
@ControllerAdvice
public class ExceptionHandler { @org.springframework.web.bind.annotation.ExceptionHandler
public ModelAndView except(Exception e){
String message = e.getMessage(); //获取异常信息
ModelAndView mv=new ModelAndView("");
mv.addObject("message",message);
return mv;
}
}

  控制器:

@RequestMapping("/hello")       //设置处理器方法与用户请求的url之间的映射关系@WebServlet
public String toIndex() throws Exception {
if (==){
throw new Exception("我是异常哈哈哈哈");
}
return "index";
}

自定义异常

  spring-mvc文件配置:

<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"></property>
<property name="exceptionAttribute" value="ex"></property>
</bean>

  控制器:

@RequestMapping("/getex")
@ResponseBody
public String getex(){
int num=/;
return "index";
}

  结果:

自定义异常类

  spring-mvc文件配置:

<!--异常处理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"></property>
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<prop key="com.cmy.exception.UserName">UserName</prop>
<prop key="com.cmy.exception.Password">Password</prop>
</props>
</property>
</bean>

  控制器:

@RequestMapping("/getex01")
public String getex01(String userName,String password) throws Exception {
if (!userName.equals("admin")){
throw new Exception("用戶民不正確!!!");
}
if (!password.equals("admin")){
throw new Exception("密碼不正確!!!");
}
return "index";
}

  UserName.class异常类:

package com.cmy.exception;

public class UserName extends Exception {

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

  Password.class异常类:

package com.cmy.exception;

//自定义异常类
public class Password extends Exception { public Password() {
} public Password(String message) {
super(message);
}
}

  页面:

  结果:

自定义异常类实现(HandelExceptionResolver接口)

  spring-mvc.xml文件配置:

<bean class="com.cmy.exception.HandelException">

  HandelException.class异常类:

public class HandelException implements HandlerExceptionResolver {

    @Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv=new ModelAndView();
mv.addObject("ex",e);
mv.setViewName("error");
if (e instanceof UserName){
mv.setViewName("UserName");
}
if (e instanceof Password){
mv.setViewName("Password");
} return mv;
}
}

  控制器:

@RequestMapping("/getex01")
public String getex01(String userName,String password) throws Exception {
if (!userName.equals("admin")){
throw new Exception("用戶民不正確!!!");
}
if (!password.equals("admin")){
throw new Exception("密碼不正確!!!");
}
return "index";
}

MVC异常处理的更多相关文章

  1. ASP.NET MVC异常处理

    ASP.NET MVC异常处理方案 如何保留异常前填写表单的数据 ASP.NET MVC中的统一化自定义异常处理 MVC过滤器详解 MVC过滤器使用案例:统一处理异常顺道精简代码 ASP.NET MV ...

  2. 统一的mvc异常处理

    mvc异常处理 using System; using System.Configuration; using System.Web.Mvc; using Infrastructure.Excepti ...

  3. NET MVC异常处理模块

    一个简单的ASP.NET MVC异常处理模块   一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行. ...

  4. Spring MVC异常处理SimpleMappingExceptionResolver

    Spring MVC异常处理SimpleMappingExceptionResolver[转] (2012-12-07 13:45:33) 转载▼ 标签: 杂谈 分类: 技术分享 Spring3.0中 ...

  5. Spring MVC异常处理代码完整实例

    Spring MVC异常处理流程: 提供构造方法传值: 配置异常处理器的bean

  6. 一个简单的ASP.NET MVC异常处理模块

    一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行.异常处理应该是一个横切点,所谓横切点就是各个部分都 ...

  7. Spring MVC异常处理详解

    Spring MVC中异常处理的类体系结构 下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要 ...

  8. ASP.NET mvc异常处理的方法

    第一种:全局异常处理 1.首先常见保存异常的类(就是将异常信息写入到文件中去) public class LogManager { private string logFilePath = strin ...

  9. MVC 异常处理机制

    方法一 :web.config配置文件的 system.web 接点下添加,若为On则不会将异常信息反馈到用户,而是友好的跳转到error.htm <customErrors mode=&quo ...

  10. Spring MVC异常处理

    Spring Mvc 中异常处理,一般有两种解决办法: 一.利用org.springframework.web.servlet.handler.SimpleMappingExceptionResolv ...

随机推荐

  1. 易初大数据 2019年10月24日 spss笔记 王庆超

    数据文件的重置结构:横向结构(个案组),纵向结构,不符合分析方法的时候就需要重组,选定变量重组为个案,数据—重构,重构数据向导,选定变量重组为个案,将选定个案重构位变量,转置所有数据,变量组数目,一个 ...

  2. 关于swoole 定时器有时候无法清除的解决方法

    关于swoole 定时器有时候无法清除的解决方法 有时候start里面写个定时器 有时候你关闭进程的时候 发现定时器还是可以进行 目前只有重启服务器才可以 清除 还有就是ps -ef | grep p ...

  3. drf

    跨域同源 django做跨域同源 需要把csrf去掉 跨站请求伪造 同源 同源机制:域名.协议.端口号相同的同源 简单请求 不写头部请求 跨域会拦截报错缺少请求信息 (1) 请求方法是以下三种方法之一 ...

  4. 关于Jvm的见解(一)

    Jvm组成结构 硬件体系(如Intel体系.spac等)——>操作系统(如Windows.Linux等)——>Java Virtual Machine  所以虚拟机与硬件系统并没有直接的交 ...

  5. SqlServer2005 查询 第一讲 计算列

    数据库查询[最重要且在数据库中难度最大] 声明一下我这里用的数据库样例是郝斌老师的(scott库) 我尽最大努力把复杂的问题简单化,方便理解,希望我写的东西能够帮助到你们 有些复杂的东西我我用自己的方 ...

  6. supervisor服务

    描述: 遇到各种各样的各种坑, 可以通过python2 的pip安装, 可以通过apt安装, 不支持python3: 如若用apt安装可能会自动启动并且加入开机自启(不保证成功),pip安装一定不会需 ...

  7. 1142 CREATE VIEW command denied to user 'blog'@'XXX.XXX.XXX.XXX' for table 'Articles'

    创建视图时,报如上的1142错误,是数据库权限设置的问题. 进入mysql的root用户,赋予所有权限即可: mysql>grant all privileges on blogDB.* to ...

  8. c++中实现单例模式singleton class

    本文首发于个人博客https://kezunlin.me/post/8932eaec/,欢迎阅读! singleton class and usage in c++. Guide what singl ...

  9. tcp和udp的网络编程(发送消息及回复)

    一.UDP  无连接的  高效的  基于数据报的  不可靠 的连接 主要的应用场景: 需要资源少,网络情况稳定的内网,或者对于丢包不敏感的应用,比如 DHCP 就是基于 UDP 协议的.不需要一对一沟 ...

  10. Centos7 搭建LAMP环境(编译安装)

    1.查看系统版本 [niemx@localhost ~]$ cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 2.安装软件准备 ...