SPRINGMVC (13) Exception annotations

Source: Internet
Author: User

Using exception annotations is more convenient

Exception Handling Classes

Package Demo15annotationexception;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 mycom on 2018/3/30.*/@Controller Public classExceptioncontroller {/** This tag means something unusual. * @ExceptionHandler () * This parenthesis can specify the specific type of exception you want, not the top exception * @param ex * @retu RN*/@ExceptionHandler PublicModelandview handlerexception (Exception ex) {Modelandview mv=NewModelandview (); Mv.addobject ("ex", ex);//saved data, displaying error messages on the page with an El expression//by default, the following two conditions are not sufficient to walk this,Mv.setviewname ("Error"); //To determine the type of exception        if(ex instanceof Nameexception) {mv.setviewname ("nameexception"); }        if(ex instanceof Ageexception) {mv.setviewname ("ageexception"); }        returnMV; } @RequestMapping ("/first")     PublicString Dofirst (string name,intAge ) throws Exception {//return different pages depending on the exception        if(!name.equals ("Admin")){            Throw NewNameexception ("User Name Exception"); }        if(age> -){            Throw NewAgeexception ("age does not match"); }        return "Success"; }}

Two types of exceptions: User name exception and age exception

 package demo15annotationexception;  /*  * * Created by mycom on 2018/3/30.  */ public  class   Nameexception extends Exception { public     Span style= "COLOR: #000000" > Nameexception () {super ();         public   nameexception (String message) {    Super (message); }}
Package demo15annotationexception; /*  */Publicclass  ageexception extends Exception    {public ageexception () {        super ();    }      Public ageexception (String message) {        super (message);}    }

In the 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/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"> <!--Package Scanner--<context:component-scanBase-package="demo15annotationexception"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/error/"></property> <property name="suffix"Value=". JSP"></property> </bean> <!--note-driven <mvc:annotation-driven></mvc:annotation-driven> </beans>

Page or use the previous blog page, you can define the page

This method can only be used in this class, in other classes can not be used, so we have to deal with the exception of the methods mentioned in a class, other classes to use the words inherit this class, but there is a disadvantage, in Java only support single inheritance, so this class can not inherit other classes

SPRINGMVC (13) Exception annotations

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.