Two steps for Interceptor

Source: Internet
Author: User

From: The path to growth http://xugw.blog.sohu.com/124311370.html

 

Two steps for Interceptor

1. Write your own interceptor public class privilegecheckinterceptor extends Abstractinterceptor {// Implement the interceptor Interface   Public String intercept (actioninvocation Invocation) throws exception { System. Out. println ("privilegecheckinterceptor "); Return invocation. Invoke () ;}2. Configure the global Interceptor (struts. XML) <struts>   <Package name = "default" extends = "struts-Default">   <Interceptors>   <Interceptor name = "myinterceptor" class = "test. Interceptor. privilegecheckinterceptor"/>   <Interceptor-stack name = "mystack">   <Interceptor-ref name = "myinterceptor"/>   <Interceptor-ref name = "defaultstack"/>   </Interceptor-stack>   </Interceptors>     <Default-interceptor-ref name = "mystack"/>   <Action name = "users1"   Class = "test. Action. usermanagementaction">   <Result name = "input">/index. jsp </result>   <Result name = "success">/success. jsp </result>   </Action>     </Package> </struts> after this configuration, all actions defined in the package will be intercepted by privilegecheckinterceptor annotation. You can also configure the interceptor, however, the global interceptor still needs struts. xml configuration Note: The interception configuration should be placed before the action Problem: Convention and interceptor (global) cooperation issues. Convention is not configured, And the interceptor must be defined in struts. xml, How do I specify a global interceptor for all actions? (This is a good question. I have been looking for a long time to find this answer) Solution: with the package inheritance relationship of struts2, The parentpackage interceptor is inherited by childpackage. Define the interceptor in struts. xml and add struts. Convention. Default. Parent. Package = default to struts. properties. Note that this package name ( Default ) The package name must be consistent with that in struts. xml. <package name =" Default "Extends =" struts-Default "> you can also use @ parentpackage (" Default ") Method to specify the parent package name problem: how to obtain the intercepted method name to solve: Use the interceptor object Actioninvocation Actionproxy proxy = actioninvocation. getproxy (); proxy. getmethod ();

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.