如果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程序中http请求的超时配置

    请求时的超时 // // 摘要: // 获取或设置 System.Net.HttpWebRequest.GetResponse() 和 System.Net.HttpWebRequest.GetReq ...

  2. sqlServer数据库常用连接字符串

    sqlServer   数据库常用连接字符串 用户名和密码验证的方式去连接到数据库服务器 <add name="conStr" connectionString=" ...

  3. AJPFX的资金安全性

    AJPFX承诺保证客户资金安全,并严格按照英国的相关规章制度从事经营活动.客户资金存放于投资级银行的独立账户中.通过实行公司资产与客户资金分别保管,在发生无偿债能力的罕见情况下,客户可获退还独立存放资 ...

  4. SecurityManager入门

    java安全管理器SecurityManager入门 SecurityManager 每个Java应用都可以有自己的安全管理器,它是防范恶意攻击的主要安全卫士. 安全管理器通过执行运行阶段检查和访问授 ...

  5. Python小白学习之路(十九)—【文件操作步骤】【文件操作模式】

    一.文件操作步骤 step1:打开文件,得到文件句柄并赋值给一个变量step2:通过句柄对文件进行操作step3:关闭文件 举例: a = open('hello world', 'r', encod ...

  6. 屏蔽百度及其广告的部分Host

    127.0.0.44 www.baidu.com127.0.0.44 cpro.baidu.com127.0.0.44 hm.baidu.com127.0.0.44 bdimg.share.baidu ...

  7. VSTO学习(二)——Excel对象模型

    要开发Excel的项目,就自然少不了对Excel对象模型的了解了,只有了解Excel对象模型,这样才能更好地对Excel进行处理.下面先给出一张Excel对象模型的图: 下面就具体对上图中的各个对象做 ...

  8. (转)教你手工mysql拆库

    原文:http://www.cnblogs.com/cchust/p/3859967.html 互联网网站应用大多采用mysql作为DB存储,限于mysql单机性能的瓶颈,为了支撑更大容量和更大的访问 ...

  9. C++ STL 初探

    学过C++的人肯定会很熟悉STL标准模板库,STL其实就是封装了一系列的接口,供我们调用.很多函数或者算法的实现不需要我们从头开始写,大大提高我们的编程效率.这篇博客在简单介绍STL的情况下,会详细的 ...

  10. spring boot日志管理配置

    spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,Log4J2和Logback.每种L ...