"Java.Spring.MVC" uses Handlerinterceptor to intercept requests

Source: Internet
Author: User

Spring's handler mapping mechanism provides a handler interceptors that can be used to add specific functionality to specific requests, such as the amount of money to be detected.

The handler mapping interceptor must implement the Handlerinterceptor interface. The interface defines three methods:

Prehandler ()-called Posthandler () before the actual handler is executed-is invoked after handler is executed aftercompletion ()-called after the request processing is completed

The Prehandler () method returns a Boolean value. You can use this method to continue or break the execution chain. Returns true, continues execution of the chain, or returns false, stopping subsequent execution.

Interceptor is configured using the Interceptors property, which is the property of the Handlermapping class.

For example:

<beans> <bean id= "handlermapping" class=
                Thod.annotation.RequestMappingHandlerMapping "> <property name=" interceptors "> <list> <ref bean= "Officehoursinterceptor"/> </list> </property> </bean > <bean id= "officehoursinterceptor" class= "samples." Timebasedaccessinterceptor "> <property name=" openingtime "value=" 9 "/> <property name=" ClosingT IME "value="/> </bean> <beans> 
Package samples;

public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {

    private int openingtime;
    private int closingtime;

    public void setopeningtime (int openingtime) {
        this.openingtime = openingtime;
    }

    public void setclosingtime (int closingtime) {
        this.closingtime = closingtime;
    }

    public boolean prehandle (HttpServletRequest request, httpservletresponse response,
            Object handler) throws Exception {
        Calendar cal = Calendar.getinstance ();
        int hour = Cal.get (hour_of_day);
        if (Openingtime <= hour && Hour < Closingtime) {return
            true;
        }
        Response.sendredirect ("http://host.com/outsideOfficeHours.html");
        return false;
    }
}

Any request requested by the handler will be interrupted by Timebasedaccessinterceptor.

Use is the base class that configures the Handlerinterceptoradaptor class as an interrupt (the implementation class of the Handlerinterceptor interface).




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.