如果SpringMVC的action中发生异常,我们想将其跳转到一个固定的错误页面,可以通过applicationContext.xml中增加如下配置实现:

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">/pages/common/error.jsp</prop>
</props>
</property>
</bean>

例如,我的代码中有一个算数错误:

package org.lyk.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import org.lyk.vo.Emp;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping("/pages/emp/*")
public class EmpAction
{
@Resource
MessageSource messageSource; @RequestMapping("echo")
public void echo(String info,MultipartFile file1,MultipartFile file2) throws IOException
{
System.out.println("=======================");
System.out.println(info);
System.out.println(file1.getOriginalFilename());
System.out.println(file2.getOriginalFilename());
System.out.println(1/0);
System.out.println("=======================");
} @InitBinder
public void dataBinder(WebDataBinder webDataBinder)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
webDataBinder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
}
}

此时的action发生异常,会跳转到/pages/common/error.jsp页面上去。

SpringMVC中异常捕获的更多相关文章

  1. scala中异常捕获与处理简单使用

    import java.io.IOException /** * 异常捕获与处理 */ object excepitonUse { def main(args: Array[String]): Uni ...

  2. 关于java多线程中异常捕获的理解

    在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...

  3. Flask中异常捕获

    HTTP 异常主动抛出 abort 方法 抛出一个给定状态代码的 HTTPException 或者 指定响应,例如想要用一个页面未找到异常来终止请求,你可以调用 abort(404). 参数: cod ...

  4. c#中异常捕获,回滚

    语法: try { 有可能出现错误的代码写在这里 } catch { 出错后的处理 } 如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容, 如果try中的代码一但 ...

  5. java 异常捕获小记

    java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...

  6. SpringMVC中的拦截器、过滤器的区别、处理异常

    1. SpringMVC中的拦截器(Interceptor) 1.1. 作用 拦截器是运行在DispatcherServlet之后,在每个Controller之前的,且运行结果可以选择放行或拦截! 除 ...

  7. (转)SpringMVC学习(八)——SpringMVC中的异常处理器

    http://blog.csdn.net/yerenyuan_pku/article/details/72511891 SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常 ...

  8. Spring-MVC开发之全局异常捕获全面解读

    异常,异常 我们一定要捕获一切该死的异常,宁可错杀一千也不能放过一个! 产品上线后的异常更要命,一定要屏蔽错误内容,以免暴露敏感信息! 在用Spring MVC开发WEB应用时捕获全局异常的方法基本有 ...

  9. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

随机推荐

  1. NET项目发布到IIS上报错:HTTP 错误 403.14

    NET项目发布到IIS上报错:HTTP 错误 404.0 - Not Found 原因:由于本机开发环境是Net4.5,所以虽然创建项目时选择的是net framework4.5的,但是webconf ...

  2. Vue 进阶之路(十)

    之前的文章介绍了 vue 的组件化,本章我们来看一下 vue 中组件的原生事件. <!DOCTYPE html> <html lang="en"> < ...

  3. AJPFX告诉你MT4平台有什么优势?

    FX TERMINAL(Meta Trader4)交易平台功能 成功驾驭金融市场的第一步是拥有正确的工具.AJPFX为客户提供二十四小时的在线交易服务,MT4交易软件是目前全世界上最为先进,应用最为广 ...

  4. FFmpeg软硬解和多线程解码

    一. AVCodecContext解码上下文 1.avcodec_register_all() : 注册所有的解码器 2.AVCodec *avcodec_find_decoder(enum AVCo ...

  5. Bootstrap框架(一)

    day57 参考:https://www.cnblogs.com/liwenzhou/p/8214637.html 下载:http://www.bootcss.com/   选择用于生产环境的 Boo ...

  6. sql语句_2

    数据表如下 一个user_id对应多个user_name,现在要求是:如果某个用户对应的user_name中存在一个a,打印user_id,a出来:如果不存在,打印user_id,0.打印时候user ...

  7. spring boot 使用application.properties 进行外部配置

    application.properties大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成war包或者jar用于生产环境时,我们可以手动修改环 ...

  8. Ubuntu“无法打开锁文件(Could not get lock)”问题解决

    用apt-get安装软件时提示: 无法获得锁 /var/lib/dpkg/lock - open(11:资源暂时不可用) 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它? 其 ...

  9. POJ 1287

    #include<iostream> #include<stdio.h> #define MAXN 100 #define inf 1000000000 using names ...

  10. 添加或删除 HTML dom元素

    添加或删除 HTML dom元素 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. <div id=" ...