spring mvc 和mybatis整合 的异常处理
1.自定义异常信息类 通过构造函数来实现异常信息的接收
public class CustomException extends Exception {
//异常信息
	private String message;
public CustomException (String message){
		super(message);
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
public void setMessage(String message) {
		this.message = message;
	}
}
2.通过实现HandlerExceptionResolver的接口来实现异常处理 流程:先是解析异常,再判断是否是系统自定义异常,如果是就直接抛出异常,如果不是自定义异常就直接构造一个自定义的异常类型(信息为“未知错误,请与管理员联系!”)
//不是自定义饿异常多半是运行异常,尽量在测试的时候就解决掉
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
CustomException customException=null;
		if(ex instanceof CustomException ){
			customException = (CustomException)ex;
		}else{
			customException = new CustomException("未知错误,请与管理员联系!");
		}
		//获取错误信息
		String  message = customException.getMessage();
		System.out.println("异常信息:"+message);
		//创建ModelAndView对象
		ModelAndView modelAndView = new ModelAndView();
		//把错误信息填充到request域中
		modelAndView.addObject("message", message);
		//传入到页面	
		modelAndView.setViewName("error");
		return modelAndView;
	}
}
3.在spring 的xml文件中配置 class 是CustomExceptionResolver的路径
<!-- 异常处理器 -->
		<bean class="com.menglin.ssm.exception.CustomExceptionResolver"></bean>
4.开始测试 (需求:当在查询的时候如果信息不存在的时候就抛出异常 )
/**
	 * 根据id来查询
	 */
	@Override
	public ItemsCustom findItemsCustomById(Integer id) throws Exception {
		ItemsCustom itemsCustom = null;
		Items items = itemsMapper.selectByPrimaryKey(id);
		if(items==null){
throw new CustomException("商品信息不存在!");
		}else{
			itemsCustom = new ItemsCustom();
			BeanUtils.copyProperties(items, itemsCustom);		
		}
		return itemsCustom;
}
5.错误信息的展示
8{U}E82ACDGS(Z}%~C.png)


spring mvc 和mybatis整合 的异常处理的更多相关文章
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
		
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
 - Spring MVC、MyBatis整合文件配置详解
		
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
 - Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)
		
Spring.Spring MVC.MyBatis整合(未万待续)
 - Spring、Spring MVC、MyBatis整合文件配置详解
		
原文 http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...
 - 【转】Spring、Spring MVC、MyBatis整合文件配置详解
		
见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...
 - Spring、Spring MVC、MyBatis整合文件配置详解2
		
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
 - Spring、Spring MVC、MyBatis 整合文件配置详解
		
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
 - Spring MVC + Spriing + MyBatis整合,写给新人
		
开发环境: 开发工具:MyEclipse 8.6 数据库:MySQL 操作系统:WIN8.1 Jar包: Spirng和SpringMVC版本:3.2.9 MyBatis版本:3.2.8 其他关联Ja ...
 - spring mvc与mybatis整合错误提示
		
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...
 
随机推荐
- jq问题处理
			
1.同一个事件,点击显示和隐藏 $(document).ready(function(){ $('.container .nav-header').click(function(){ var chan ...
 - Eclipse + Pydev 配置Python开发环境
			
之前有人问 Eclipse + Pydev 的环境配置问题,在此记录一下. 前提条件:需要安装 Eclipse .jdk.Python.这三个软件都可以在官网上下载. 假设上述软件已经安装好了,下面是 ...
 - linux jmail乱码
			
在Windows 里面,标题和内容都正常, 在linux里面,--- 1.字符串 more /etc/sysconfig/i18n export LANG=en_US.UTF-8 ====默认是e ...
 - [置顶] android LBS的研究与分享(附PPT)
			
早上起床后想想以前的工作有什么可以分享的... 两年前,前公司在做一个陌生人交友的社交软件(比陌陌还早),需要研究LBS的相关工作(这里面主要研究的是Google定位)... 一.简要调研结果 很多 ...
 - [React Native] Up and Running
			
We'll download the requirements for getting started with React Native, refactor our app to ES6, walk ...
 - Poco库网络模块例子解析1-------字典查询
			
Poco的网络模块在Poco::Net名字空间下定义 下面是字典例子解析 #include "Poco/Net/StreamSocket.h" //流式套接字 #include ...
 - 第一章建立asp.net MVC
			
第一步 第二步 创建controller 创建View view和controller之间的关系
 - 深入理解计算机系统第二版习题解答CSAPP 2.7
			
下面的函数将输出什么结果? const char *s = "abcdef"; show_bytes((byte_pointer) s, strlen(s)); 其中字母'a'~' ...
 - Verilog HDL常用的行为仿真描述语句
			
一.循环语句 1.forever语句 forever语句必须写在initial模块中,主要用于产生周期性波形. 2.利用for.while循环语句完成遍历 for.while语句常用于完成遍历测试.当 ...
 - VS2012 直接浏览网页时报错
			
VS2012 直接浏览网页时报错 "托管管道模式不能为集成" 只要在configuration文件里面添加 <system.webServer> < ...