SpringMVC中异常捕获
如果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中异常捕获的更多相关文章
- scala中异常捕获与处理简单使用
import java.io.IOException /** * 异常捕获与处理 */ object excepitonUse { def main(args: Array[String]): Uni ...
- 关于java多线程中异常捕获的理解
在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...
- Flask中异常捕获
HTTP 异常主动抛出 abort 方法 抛出一个给定状态代码的 HTTPException 或者 指定响应,例如想要用一个页面未找到异常来终止请求,你可以调用 abort(404). 参数: cod ...
- c#中异常捕获,回滚
语法: try { 有可能出现错误的代码写在这里 } catch { 出错后的处理 } 如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容, 如果try中的代码一但 ...
- java 异常捕获小记
java 中异常捕获常用的为: try{ //业务代码 }catch(Exception e){ //异常捕获 }finally{ // 不管有无异常, 最后都会执行到这里 } 在方法体内如果想要把异 ...
- SpringMVC中的拦截器、过滤器的区别、处理异常
1. SpringMVC中的拦截器(Interceptor) 1.1. 作用 拦截器是运行在DispatcherServlet之后,在每个Controller之前的,且运行结果可以选择放行或拦截! 除 ...
- (转)SpringMVC学习(八)——SpringMVC中的异常处理器
http://blog.csdn.net/yerenyuan_pku/article/details/72511891 SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常 ...
- Spring-MVC开发之全局异常捕获全面解读
异常,异常 我们一定要捕获一切该死的异常,宁可错杀一千也不能放过一个! 产品上线后的异常更要命,一定要屏蔽错误内容,以免暴露敏感信息! 在用Spring MVC开发WEB应用时捕获全局异常的方法基本有 ...
- 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题
问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...
随机推荐
- Asp.Net MVC EF之一:使用Database类在EF框架中执行Sql语句
引言 从EF6开始,增加了DateBase类,他通过从 DbContext 对象获取此类的实例.可用于管理支持数据库上下文或连接的实际数据库.这包括创建.删除和检查数据库的存在. 在6以前,我们使用E ...
- Http请求基本方法
1.Http请求基本方法 /// <summary> /// Http请求基本方法 /// </summary> /// <param name="conten ...
- ip addr 相关操作
1.添加ip: ip addr add 1.1.1.100/255.255.255.0 dev eth0 2.删除ip: ip addr del 1.1.1.100/255.255.255.0 dev ...
- Mysql数据库一:安装与创建windows服务
Mysql数据库安装与创建windows服务 1.先下载压缩包(mysql-5.7.18-winx64.zip)移动到对应目录(如D:\software)后解压. 2.安装服务端: mysqld:带d ...
- jzoj4724
DJL为了避免成为一只咸鱼,来找czgj学习Fibonacci数列. 通过czgj的谆谆教导,DJL明白了Fibonacci数列是这样定义的: F(1)=1;F(2)=1;F(n)=F(n-1)+F( ...
- [AIR] AIR程序调用本地默认应用程序打开本地文件
摘要: File类提供了一个方法openWithDefaultApplication可以用本地默认应用程序打开指定路径下的文件. 当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: v ...
- Python-WSGI协议,mini-web框架
本次带给大家的是WSGI-mini-web框架, 其中要下载一些网络页面, 大佬们不要见怪. 我所做的mini-web 支持路由, 正则表达式, 添加了log日志功能:解析了url编码可以用 来理解W ...
- QuantLib 金融计算——基本组件之 Currency 类
目录 QuantLib 金融计算--基本组件之 Currency 类 概述 构造函数 成员函数 如果未做特别说明,文中的程序都是 python3 代码. QuantLib 金融计算--基本组件之 Cu ...
- MySQL命令行导入sql文件时出现乱码解决方案
Note: sql> source F:weibo.sql(执行相关sql文件) sql> select * from sina into outfile "/weibo.txt ...
- Vue路由-命名视图实现经典布局
Vue路由-命名视图实现经典布局 相关Html: <!DOCTYPE html> <html lang="en"> <head> <met ...