• No results found

Interceptor Stacks

In document Struts2 Black Book (Page 114-119)

For different applications and for different actions in an application, we may need to apply a same set of Interceptors repeatedly. Therefore, instead of writing the whole list of Interceptors in every action mapping repeatedly, we can group these set of Interceptors as an Interceptor stack and can use stack

name in the action mapping. In Listing 4.2, we have defined two Interceptors and an Interceptor stack (custom_stack) which includes these two Interceptors. The Interceptor stack is defined using

<interceptor-stack> element. The action mapping provided in Listing 4.2 uses a single

<interceptor-ref> element to declare Interceptor stack to be used, instead of giving two

<interceptor-ref> elements for each Interceptor class, as shown in Listing 4.1:

Listing 4.2: Sample Interceptor Stack defined in struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd ">

<struts>

<include file="struts-default.xml"/>

<package name="default" extends="struts-default">

<interceptors>

<interceptor name="interceptor1" class="Interceptor1_class_name"/>

<interceptor name="interceptor2" class="Interceptor2_class_name "/>

<interceptor-stack name="custom_stack">

<interceptor-ref name="interceptor1"/>

<interceptor-ref name="interceptor2"/>

</interceptor-stack>

</interceptors>

<action name="login" class="LoginAction">

<interceptor-ref name="custom_stack"/>

<result name="input">login.jsp</result>

<result name="success">success.jsp</result>

</action>

</package>

</struts>

The name attribute of <interceptor-ref> can take the name of an Interceptor or an Interceptor stack.

Default Interceptor Configuration–struts-default.xml

We have developed Struts 2 applications in previous chapters, but most of the applications do not define any Interceptor or Interceptor stack in its Struts configuration file. All framework Interceptors are defined in struts-default.xml. In addition to defining all framework supported results, the struts-default.xml file contains the definitions for all Interceptors and Interceptor stacks. The Interceptor and Interceptor stack definitions can be extended to your application from default.xml. To use the Interceptors bundled with framework, we can include struts-default.xml into our struts.xml file and extend our package from the struts-default package.

Here’s one more sample struts.xml file, given in Listing 4.3, for a Struts 2 application:

Listing 4.3: Sample struts.xml with three action mappings

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd ">

<struts>

<include file="struts-default.xml"/>

<package name="mypackage" extends="struts-default">

<action name="action1" class="someAction1">

<interceptor-ref name="params"/>

<interceptor-ref name="servlet-config"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="workflow"/>

<result name="success">success.jsp</result>

<result name="error">error.jsp</result>

</action>

<action name="action2" class="someAction2">

<interceptor-ref name="completeStack"/>

<result name="success">success.jsp</result>

<result name="error">error.jsp</result>

</action>

<action name="action3" class="someAction3">

<result name="success">success.jsp</result>

<result name="error">error.jsp</result>

</action>

</package>

</struts>

The three action mappings are provided in the Listing 4.3. All three action mappings are grouped in a package named mypackage. The different set of Interceptors are defined for two action mappings and the last action mapping do not have any <interceptor-ref> element defining Interceptor for it.

The four Interceptors are defined for action action1. Now, you might ask, “Where are the definitions for these Interceptors and what classes will be executed?” The answer is in the struts-default.xml file and shown in Listing 4.4. The struts-default.xml file, which is contained in struts2-core-2.0.6.jar available with Struts 2 distribution, defines all framework Interceptors. To use framework Interceptors, like action1, make sure of the following things:

‰ struts-default.xml is included in your struts.xml file (see <include> element in Listing 4.3).

‰ action mappings are enclosed in a <package> element, which extends struts-default package (see <package> element in Listing 4.3).

Here’s Listing 4.4 for some Interceptor definitions provided in struts-default.xml file:

Listing 4.4: Interceptors configured in struts-default.xml

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts-default">

<!—Result type declaration-->

<result-types>

. . . . . . . .

</result-types>

<!-- Interceptor and Interceptor Stack Declaration -->

<interceptors>

These packages containing Interceptor classes are available in the Struts 2 JAR files. The Interceptor definition just maps an Interceptor class name with an alias name, which can further be used to refer that Interceptor.

Similar to defining Interceptors, different Interceptor stacks are also defined in struts-defalut.xml.

These Interceptor stacks are a group of some commonly used and required Interceptors. These Interceptors stacks are defined using <interceptor-stack name=" "> element. An Interceptor stack can combine Interceptors as well as other Interceptor stacks. See the definition of basicStack and its use in other Interceptor stack definitions in Listing 4.5. The required conditions to use Interceptor stacks in our action mapping are similar to what is defined for using Interceptor definitions from struts-default.xml earlier. The second action mapping provided in Listing 4.3 uses completeStack, which is an Interceptor stack and defined in struts-default.xml.

Listing 4.5 defines all Interceptor stacks:

Listing 4.5: Interceptor Stacks configured in struts-default.xml

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts-default">

<!-- Interceptor and Interceptor Stack Declaration -->

<interceptors>

<!-- Sample validation and workflow stack -->

<interceptor-stack name="validationWorkflowStack">

<interceptor-ref name="basicStack"/>

<interceptor-ref name="validation"/>

<interceptor-ref name="workflow"/>

</interceptor-stack>

<!-- Sample file upload stack -->

<interceptor-stack name="fileUploadStack">

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>

<!-- Sample model-driven stack -->

<interceptor-stack name="modelDrivenStack">

<interceptor-ref name="model-driven"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>

<!-- Sample action chaining stack -->

<interceptor-stack name="chainStack">

<interceptor-ref name="chain"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>

<!-- Sample i18n stack -->

<interceptor-stack name="chainStack">

<interceptor-ref name="i18n"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>

<interceptor-stack name="executeAndWaitStack">

<interceptor-ref name="defaultStack"/>

</interceptor-stack>

</interceptors>

<default-interceptor-ref name="defaultStack"/>

</package>

</struts>

For the last action mapping for action action3 in Listing 4.3, there is no Interceptor or Interceptor stack defined. It does not mean that the request for this action is not going to be intercepted and pre-processed. When there is no Interceptor declared for an action mapping and our package extends the struts-default package, the default Interceptor stack is used. The default Interceptor stack is defaultStack, which is defined in struts-default.xml using <default-interceptor-ref name="defaultStack"/> element. We can override this definition by defining default Interceptor in our struts.xml file.

In document Struts2 Black Book (Page 114-119)