Source

webwork / docs / wikidocs / TutorialLesson05.html

Full commit
<html>
    <head>
        <title>WebWork 2 : TutorialLesson05</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            WebWork 2 : TutorialLesson05
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Sep 16, 2004 by <font color="#0050B2">vitorsouza</font>.
				    </div>

				    <p class="paragraph"><h1 style="margin: 4px 0px 4px 0px;" class="heading1"><a name="TutorialLesson05-Lesson5%3AInterceptors"> Lesson 5: Interceptors</a></h1></p>Interceptors allow arbitrary code to be included in the call stack for your action before and/or after processing the action, which can vastly simplify your code itself and provide excellent opportunities for code reuse. Many of the features of XWork and WebWork are implemented as interceptors and can be applied via external configuration along with your own Interceptors in whatever order you specify for any set of actions you define.<p class="paragraph">In other words, when you access a <tt class="monospaced">&#42;.action</tt> URL, WebWork&#039;s <tt class="monospaced">ServletDispatcher</tt> proceeds to the invocation of the an action object. Before it is executed, however, the invocation can be intercepted by another object, that is hence called interceptor. To have an interceptor executed before (or after) a given action, just configure <tt class="monospaced">xwork.xml</tt> properly, like the example below, taken from <a href="TutorialLesson04-01-01.html" title="TutorialLesson04-01-01">lesson 4.1.1</a>:</p><h3 class="heading3"><a name="TutorialLesson05-Interceptorconfigurationfromlesson4.1.1%3A"> Interceptor configuration from lesson 4.1.1:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;formProcessing&quot;</span> class=<span class="xml&#45;quote">&quot;lesson04&#95;01&#95;01.FormProcessingAction&quot;</span>&gt;</span> &#10;	<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;input&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>ex01&#45;index.jsp<span class="xml&#45;tag">&lt;/result&gt;</span> &#10;	<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>ex01&#45;success.jsp<span class="xml&#45;tag">&lt;/result&gt;</span> &#10;	<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;validationWorkflowStack&quot;</span> /&gt;</span> &#10;<span class="xml&#45;tag">&lt;/action&gt;</span></pre>
