Ssm-springmvc-26:springmvc exception-Scary Custom Exception Annotations Edition

Source: Internet
Author: User

------------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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.