SpringMVC06Exception 異常處理

2022-09-07 02:48:11 字數 2851 閱讀 7362

1.配置web.xml檔案

characterencoding

class>org.springframework.web.filter.characterencodingfilterclass>

encoding

utf-8

forceencoding

true

/*springmvc

org.springframework.web.servlet.dispatcherservlet

contextconfiglocation

classpath:spring-mvc.xml

1

/2.實體類userinfo

public

class

userinfo

public

void

setname(string name)

public

integer getage()

public

void

setage(integer age)

}

3.1:自定義異常類(使用者異常)

public

class userinfoexception extends

exception

public

userinfoexception(string message)

}

3.2:自定義異常類(使用者名稱異常)

public

class nameexception extends

userinfoexception

public

nameexception(string message)

}

3.3:自定義異常類(年齡異常)

public

class ageexception extends

userinfoexception

public

ageexception(string message)

}

4.處理器

//物件異常處理

public string dofirst(userinfo info) throws

userinfoexception

else

if (info.getage() > 60)

return "/index.jsp";}}

5.對映器

6.1:檢視頁面(登入頁面)

6.2:檢視頁面(異常測試頁面)

$

6.3:使用者名稱異常(測試頁)

6.4:年齡異常(測試頁)

6.5:歡迎頁面

7.1:測試頁面(正常登入)

7.2:測試使用者名稱錯誤

7.3:測試年齡錯誤

SpringMVC異常統一處理

正文 spring 統一異常處理有 3 種方式,分別為 使用 exceptionhandler 註解 實現 handlerexceptionresolver 介面 使用 controlleradvice 註解 package com.tao.smp.exception api統一的返回結果類 pub...

第7章 Spring MVC的高階技術 異常處理

概述 只要程式都需要處理異常,spring會有哪些處理異常的方式 特定的spring異常將會自動對映為指定的http狀態碼異常上可以新增 responsestatus註解,從而改變http狀態碼在控制器中新增乙個帶有 exceptionhandler註解可以處理這個控制器所有丟擲此異常的 對於自動對...

springmvc異常統一處理(一)

目錄 正文 spring 統一異常處理有 3 種方式,分別為 使用 exceptionhandler 註解 實現 handlerexceptionresolver 介面 使用 controlleradvice 註解 使用該註解有乙個不好的地方就是 進行異常處理的方法必須與出錯的方法在同乙個contr...