</div></div><p class="paragraph">As you can see, lesson 4.1.1&#039;s <tt class="monospaced">formProcessing</tt> Action uses the <tt class="monospaced">validationWorkflowStack</tt>. That is an interceptor stack, which organizes a bunch of interceptors in the order in which they are to be executed. That stack is configured in <tt class="monospaced">webwork-default.xml</tt>, so all we have to do to use it is declare a <tt class="monospaced">&lt;interceptor-ref /&gt;</tt> under the action configuration or a <tt class="monospaced">&lt;default-interceptor-ref /&gt;</tt>, under package configuration, as seen in <a href="TutorialLesson03.html" title="TutorialLesson03">lesson 3</a>&#039;s first example:</p><h3 class="heading3"><a name="TutorialLesson05-Interceptorconfigurationfromlesson3.1%3A"> Interceptor configuration from lesson 3.1:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#33;DOCTYPE xwork PUBLIC <span class="xml&#45;quote">&quot;&#45;//OpenSymphony Group//XWork 1.0//EN&quot;</span> &#10;<span class="xml&#45;quote">&quot;&#104;ttp://www.opensymphony.com/xwork/xwork&#45;1.0.dtd&quot;</span>&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;xwork&gt;</span>&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Include webwork defaults (from WebWork JAR). &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;webwork&#45;default.xml&quot;</span> /&gt;</span>&#10;	&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Configuration for the default package. &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;package name=<span class="xml&#45;quote">&quot;default&quot;</span> extends=<span class="xml&#45;quote">&quot;webwork&#45;default&quot;</span>&gt;</span>&#10;		<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Default interceptor stack. &#45;&#45;&gt;</span></span> &#10;		<span class="xml&#45;tag">&lt;default&#45;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span> /&gt;</span> &#10;		&#10;		<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Action: Lesson 03: HelloWebWorldAction. &#45;&#45;&gt;</span></span> &#10;		<span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;helloWebWorld&quot;</span> class=<span class="xml&#45;quote">&quot;lesson03.HelloWebWorldAction&quot;</span>&gt;</span> &#10;			<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;dispatcher&quot;</span>&gt;</span>ex01&#45;success.jsp<span class="xml&#45;tag">&lt;/result&gt;</span> &#10;		<span class="xml&#45;tag">&lt;/action&gt;</span> &#10;	<span class="xml&#45;tag">&lt;/package&gt;</span>&#10;<span class="xml&#45;tag">&lt;/xwork&gt;</span></pre>
</div></div><p class="paragraph">But let&#039;s see how it works from scracth:</p><ol>
<li> Create an interceptor class, which is a class that implements the <tt class="monospaced">com.opensymphony.xwork.interceptor.Interceptor</tt> interface (bundled in <tt class="monospaced">xwork-1.0.jar</tt>);</li>
<li> Declare the class in your XML configuration file (<tt class="monospaced">xwork.xml</tt>) using the element <tt class="monospaced">&lt;interceptor /&gt;</tt> nested within <tt class="monospaced">&lt;interceptors /&gt;</tt>;</li>
<li> Create stacks of interceptors, using the <tt class="monospaced">&lt;interceptor-stack /&gt;</tt> element (<em class="emphasis">optional</em>);</li>
<li> Determine which interceptors are used by which action, using <tt class="monospaced">&lt;interceptor-ref /&gt;</tt> or <tt class="monospaced">&lt;default-interceptor-ref /&gt;</tt>. The former defines the interceptors to be used in a specific action, while the latter determines the default interceptor stack to be used by all actions that do not specify their own <tt class="monospaced">&lt;interceptor-ref /&gt;</tt>.</li>
</ol><br/>
Looking inside <tt class="monospaced">webwork-default.xml</tt> we can see how it&#039;s done:<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson05-webworkdefault.xml%3A"> webwork-default.xml:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#33;DOCTYPE xwork PUBLIC <span class="xml&#45;quote">&quot;&#45;//OpenSymphony Group//XWork 1.0//EN&quot;</span> &#10;<span class="xml&#45;quote">&quot;&#104;ttp://www.opensymphony.com/xwork/xwork&#45;1.0.dtd&quot;</span>&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;xwork&gt;</span>&#10;	<span class="xml&#45;tag">&lt;package name=<span class="xml&#45;quote">&quot;webwork&#45;default&quot;</span>&gt;</span>&#10;		<span class="xml&#45;tag">&lt;result&#45;types&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;dispatcher&quot;</span> default=<span class="xml&#45;quote">&quot;true&quot;</span>&#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.dispatcher.ServletDispatcherResult&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;redirect&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.dispatcher.ServletRedirectResult&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;velocity&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.dispatcher.VelocityResult&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;chain&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.ActionChainResult&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;result&#45;type name=<span class="xml&#45;quote">&quot;xslt&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.views.xslt.XSLTResult&quot;</span>/&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/result&#45;types&gt;</span>&#10;	&#10;		<span class="xml&#45;tag">&lt;interceptors&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;timer&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.TimerInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;logger&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.LoggingInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;chain&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.ChainingInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;static&#45;params&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.StaticParametersInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;params&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.ParametersInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;model&#45;driven&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.ModelDrivenInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;component&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.component.ComponentInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;token&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.interceptor.TokenInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;token&#45;session&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.interceptor.TokenSessionStoreInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;validation&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.validator.ValidationInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;workflow&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;servlet&#45;config&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.interceptor.ServletConfigInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;prepare&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.xwork.interceptor.PrepareInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;conversionError&quot;</span> &#10;				class=<span class="xml&#45;quote">&quot;com.opensymphony.webwork.interceptor.WebWorkConversionErrorInterceptor&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor&#45;stack name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;static&#45;params&quot;</span>/&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;params&quot;</span>/&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;conversionError&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;/interceptor&#45;stack&gt;</span>&#10;			<span class="xml&#45;tag">&lt;interceptor&#45;stack name=<span class="xml&#45;quote">&quot;validationWorkflowStack&quot;</span>&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;defaultStack&quot;</span>/&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;validation&quot;</span>/&gt;</span>&#10;				<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;workflow&quot;</span>/&gt;</span>&#10;			<span class="xml&#45;tag">&lt;/interceptor&#45;stack&gt;</span>&#10;		<span class="xml&#45;tag">&lt;/interceptors&gt;</span>&#10;	<span class="xml&#45;tag">&lt;/package&gt;</span>&#10;<span class="xml&#45;tag">&lt;/xwork&gt;</span></pre>
</div></div></p>Since we included <tt class="monospaced">webwork-default.xml</tt> in our <tt class="monospaced">xwork.xml</tt>, all the interceptors and stacks above are available for us to use in our actions. Here&#039;s what these interceptors do:<br/>

