<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringMVC如何處理異常?

          共 3066字,需瀏覽 7分鐘

           ·

          2020-09-11 05:14


          一、描述


          在J2EE項目的開發(fā)中,不管是對底層的數(shù)據(jù)庫操作過程,還是業(yè)務層的處理過程,還是控制層的處理過程,都不可避免會遇到各種可預知的、不可預知的異常需要處理。每個過程都單獨處理異常,系統(tǒng)的代碼耦合度高,工作量大且不好統(tǒng)一,維護的工作量也很大。?那么,能不能將所有類型的異常處理從各處理過程解耦出來,這樣既保證了相關(guān)處理過程的功能較單一,也實現(xiàn)了異常信息的統(tǒng)一處理和維護?答案是肯定的。下面將介紹使用Spring MVC統(tǒng)一處理異常的解決和實現(xiàn)過程。?


          二、Spring MVC處理異常常見方式


          Spring MVC處理異常常見有兩種方式:?


          1、實現(xiàn)HandlerExceptionResolver 接口,自定義異常處理器;
          2、使用注解@ExceptionHandler實現(xiàn)異常處理。

          三、實戰(zhàn)


          1、實現(xiàn)HandlerExceptionResolver接口,自定義異常處理器。


          package?com.demo.exception;

          import?javax.servlet.http.HttpServletRequest;
          import?javax.servlet.http.HttpServletResponse;

          import?org.springframework.web.servlet.HandlerExceptionResolver;
          import?org.springframework.web.servlet.ModelAndView;

          public?class?MyHandlerExceptionResolver?implements?HandlerExceptionResolver{
          ????
          ????/**
          ?????* 定義自己的異常處理器(實現(xiàn)HandlerExceptionResolver接口)
          ?????*
          ?????*/

          ????public?ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
          ????????????Object handler, Exception ex)
          {
          ????????
          ?????????ModelAndView mv=new?ModelAndView();
          ?????????mv.addObject("ex",ex);
          ?????????mv.setViewName("error");
          ?????????return?mv;

          ????}

          }


          然后在項目的配置文件中添加(spring-mvc.xml):



          <bean?class="com.demo.exception.MyHandlerExceptionResolver"/>


          error.jsp


          <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>
          <%
          String?path?= request.getContextPath();
          String?basePath?= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <%@taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"%>

          <html>
          <head>
          ????<title>錯誤頁面title>
          ????<link?href="styles/main.css" />" type="text/css" rel="stylesheet" />
          head>
          <body?style="margin:0 auto;text-align:center;">
          ????<div?class="main"?style="width:40%;">
          ????????<h2?class="title"><span>出錯啦!span>h2>
          ????????<fieldset>
          ????????????<legend>錯誤信息legend>
          ????????????<p>
          ????????????????${ex.message}
          ????????????p>?
          ????????fieldset>
          ????div>
          body>
          html>

          ??

          這樣就完成了異常的捕捉和處理。


          2、使用注解@ExceptionHandler實現(xiàn)異常處理


          我們介紹了第一種捕捉處理異常方式,但是第一種方式需要在配置文件中進行配置,有的時候我們會覺得配置文件內(nèi)容太多太亂,那么我們就可以借助@ExceptionHandler注解來實現(xiàn)零配置的異常捕捉和處理。


          首先,新建一個類,用于定義自己的異常處理器。注意,類中處理異常的方法要使用@ExceptionHandler注解。


          package?com.demo.exception;

          import?java.sql.SQLException;
          import?javax.servlet.http.HttpServletRequest;
          import?org.springframework.web.bind.annotation.ExceptionHandler;

          public?class?MyHandlerExceptionResolver1?{
          ????
          ?????@ExceptionHandler
          ?????public?String exception(HttpServletRequest request, Exception ex)?{
          ??????????????
          ?????????//添加自己的異常處理邏輯,如日志記錄
          ?????????request.setAttribute("exceptionMessage", ex.getMessage());

          ?????????// 根據(jù)不同的異常類型進行不同處理
          ?????????if(ex instanceof?SQLException){
          ?????????????return?"testerror";
          ?????????}else{
          ????????????return?"error";
          ?????????}
          ??????}

          }


          其次,新建一個HelloWorldController,讓它繼承于我們自己定義的注解異常處理器MyHandlerExceptionResolver1。


          然后,修改HelloWorldController 中的index方法,使其拋出異常,看能不能正常捕捉。


          package?com.demo.controller;

          import?java.sql.SQLException;

          import?org.springframework.stereotype.Controller;
          import?org.springframework.web.bind.annotation.RequestMapping;
          import?org.springframework.web.servlet.ModelAndView;

          import?com.demo.exception.MyHandlerExceptionResolver1;

          @Controller
          @RequestMapping("/hello")
          public class HelloWorldController extends MyHandlerExceptionResolver1{
          ????
          ????@RequestMapping("/index")
          ????public ModelAndView index() throws SQLException{
          ????????
          ????????throw?new?SQLException("數(shù)據(jù)庫異常!");
          ????}
          }


          最后,在views文件夾中添加testerror.jsp視圖來顯示錯誤信息:


          <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>
          <%
          String?path?= request.getContextPath();
          String?basePath?= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <%@taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"%>

          <html>
          <head>
          ????<title>錯誤頁面title>
          ????<link?href="styles/main.css" />" type="text/css" rel="stylesheet" />
          head>
          <body?style="margin:0 auto;text-align:center;">
          ????<div?class="main"?style="width:40%;">
          ????????<h2?class="title"><span>出錯啦!span>h2>
          ????????<fieldset>
          ????????????<legend>錯誤信息legend>
          ????????????<p>
          ????????????????${exceptionMessage}
          ????????????p>?
          ????????fieldset>
          ????div>
          body>
          html>


          運行項目:http://localhost/SSMProDemo/hello/index



          可以看到異常已經(jīng)被捕捉并顯示,這樣只要把我們的其它的Controller全部繼承于BaseController就能實現(xiàn)異常的集中捕捉和處理了。



          原文鏈接:cnblogs.com/xiaoxi/p/6273539.html





          瀏覽 44
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲AV成人精品日韩一区麻豆 | 人人色人人射 | 波多野结衣一区二区三区 | 在线免费观看视频a | 亚洲一区动漫 |