Struts2 uses the 12-interceptor 2-Configuration

Source: Internet
Author: User

 

To activate the dependency injection function (or any other function provided by the interceptor) in the action, you must configure the action. Like other elements, many interceptors have provided default configuration items. You only need to confirm that the package of the Action inherits the "struts-Default" package.

Before configuring a new interceptor, you must first define it. <Interceptors... /> And <interceptor... /> Labels must be directly placed in the <package> label. As mentioned above, their configuration items are as follows:

<Interceptors>

...

<Interceptor name = "autowiring"

Class = "... Xwork2.spring. Interceptor. actionautowiringinterceptor "/>

</Interceptors>

We also need to ensure that the required interceptor is applied to the action. This can be achieved in two ways. The first is to assign the Interceptor to each action independently:

<Action name = "my" class = "myaction">

<Result> View. jsp </result>

<Interceptor-ref name = "autowiring"/>

</Action>

In this case, there is no limit on the number of interceptors applied by action. However, the configuration sequence of the interceptor must be the same as the execution sequence.

The second method is to configure a default interceptor under the current package:

<Default-interceptor-ref name = "autowiring"/>

This statement is also directly placed in <package... /> Label, but only one interceptor can be configured as the default value.

Now that the interceptor has been configured, the interceptor will be executed every time the URL mapped to the Action receives the request. However, this function is still very limited, because in most cases, an action must have multiple interceptors. In fact, many functions of struts2 are based on interceptors, so it is not uncommon for an action to have 7 or 8 interceptors. As you can imagine, if we want to configure each interceptor for each action one by one, we will soon become overwhelmed. Therefore, we generally use an interceptor stack to manage the interceptor. Below is an example in the struts-default.xml file:

<Interceptor-stack name = "basicstack">

<Interceptor-ref name = "exception"/>

<Interceptor-ref name = "servletconfig"/>

<Interceptor-ref name = "prepare"/>

<Interceptor-ref name = "checkbox"/>

<Interceptor-ref name = "Params"/>

<Interceptor-ref name = "conversionerror"/>

</Interceptor-stack>

This configuration node is placed in <package... /> Node. Each <Interceptor-ref... /> The tag references the interceptor or interceptor stack configured earlier.

We have seen how to apply the interceptor in the action, and the method of the interceptor stack is the same and the same tag is used:

<Action name = "my" class = "myaction">

<Result> View. jsp </result>

<Interceptor-ref name = "basicstack"/>

</Action>

The same is true for default interceptors-you only need to replace the name of a single interceptor with the name of the interceptor stack.

<Default-interceptor-ref name = "basicstack"/>

According to the above situations, When configuring the initial interceptor and interceptor stack, make sure that their names are unique.

 

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.