------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------
Annotation method to implement exception parsing, say, directly to start, and the same habits as before, and the same code on the back of the blog, not the same in front
Case:
1. Customizing the processor and processing methods
Package Cn.dawn.day18annotationexception;import Cn.dawn.day18annotationexception.userexception.userageexception;import Cn.dawn.day18annotationexception.userexception.usernameexception;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.ExceptionHandler; Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.servlet.modelandview;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpServletResponse;/** * Created by Dawn on 2018/3/28.*/@Controller Public classZdyexceptioncontroller {/*acting on both of these*/@ExceptionHandler ({usernameexception.class, Userageexception.class}) PublicModelandview resolveexception (Exception ex) {Modelandview Modelandview=NewModelandview (); /*The returned exception object*/Modelandview.addobject ("ex", ex); /*judge to go to that page*/ if(ex instanceof Usernameexception) {modelandview.setviewname ("name"); } if(ex instanceof Userageexception) {modelandview.setviewname (" Age"); } returnModelandview; } @RequestMapping ("/annotationexception") Publicstring Annotationexception (String Username,integer userage) throws Exception {if(!username.equals ("Admin")){ Throw NewUsernameexception ("login name is incorrect"); } if(userage< -){ Throw NewUserageexception ("underage, go away"); } return "Success"; }}
2. Own XML large configuration file
<?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/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><!--package Scanner--<context:component-scanBase-package="cn.dawn.day18annotationexception"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/day18/"></property> <property name="suffix"Value=". JSP"></property> </bean></beans>
3. Modify the context configuration path of the central scheduler for Web. xml
4.login.jsp
<%@ page pageencoding="UTF-8"Contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>"${pagecontext.request.contextpath}/annotationexception"Method="Post">User name:<input name="username">Age:<input name="Userage"> <input type="Submit"Value="Login"/></form></body>The following content is the same as the previous blog
5.UserageException Custom Exceptions
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);} }
6.UsernameException Custom Exceptions
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);} }
7.jsp page
7.1success.jsp
<%@ page language= "java" pageencoding= "Utf-8" iselignored= "false"%>
7.2error.jsp
<%@ page pageencoding= "UTF-8" contenttype= "Text/html;charset=utf-8" language= "Java" iselignored= "false" %>
7.3age.jsp
<%@ page pageencoding= "UTF-8" contenttype= "Text/html;charset=utf-8" language= "Java" iselignored= "false" %>
7.4name.jsp
<%@ page pageencoding= "UTF-8" contenttype= "Text/html;charset=utf-8" language= "Java" iselignored= "false" %>
8. Launch Tomcat to access login.jsp
Ssm-springmvc-26:springmvc exception-Scary custom exception annotations version