SSM-SpringMVC-24:SpringMVC異常高級之自定義異常
阿新 • • 發佈:2018-03-31
BE request input suffix super() internal except simple res
------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------
自定義異常,大家都會,對吧,無非就是繼承異常類等操作,很簡單,我就不多扯皮了,但是在xml配置文件中有個不同的操作,我一會重點列出來
案例開始:
1.自定義異常類:UserageException
package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by Dawn on 2018/3/30. */ public class UserageException extends Exception {public UserageException() { super(); } public UserageException(String message) { super(message); } }
2.自定義異常類:UsernameException
package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by Dawn on 2018/3/30. */ public class UsernameException extends Exception {public UsernameException() { super(); } public UsernameException(String message) { super(message); } }
3.定義處理器和處理方法:
package cn.dawn.day17selfexceptionresolver; import cn.dawn.day17selfexceptionresolver.userexception.UserageException; import cn.dawn.day17selfexceptionresolver.userexception.UsernameException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;/** * Created by Dawn on 2018/3/28. */ @Controller public class ZDYExceptionController { @RequestMapping("/zidingyiException") public String zidingyiException(String username,Integer userage) throws Exception { if(!username.equals("admin")){ throw new UsernameException("登陸名不正確"); } if(userage<18){ throw new UserageException("未成年,走開"); } return "success"; } }
在自己的xml大配置中:SimpleMappingExceptionResolver配一個exceptionMappings
<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包掃描器--> <context:component-scan base-package="cn.dawn.day16exceptionhigh"></context:component-scan> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day16/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--默認出現異常跳轉的頁面--> <property name="defaultErrorView" value="error"></property> <!--這個可以註入異常對象,就像catch參數裏的(Exception ex)一樣--> <property name="exceptionAttribute" value="ex"></property> <!--自定義的異常--> <property name="exceptionMappings"> <props> <prop key="cn.dawn.day16exceptionhigh.userexception.UserageException">age</prop> <prop key="cn.dawn.day16exceptionhigh.userexception.UsernameException">name</prop> </props> </property> </bean> </beans>
上面標紅的是重中之重
4.將web.xml的中央調度器上下文配置位置改為上面新的那個xml
5.jsp頁面
5.1age.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>年齡錯誤ERROR</h2> <p>${ex.message}</p> 服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天 </body> </html>
5.2name.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>名字錯誤ERROR</h2> <p>${ex.message}</p> 服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天 </body> </html>
5.3login.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>登錄</h2> <form action="${pageContext.request.contextPath}/zidingyiException" method="post"> 用戶名:<input name="username"> 年齡:<input name="userage"> <input type="submit" value="登錄"/> </form> </body> </html>
5.4success.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <html> <body> <%--<img src="image/1.jpg">--%> <h2>Success!</h2> </body> </html>
5.5error.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>ERROR</h2> <p>${ex.message}</p> 服務器被猴子砍了,攻城獅在搶修中,還殺了個程序猿祭天 </body> </html>
6.啟動tomcat訪問login.jsp
SSM-SpringMVC-24:SpringMVC異常高級之自定義異常