Summary of struts2 annotations----@InterceptorRef

Source: Internet
Author: User

write in front:

This article is mainly about the use of the annotation mode interceptor, so the XML file will not be detailed, the code in the text is relatively simple, there is no permission control Ah, interception of information and other code, the focus is to explain the use of the method of annotation, understand the use of the method, the content can be self-expanding, according to their own needs, All right, that's it.


Last time wrote @action and @result two kinds of annotations, this time to talk about @interceptor and @parentpackage


Introduction:@InterceptorRef/@InterceptorRefs:

@InterceptorRef can be used to configure interceptors with the following properties

    1. Value: Specifies the name of the Interceptor (stack), which is the name configured below (typically only value can be omitted)
    2. Params: is an array of strings used to pass parameters

Although it is the way to use annotations, it is still necessary to pre-configure the Interceptor information in the XML file

<interceptor name= "Interceptor1" class= "Struts2.interceptor.TestInterceptor1" ></interceptor>< Interceptor-stack name= "InterceptorStack1" ><interceptor-ref name= "Defaultstack"/><interceptor-ref Name= "Interceptor1"/></interceptor-stack>

The specific interceptor class is as follows

public class TestInterceptor1 extends Abstractinterceptor {@Overridepublic String intercept (actioninvocation arg0) Throws Exception {System.out.println ("--------TestInterceptor1--------"); return Arg0.invoke ();}}

OK, so we've defined the interceptor, and if we use XML configuration, then we need to use the interceptor in the following way

<action name= "Login" class= "struts.action.xxx" > <!--....--> <interceptor -ref name= "InterceptorStack1"/></action>
Since the action is configured with annotations, then the interceptor does not need to write in this, the specific configuration in two ways, one is configured on the class, the other is configured on the method, the following is configured on the method (action example or last article example, specifically, don't point me, don't point me !! Don't ~ ~)

@Namespace ("/") @ParentPackage ("Default") public class TestAction1 extends Actionsupport {@Action (value = "Add", results = {@Result (name = "Add", location = "/add.jsp")},interceptorrefs={@InterceptorRef ("InterceptorStack1")}) public String Add () throws Exception {return "add";}}

Interceptorrefs can put multiple @interceptorref, and results similar

When accessing/add, the console prints out--->--------TestInterceptor1--------


The next step is to use annotations on the class

@Namespace ("/") @ParentPackage ("Default") @InterceptorRefs ({@InterceptorRef ("InterceptorStack1")}) public class  TestAction1 extends Actionsupport {@Action (value = "add", results = {@Result (name = "Add", location = "/add.jsp")}) public String Add () throws Exception {return "add";}}
This effect is the same as above, and the action of the access response will be typed--->--------TestInterceptor1--------
@InterceptorRefs can configure multiple interceptors, similar to specifying interceptorrefs= {} in @action, and of course, you can specify only one interceptor with @interceptorref

OK, this is the two ways to use, from the above example, it seems that there is no difference, in fact, the difference is still there, next look at the following action

@Namespace ("/") @ParentPackage ("Default") @InterceptorRef ("InterceptorStack1") public class TestAction1 extends Actionsupport {@Action (value = "add", results = {@Result (name = "Add", location = "/add.jsp")}) the public String Add () throw s Exception {return "add";} @Action (value = "Delete", results = {@Result (name = "Delete", location = "/delete.jsp")}) public String Delete () throws E xception {return "delete";}}
This will print when you access/delete and/add--->--------TestInterceptor1--------
That is, the interceptor configured on the class is acting on all methods.

Look at the following mix and use two ways

@Namespace ("/") @ParentPackage ("Default") @InterceptorRef ("InterceptorStack1") public class TestAction1 extends Actionsupport {@Action (value = "add", results = {@Result (name = "Add", location = "/add.jsp")},interceptorrefs = {@Interc Eptorref ("InterceptorStack2") <span style= "White-space:pre" ></SPAN>}) public String Add () throws Exception {return "add";} @Action (value = "Delete", results = {@Result (name = "Delete", location = "/delete.jsp")}) public String Delete () throws E xception {return "delete";}}

---> --------TestInterceptor1 are printed when/delete is accessed--------

This is of course, the Delete method does not change anything, and as with the example of the interceptor above that acts on the class, the Interceptor 1 will intercept the method

and what happens when you visit/add? This will print out

---> --------TestInterceptor1-------- and

---> --------TestInterceptor2--------

(interceptors 2 and 1 are defined as 1, just change 1 to 2)

This is the difference between the interceptor and the definition on the class that act on the method

The interceptor defined on the method will only intercept this method, and the interceptor defined on the class will intercept all methods



Struts2 Comment Summary [email protected]

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.