SPRINGMVC Interceptor Interceptor Configuration using (source) __spring

Source: Internet
Author: User

Two-step configuration Spring MVC Interceptor Interceptor One interceptor first step to implement Handlerinterceptor interface The second step registers into spring

Two-step configuration Spring MVC Interceptor Interceptor A. Interceptor.

Unlike filters, interceptors are java-based reflection mechanisms, and filters are Java EE standards, in the form of function callbacks. Detailed differences See also: relationship and difference between Java filter and SPRINGMVC interceptor

In Springmvc, the Interceptor (Handlerinterceptor) is an interface (interface) that must be overridden in three ways:

          Prehandler (): Before entering the handler, the use of identity authentication, identity authorization, login verification, such as identity authentication, the user did not login, interception is no longer down, the return value is False, you can intercept; otherwise, when true, the interception is not performed;

          Posthandler (): After entering the handler method, to return to Modelandview before the execution, using the scene from the Modelandview parameters, for example, the common model data passed here to the view, you can also specify the view of the display;

         Afterhandler (): Similar to the finally statement block or C + + destructor, after the execution of handler, generally used for exceptions, log processing, etc.;

Quote: http://blog.csdn.net/lablenet/article/details/50483674 First step: Implement Handlerinterceptor interface

example, all requests except/login are blocked:

Package site.gaoyisheng.filter;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.HandlerInterceptor;

Import Org.springframework.web.servlet.ModelAndView;

Import Site.gaoyisheng.pojo.User; public class Loginhandlerinterceptor implements Handlerinterceptor {@Override public boolean prehandle (HTTPSERVL  Etrequest request, HttpServletResponse response, Object handler) throws Exception {String RequestUri

        = Request.getrequesturi (); if (!requesturi.equalsignorecase ("/trans/login")) {//Description in edit page User currentUser = (user) Request
            . GetSession (). getattribute ("CurrentUser");
            if (CurrentUser!= null) {//Login successful user return true; else {//No login, turn to login interface Request.getrequestdispatcher ("/login"). Forward (request, response)
                ;
            return false;
    }    else return true;
            @Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview) throws Exception {} @Override public void Aftercompletion (httpservletreq
 Uest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}
Step two: Register in spring

In the Springmvc.xml configuration, register the interceptor.

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path= "/**"/>
        <bean class= " Site.gaoyisheng.filter.LoginHandlerInterceptor "></bean>
    </mvc:interceptor>
</MVC: Interceptors>

Source: Welcome to Exchange.
Https://github.com/timo1160139211/trans

Resources:
http://blog.csdn.net/lablenet/article/details/50483674
http://www.imooc.com/learn/498

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.