<ul class="star">
<li> <b class="strong">timer</b>: clocks how long the action (including nested interceptors and view) takes to execute;</li>
<li> <b class="strong">logger</b>: logs the action being executed;</li>
<li> <b class="strong">chain</b>: makes the previous action&#039;s properties available to the current action. Used to make action chaining (reference: <a href="Result Types.html" title="Result Types">Result Types</a>);</li>
<li> <b class="strong">static-params</b>: sets the parameters defined in <tt class="monospaced">xwork.xml</tt> onto the action. These are the <tt class="monospaced">&lt;param /&gt;</tt> tags that are direct children of the <tt class="monospaced">&lt;action /&gt;</tt> tag;</li>
<li> <b class="strong">params</b>: sets the request (POST and GET) parameters onto the action class. We have seen an example of this in <a href="TutorialLesson03.html" title="TutorialLesson03">lesson 3</a>;</li>
<li> <b class="strong">model-driven</b>: if the action implements <tt class="monospaced">ModelDriven</tt>, pushes the <tt class="monospaced">getModel()</tt> result onto the Value Stack;</li>
<li> <b class="strong">component</b>: enables and makes registered components available to the actions. (reference: <span class="error">&#91;IoC &amp; Components&#93;</span>);</li>
<li> <b class="strong">token</b>: checks for valid token presence in action, prevents duplicate form submission;</li>
<li> <b class="strong">token-session</b>: same as above, but storing the submitted data in session when handed an invalid token;</li>
<li> <b class="strong">validation</b>: performs validation using the validators defined in <tt class="monospaced">&#123;Action&#125;-validation.xml</tt> (reference: <a href="Validation.html" title="Validation">Validation</a>). We&#039;ve seen an example of this in <a href="TutorialLesson04-01-01.html" title="TutorialLesson04-01-01">lesson 4.1.1</a>;</li>
<li> <b class="strong">workflow</b>: calls the validate method in your action class. If action errors created then it returns the <tt class="monospaced">INPUT</tt> view. Good to use together with the validation interceptor (reference: <a href="Validation.html" title="Validation">Validation</a>);</li>
<li> <b class="strong">servlet-config</b>: give access to <tt class="monospaced">HttpServletRequest</tt> and <tt class="monospaced">HttpServletResponse</tt> (think twice before using this since this ties you to the Servlet API);</li>
<li> <b class="strong">prepare</b>: allows you to programmatic access to your Action class before the parameters are set on it.;</li>
<li> <b class="strong">conversionError</b>: <font color="red"><em class="emphasis">help needed here</em></font>.</li>
</ul><p class="paragraph"><h2 class="heading2"><a name="TutorialLesson05-BuildingyourownInterceptor"> Building your own Interceptor</a></h2></p>If none of the above interceptors suit your particular need, you will have to implement your own interceptor. Fortunately, this is an easy task to accomplish. Suppose we need an interceptor that places a greeting in the Session according to the time of the day (morning, afternoon or evening). Here&#039;s how we could implement it:<p class="paragraph"><h3 class="heading3"><a name="TutorialLesson05-GreetingInterceptor.java%3A"> GreetingInterceptor.java:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">package</span> lesson05;&#10;&#10;<span class="java&#45;keyword">import</span> java.util.Calendar;&#10;<span class="java&#45;keyword">import</span> com.opensymphony.xwork.interceptor.Interceptor;&#10;<span class="java&#45;keyword">import</span> com.opensymphony.xwork.ActionInvocation;&#10;&#10;<span class="java&#45;keyword">public</span> class GreetingInterceptor <span class="java&#45;keyword">implements</span> Interceptor &#123;&#10;	<span class="java&#45;keyword">public</span> void init() &#123; &#125;&#10;	<span class="java&#45;keyword">public</span> void destroy() &#123; &#125;&#10;	<span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">String</span> intercept(ActionInvocation invocation) <span class="java&#45;keyword">throws</span> Exception &#123;&#10;		Calendar calendar = Calendar.getInstance();&#10;		<span class="java&#45;object">int</span> hour = calendar.get(Calendar.HOUR&#95;OF&#95;DAY);&#10;		<span class="java&#45;object">String</span> greeting = (hour &lt; 6) &#63; <span class="java&#45;quote">&quot;Good evening&quot;</span> : &#10;			((hour &lt; 12) &#63; <span class="java&#45;quote">&quot;Good morning&quot;</span>: &#10;			((hour &lt; 18) &#63; <span class="java&#45;quote">&quot;Good afternoon&quot;</span>: <span class="java&#45;quote">&quot;Good evening&quot;</span>));&#10;&#10;		invocation.getInvocationContext().getSession().put(<span class="java&#45;quote">&quot;greeting&quot;</span>, greeting);&#10;&#10;		<span class="java&#45;object">String</span> result = invocation.invoke();&#10;&#10;		<span class="java&#45;keyword">return</span> result;&#10;	&#125;&#10;&#125;</pre>
</div></div></p><h3 class="heading3"><a name="TutorialLesson05-xwork.xml%3A"> xwork.xml:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="xml&#45;tag">&lt;&#33;DOCTYPE xwork PUBLIC <span class="xml&#45;quote">&quot;&#45;//OpenSymphony Group//XWork 1.0//EN&quot;</span> &#10;<span class="xml&#45;quote">&quot;&#104;ttp://www.opensymphony.com/xwork/xwork&#45;1.0.dtd&quot;</span>&gt;</span>&#10;&#10;<span class="xml&#45;tag">&lt;xwork&gt;</span>&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Include webwork defaults (from WebWork JAR). &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;include file=<span class="xml&#45;quote">&quot;webwork&#45;default.xml&quot;</span> /&gt;</span>&#10;	&#10;	<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Configuration for the default package. &#45;&#45;&gt;</span></span>&#10;	<span class="xml&#45;tag">&lt;package name=<span class="xml&#45;quote">&quot;default&quot;</span> extends=<span class="xml&#45;quote">&quot;webwork&#45;default&quot;</span>&gt;</span>&#10;		<span class="xml&#45;tag">&lt;interceptors&gt;</span> &#10;			<span class="xml&#45;tag">&lt;interceptor name=<span class="xml&#45;quote">&quot;greeting&quot;</span> class=<span class="xml&#45;quote">&quot;section02.lesson05.GreetingInterceptor&quot;</span> /&gt;</span> &#10;		<span class="xml&#45;tag">&lt;/interceptors&gt;</span> &#10;		&#10;		<span class="xml&#45;tag"><span class="xml&#45;comment">&lt;&#33;&#45;&#45; Action: Lesson 5: GreetingInterceptor. &#45;&#45;&gt;</span></span> &#10;		<span class="xml&#45;tag">&lt;action name=<span class="xml&#45;quote">&quot;greetingAction&quot;</span> class=<span class="xml&#45;quote">&quot;lesson05.GreetingAction&quot;</span>&gt;</span> &#10;			<span class="xml&#45;tag">&lt;result name=<span class="xml&#45;quote">&quot;success&quot;</span> type=<span class="xml&#45;quote">&quot;velocity&quot;</span>&gt;</span>ex01&#45;result.vm<span class="xml&#45;tag">&lt;/result&gt;</span> &#10;			<span class="xml&#45;tag">&lt;interceptor&#45;ref name=<span class="xml&#45;quote">&quot;greeting&quot;</span> /&gt;</span> &#10;		<span class="xml&#45;tag">&lt;/action&gt;</span> &#10;	<span class="xml&#45;tag">&lt;/package&gt;</span>&#10;<span class="xml&#45;tag">&lt;/xwork&gt;</span></pre>
</div></div><p class="paragraph"><h3 class="heading3"><a name="TutorialLesson05-GreetingAction.java%3A"> GreetingAction.java:</a></h3>
<div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">package</span> lesson05;&#10;&#10;<span class="java&#45;keyword">import</span> com.opensymphony.xwork.ActionSupport;&#10;&#10;<span class="java&#45;keyword">public</span> class GreetingAction <span class="java&#45;keyword">extends</span> ActionSupport &#123;&#10;	<span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">String</span> execute() <span class="java&#45;keyword">throws</span> Exception &#123;&#10;		<span class="java&#45;keyword">return</span> SUCCESS;&#10;	&#125;&#10;&#125;</pre>
</div></div></p><h3 class="heading3"><a name="TutorialLesson05-ex01result.vm%3A"> ex01-result.vm:</a></h3>
<div class="code"><div class="codeContent">
<pre>&lt;html&gt;&#10;&lt;head&gt;&#10;&lt;title&gt;WebWork Tutorial &#45; Lesson 5 &#45; Example 1&lt;/title&gt;&#10;&lt;/head&gt;&#10;&lt;body&gt;&#10;&#10;#set ($ses = $req.getSession())&#10;&lt;p&gt;&lt;b&gt;$&#123;ses.getAttribute(&#039;greeting&#039;)&#125;&#33;&lt;/b&gt;&lt;/p&gt;&#10;&#10;&lt;/body&gt;&#10;&lt;/html&gt;</pre>
</div></div><p class="paragraph">Let&#039;s take a look at our interceptor class first. As explained before, the interceptor must implement <tt class="monospaced">com.opensymphony.xwork.interceptor.Interceptor</tt>&#039;s methods: <tt class="monospaced">init()</tt>, called during interceptor initialization, <tt class="monospaced">destroy()</tt>, called during destruction, and most importantly, <tt class="monospaced">intercept(ActionInvocation invocation)</tt>, which is where we place the code that does the work.</p>Notice that our interceptor returns the result from <tt class="monospaced">invocation.invoke()</tt> which is the method responsible for executing the next interceptor in the stack or, if this is the last one, the action. This means that the interceptor has the power of short-circuiting the action invocation and return a result string without executing the action at all! Use this with caution, though.<p class="paragraph">One other thing that interceptors can do is execute code after the action has executed. To do that, just place code after the invocation.invoke() call. WebWork provides an abstract class that already implements this kind of behaviour: <tt class="monospaced">com.opensymphony.xwork.interceptor.AroundInterceptor</tt>. Just extend it and implement the methods <tt class="monospaced">before(ActionInvocation invocation)</tt> and <tt class="monospaced">after(ActionInvocation dispatcher, String result)</tt>.</p>The <tt class="monospaced">xwork.xml</tt> configuration, the action class and the result page are pretty straightforward and require no further explanation.<br/>

<blockquote class="blockquote"> <a href="TutorialExamples.html" title="TutorialExamples">Try the example!</a></blockquote>
<p class="paragraph"><hr class="line"/></p><a href="TutorialLesson04.html" title="TutorialLesson04">Previous Lesson</a> | <a href="TutorialEnd.html" title="TutorialEnd">End of Tutorial</a><br/>



				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Oct 18, 2004 00:08</font></td>
		    </tr>
	    </table>
    </body>
</html>