Configuration of the Struts2 interceptor

Source: Internet
Author: User

? Struts2 interceptors can intercept a number of requests, such as users do not log in to access a page, such as the user does not have access to a page, but send access requests, these are to be intercepted, based on these requirements, I studied the Struts2 interceptor.

To use interceptors, you first need to configure them in Struts.xml, with the following configuration files:

                                                                                                                                                       / web-inf/index/login.jsp                                                                           mresult

It is worth noting that the name of the defined interceptor stack and interceptor-stack must be consistent.

Once configured, we need to write a interceptor class to handle requests for user access (allowing access or not, etc.), which is Logininterceptor.java in the class attribute of <interceptor> above:

Logininterceptor {    string intercept (actioninvocation actioninvocation)  Exception {        String name =  Actioninvocation.getinvocationcontext (). GetName ();         (name.equals ())  {            actioninvocation.invoke ();         } {             map session = actioninvocation.getinvocationcontext (). GetSession ();              (session == )  {                 ServerCommonString.;             } {                 string userid =  (String)  session.get ();                 String userPwd =  (String)  session.get ();                 String userType =  (String)  session.get ();                  (userid == | |  userpwd == | |  userType == )  {                     ServerCommonString.;                 } {                      actioninvocation.invoke ();                 }             }        }     }}


It is important to note that this class inherits the Abstractinterceptor class and then overrides the Interceptor () method.

That's about it, that's pretty simple!

Configuration of the Struts2 interceptor

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.