SPRINGMVC (10) Implementing annotation-type permission validation

Source: Internet
Author: User

How do you handle the exceptions that occur in your project and write code catch exceptions where every possible exception occurs? This is obviously unreasonable, and when the project becomes larger and bigger it is also not maintainable. So how do we ensure that our code for handling exceptions is streamlined and easy to maintain? This is what this article is about, exception handling.

In spring MVC we can focus on the exception in the following 2 ways:

I. Inherit the Handlerexceptionresolver interface to implement its own processing methods, such as:

 Public classMyhandlerexceptionresolverImplementsHandlerexceptionresolver {@Override PublicModelandview resolveexception (httpservletrequest request, httpservletresponse response, Object handler, Exception Ex) {//add your own exception handling logic, such as logging//TODO auto-generated Method Stub        return NewModelandview ("Exception"); }     }

Then add the following in the project's configuration file:

<id= "Exceptionresolver"  class= "package name." Myhandlerexceptionresolver "/>

This completes the seizure and processing of the exception.

Two. We introduced the first kind of catch handling exception mode, but the first method needs to be configured in the configuration file, sometimes we feel that the configuration file content is too messy, then we can use the @exceptionhandler annotations to achieve zero configuration exception capture and processing.

First, create a parent class Basecontroller for the controller in the package com.demo.web.controllers of our project, as follows:

 Packagecom.demo.web.controllers;Importjava.sql.SQLException;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.web.bind.annotation.ExceptionHandler; Public Abstract classBasecontroller {@ExceptionHandler PublicString Exception (HttpServletRequest request, exception e) {//add your own exception handling logic, such as loggingRequest.setattribute ("Exceptionmessage", E.getmessage ()); //different handling depending on the type of exception        if(EinstanceofSQLException)return"Testerror"; Else            return"Error"; }      }

Second, modify the Helloworldcontroller in the project so that it inherits from the Basecontroller for testing:

 Public class extends basecontroller{    //... Content omitted }

Then, modify the index method in the Helloworldcontroller so that it throws an exception to see if it can catch the normal:

//@AuthPassport@RequestMapping (value={"/index", "/hello"}) PublicModelandview index ()throwssqlexception{Throw NewSQLException ("Database exception. "); /*Modelandview Modelandview = new Modelandview ();      Modelandview.addobject ("message", "Hello world!");      Modelandview.setviewname ("index"); return modelandview;*/}

Finally, add the testerror.jsp view in the Views folder to display the error message:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>error!</title></Head><Body>${exceptionmessage}</Body></HTML>

To run the project:

You can see that the exception has been captured and displayed so that the exception can be captured and processed as long as all our other controllers are inherited from the Basecontroller.

SPRINGMVC (10) Implementing annotation-type permission validation

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.