标签: springmvchtml
[TOC]前端
本文主要介绍springmvc中异常处理的思路,并展现如何自定义异常处理类以及全局异常处理器的配置java
系统中异常包括两类:git
前者经过捕获异常从而获取异常信息,后者主要经过规范代码开发、测试经过手段减小运行时异常的发生。github
系统的dao、service、controller出现都经过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,以下图:spring
springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。mvc
对不一样的异常类型定义异常类,继承Exception。app
package com.iot.learnssm.firstssm.exception; /** * Created by brian on 2016/3/7. * * 系统 自定义异常类,针对预期的异常,须要在程序中抛出此类的异常 */ public class CustomException extends Exception{ //异常信息 public String message; public CustomException(String message){ super(message); this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
思路:jsp
系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。学习
全局异常处理器处理思路:
解析出异常类型
springmvc提供一个HandlerExceptionResolver
接口
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //handler就是处理器适配器要执行Handler对象(只有method) //解析出异常类型 //若是该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展现 //String message = null; //if(ex instanceof CustomException){ //message = ((CustomException)ex).getMessage(); //}else{ ////若是该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”) //message="未知错误"; //} //上边代码变为 CustomException customException; if(ex instanceof CustomException){ customException = (CustomException)ex; }else{ customException = new CustomException("未知错误"); } //错误信息 String message = customException.getMessage(); ModelAndView modelAndView = new ModelAndView(); //将错误信息传到页面 modelAndView.addObject("message", message); //指向错误页面 modelAndView.setViewName("error"); return modelAndView; } }
<%-- Created by IntelliJ IDEA. User: Brian Date: 2016/3/4 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>错误提示</title> </head> <body> ${message} </body> </html>
<!-- 全局异常处理器 只要实现HandlerExceptionResolver接口就是全局异常处理器 --> <bean class="com.iot.learnssm.firstssm.exception.CustomExceptionResolver"></bean>
全局异常处理器只有一个,配置多个也没用。
在controller、service、dao中任意一处须要手动抛出异常。若是是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,若是不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception { //调用service根据商品id查询商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(items_id); //判断商品是否为空,根据id没有查询到商品,抛出异常,提示用户商品信息不存在 if(itemsCustom == null){ throw new CustomException("修改的商品信息不存在!"); } //经过形参中的model将model数据传到页面 //至关于modelAndView.addObject方法 model.addAttribute("items", itemsCustom); return "items/editItems"; }
public ItemsCustom findItemsById(Integer id) throws Exception { Items items = itemsMapper.selectByPrimaryKey(id); if(items==null){ throw new CustomException("修改的商品信息不存在!"); } //中间对商品信息进行业务处理 //.... //返回ItemsCustom ItemsCustom itemsCustom = null; //将items的属性值拷贝到itemsCustom if(items!=null){ itemsCustom = new ItemsCustom(); BeanUtils.copyProperties(items, itemsCustom); } return itemsCustom; }
上边的功能,建议在service中抛出异常。