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
public3.1:自定義異常類(使用者異常)class
userinfo
public
void
setname(string name)
public
integer getage()
public
void
setage(integer age)
}
public3.2:自定義異常類(使用者名稱異常)class userinfoexception extends
exception
public
userinfoexception(string message)
}
public3.3:自定義異常類(年齡異常)class nameexception extends
userinfoexception
public
nameexception(string message)
}
public4.處理器class ageexception extends
userinfoexception
public
ageexception(string message)
}
//物件異常處理5.對映器public string dofirst(userinfo info) throws
userinfoexception
else
if (info.getage() > 60)
return "/index.jsp";}}
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...