在springMVC中使用自定义的异常处理
2015-08-05 10:39
531 查看
在spirngMVC中进行异常的处理,是在Controller层进行处理,在业务层,dao层都是向上跑出去。
在congtroller中也跑出去,抛给dispatcherServlet进行处理。
使用自定义的异常类需要定义一个userExceptionPo进行再抛出异常时候的异常信息存储。
需要定义一个异常解析器,异常解析器exceptionresolver中判断该异常是否是从自定义异常来的,如果是,则保持异常信息,如果不是,则重新创建异常信息。最后将异常信息设置在modelANdView返回到dispatcherServlet中。
需要在springMVC-servlet.xml中配置处理自定义异常解析器的bean,id="hadlerExceptionResolver" class为自定义异常解析器类。
ExceptionPO类定义:
配置handlerExceptionResolver
在congtroller中也跑出去,抛给dispatcherServlet进行处理。
使用自定义的异常类需要定义一个userExceptionPo进行再抛出异常时候的异常信息存储。
需要定义一个异常解析器,异常解析器exceptionresolver中判断该异常是否是从自定义异常来的,如果是,则保持异常信息,如果不是,则重新创建异常信息。最后将异常信息设置在modelANdView返回到dispatcherServlet中。
需要在springMVC-servlet.xml中配置处理自定义异常解析器的bean,id="hadlerExceptionResolver" class为自定义异常解析器类。
<bean id="handlerExceptionResolver" class="com.jd.jr.Exception.UserExceptionResolver"> </bean>
自定义异常解析类实现了HandlerexceptionResolver
<pre name="code" class="java">package com.jd.jr.Exception; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; /** * Created by guojiangjiang on 2015/8/5. */ /* * 自定义异常解析 * springMVC提供的异常处理主要有两种方式,一种是直接实现自己的 * HandlerExceptionResolver,另一种是使用注解的方式实现 * 一个专门用于处理异常的Controller——ExceptionHandler。 * */ public class UserExceptionResolver implements HandlerExceptionResolver { /** * @param httpServletRequest * @param httpServletResponse * @param o * @param e 系统抛出的异常对象,从里面拿到异常 * @return 返回类型为ModelAndView */ public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { UserException userException = new UserException(); ModelAndView mv = new ModelAndView(); if(e instanceof UserException){ //是自定义异常类型,在业务层中通过抛出异常的方式进行处理: //定义自己可能抛出的异常:throw new UserException("hello Excpetion"); userException = (UserException)e; mv.setViewName("error"); mv.addObject("message",userException.getMessage()); return mv; }else if(e instanceof IOException){ mv.addObject("message","ioExe"); mv.setViewName("error"); return mv; }else if(e instanceof SQLException){ mv.addObject("message","sqlexe"); mv.setViewName("error"); return mv; }else{ System.out.println(e.getMessage()); mv.addObject("message",new UserException("unknow exception")); mv.setViewName("error"); return mv; } } }
业务方法测试:
<pre name="code" class="java">package com.jd.jr.controller; import com.jd.jr.Exception.UserException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.io.IOException; import java.sql.SQLException; /** * Created by guojiangjiang on 2015/8/10. */ @Controller public class ExceptionTest { @RequestMapping("/testException") public String exception() throws Exception { String ex ="io"; System.out.println("exception starting ..."+ex); if (ex.equals("io")){ throw new IOException(); }else if(ex.equals("sql")){ throw new SQLException(); }else if(ex.equals("cus")){ System.out.println("cus ..."); throw new UserException("cus"); } return "error"; } }
ExceptionPO类定义:
<pre name="code" class="java">package com.jd.jr.Exception; /** * Created by guojiangjiang on 2015/8/5. */ public class UserException extends Exception { public UserException() { } private String message; public UserException(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
配置handlerExceptionResolver
<pre name="code" class="html"> <!--配置自定义的异常处理-->
<bean id="handlerExceptionResolver" class="com.jd.jr.Exception.UserExceptionResolver"> </bean>
相关文章推荐
- java数组
- JAVA中使用FTPClient上传下载
- 如何用JUnit4测试spring service
- java中的float double精度丢失
- Eclipse 安装 FreeMarker IDE 方法
- Java环境变量的配置步骤及JDK、JRE
- Java IO【21】PrintWriter的学习
- Java 数字转百分比%
- 关于 Eclipse 导出 Android项目 JavaDoc 详细过程
- java循环|分支结构
- 【MyBatis】MyBatis+SpringMVC+EasyUI整合分页
- final、static、static final修饰的字段赋值的区别
- JAVA学习笔记-----------InetAddress 的有关IP地址的使用案例(杂乱)
- Spring Ioc--Bean装配
- java中volatile关键字的含义
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典)
- java加密解密技术(2) 单向加密MD5
- java 时间戳转换
- Spring_Ioc&Factory
- Java基础-集合Collection&List接口