成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SpringMvc異常處理器怎么實(shí)現(xiàn)

這篇文章主要講解了“SpringMvc異常處理器怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringMvc異常處理器怎么實(shí)現(xiàn)”吧!

創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的服務(wù)器托管服務(wù)

SpringMvc 異常處理器

簡介

SpringMvc 在處理請(qǐng)求過程中出現(xiàn)異常信息由異常處理器進(jìn)行處理,自定義異常處理器可以實(shí)現(xiàn)一個(gè)系統(tǒng)的異常處理邏輯。

異常理解

異常包含編譯時(shí)異常和運(yùn)行時(shí)異常,其中編譯時(shí)異常也叫預(yù)期異常。運(yùn)行時(shí)異常只有在項(xiàng)目運(yùn)行的情況下才會(huì)發(fā)現(xiàn),編譯的時(shí)候不需要關(guān)心。

運(yùn)行時(shí)異常,比如:空指針異常、數(shù)組越界異常,對(duì)于這樣的異常,只能通過程序員豐富的經(jīng)驗(yàn)來解決和測(cè)試人員不斷的嚴(yán)格測(cè)試來解決。

編譯時(shí)異常,比如:數(shù)據(jù)庫異常、文件讀取異常、自定義異常等。對(duì)于這樣的異常,必須使用 try catch代碼塊或者throws關(guān)鍵字來處理異常。

異常處理思路

系統(tǒng)中異常包括兩類:預(yù)期異常(編譯時(shí)異常)和運(yùn)行時(shí)異常RuntimeException,前者通過捕獲異常從而獲取異常信息,后者主要通過規(guī)范代碼開發(fā)、測(cè)試等手段減少運(yùn)行時(shí)異常的發(fā)生。

系統(tǒng)的dao、service、controller出現(xiàn)都通過throws Exception向上拋出,最后由SpringMvc前端控制器交給異常處理器進(jìn)行異常處理,如下圖:

SpringMvc異常處理器怎么實(shí)現(xiàn)

 全局范圍只有一個(gè)異常處理器。

自定義異常類

第一步:CustomException.java

package com.cyb.ssm.exception;

/**
 * 自定義編譯時(shí)異常
 * 
 * @author apple
 *
 */
public class CustomException extends Exception {
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public CustomException(String msg) {
        super();
        this.msg = msg;
    }
}

第二步:CustomExceptionResolver.java(重點(diǎn))

package com.cyb.ssm.resolver;

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

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

import com.cyb.ssm.exception.CustomException;

public class CustomExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        String message="";
        // 異常處理邏輯
        if (ex instanceof CustomException) {
            message = ((CustomException) ex).getMsg();
        } else {
            message="未知錯(cuò)誤";
        }
        ModelAndView mv=new ModelAndView();
        mv.setViewName("error");
        mv.addObject("message", message);
        return mv;
    }
}

第三步:在springmvc.xml中加入bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 處理器類的掃描 -->
    <context:component-scan
        base-package="com.cyb.ssm.controller"></context:component-scan>
    <mvc:annotation-driven conversion-service="conversionService"/>
    <!-- 顯示配置視圖解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 配置自定義的轉(zhuǎn)換服務(wù) -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <!-- 自定義日期類型轉(zhuǎn)換器 -->
                <bean class="com.cyb.ssm.controller.converter.DateConverter"></bean>
            </set>
        </property>
    </bean>
    <!-- 配置異常處理器 -->
    <bean class="com.cyb.ssm.resolver.CustomExceptionResolver"></bean>
</beans>

第四步:jsp錯(cuò)誤頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>錯(cuò)誤頁面</title>
</head>
<body>
    ${message }
</body>
</html>

第五步:測(cè)試類

@RequestMapping("queryItem")
    public ModelAndView queryItem() throws CustomException {
        //查詢數(shù)據(jù)庫,用靜態(tài)數(shù)據(jù)模擬
        List<Item> itemList = Service.queryItemList();
        ModelAndView mvAndView = new ModelAndView();
        mvAndView.addObject("itemList", itemList);
        //設(shè)置視圖(邏輯路徑)
        mvAndView.setViewName("item/item-list");
        if (true) {
            throw new CustomException("我是自定義異常類");
        }
        return mvAndView;
    }

SpringMvc異常處理器怎么實(shí)現(xiàn)

感謝各位的閱讀,以上就是“SpringMvc異常處理器怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SpringMvc異常處理器怎么實(shí)現(xiàn)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

文章名稱:SpringMvc異常處理器怎么實(shí)現(xiàn)
文章路徑:http://jinyejixie.com/article8/iejpop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、電子商務(wù)、網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、軟件開發(fā)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
陈巴尔虎旗| 开封县| 晋宁县| 营山县| 宜宾市| 肃宁县| 石城县| 万宁市| 南丹县| 石泉县| 安岳县| 博白县| 云浮市| 湘潭市| 浠水县| 宜兰县| 洞头县| 朝阳市| 延庆县| 丹凤县| 连江县| 辽源市| 台山市| 武清区| 平江县| 彝良县| 新巴尔虎左旗| 永济市| 开江县| 林口县| 汉寿县| 西藏| 石家庄市| 扶绥县| 平利县| 普兰县| 宝鸡市| 屯昌县| 会昌县| 南宫市| 平泉县|