The basic method and operation of Strust2 Custom Interceptor

Source: Internet
Author: User

Requirements: The production of a website requires users to log in to view, that is, a permission problem

1. First, it is clear that there are two action requests that can be passed before the user logs in, i.e. registration and login.

2. Create interceptors, such as Userlogininterceptor.java, as follows

 Public classUserlogininterceptorextendsAbstractinterceptor {@Override PublicString Intercept (actioninvocation arg0)throwsException {Action action=(Action) arg0.getaction (); //release both of these action requests        if(Actioninstanceofregisteraction| | Actioninstanceofloginaction) {            returnArg0.invoke (); }Else{            //get the session there are no users? User user = (user) Arg0.getinvocationcontext (). GetSession (). Get ("User") ; if(user==NULL){                //if not, you want the page to convey the wrong messageArg0.getinvocationcontext (). GetSession (). Put ("NoLand", "You do not have permission, please register or log in first!"). "); return"Input" ; }Else{                //If there is, release it .                returnArg0.invoke (); }        }    }}

3. When the Interceptor is written, it should be configured in Struts.xml

       <Interceptors>         <!--Configuring a written interceptor -            <Interceptorname= "Userlogin"class= "Com.blog.interceptor.UserLoginInterceptor"/>         <!--define your own interceptor stack, consisting of Struts2 's own interceptor stack and the front interceptor -            <Interceptor-stackname= "Blogstack">                <Interceptor-refname= "Userlogin"/>                <Interceptor-refname= "Defaultstack"/>            </Interceptor-stack>        </Interceptors>       <!--To set the custom interceptor stack as the default stack -        <Default-interceptor-refname= "Blogstack"/>

4. On the no-access jump page, display the reminder message that you put in the session.

The basic method and operation of Strust2 Custom Interceptor

Related Article

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.