blob: 9e91bc9b80f70fc9a5905ee05f58a636975a0891 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="Date-Revision-yyyymmdd" content="20140918"/>
<meta http-equiv="Content-Language" content="en"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Introducing Interceptors</title>
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,400italic,600italic,700italic" rel="stylesheet" type="text/css">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<link href="/css/main.css" rel="stylesheet">
<link href="/css/custom.css" rel="stylesheet">
<link href="/highlighter/github-theme.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="/js/community.js"></script>
</head>
<body>
<a href="http://github.com/apache/struts" class="github-ribbon">
<img style="position: absolute; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
</a>
<header>
<nav>
<div role="navigation" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" data-toggle="collapse" data-target="#struts-menu" class="navbar-toggle">
Menu
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/index.html" class="navbar-brand logo"><img src="/img/struts-logo.svg"></a>
</div>
<div id="struts-menu" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Home<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/index.html">Welcome</a></li>
<li><a href="/download.cgi">Download</a></li>
<li><a href="/releases.html">Releases</a></li>
<li><a href="/announce.html">Announcements</a></li>
<li><a href="http://www.apache.org/licenses/">License</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks!</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Support<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/mail.html">User Mailing List</a></li>
<li><a href="https://issues.apache.org/jira/browse/WW">Issue Tracker</a></li>
<li><a href="/security.html">Reporting Security Issues</a></li>
<li class="divider"></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Migration+Guide">Version Notes</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Security+Bulletins">Security Bulletins</a></li>
<li class="divider"></li>
<li><a href="/maven/project-info.html">Maven Project Info</a></li>
<li><a href="/maven/struts2-core/dependencies.html">Struts Core Dependencies</a></li>
<li><a href="/maven/struts2-plugins/modules.html">Plugin Dependencies</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Documentation<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/birdseye.html">Birds Eye</a></li>
<li><a href="/primer.html">Key Technologies</a></li>
<li><a href="/kickstart.html">Kickstart FAQ</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Home">Wiki</a></li>
<li class="divider"></li>
<li><a href="/getting-started/">Getting Started</a></li>
<li><a href="/security/">Security Guide</a></li>
<li><a href="/core-developers/">Core Developers Guide</a></li>
<li><a href="/tag-developers/">Tag Developers Guide</a></li>
<li><a href="/maven-archetypes/">Maven Archetypes</a></li>
<li><a href="/plugins/">Plugins</a></li>
<li><a href="/maven/struts2-core/apidocs/index.html">Struts Core API</a></li>
<li><a href="/tag-developers/tag-reference.html">Tag reference</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/FAQs">FAQs</a></li>
<li><a href="http://cwiki.apache.org/S2PLUGINS/home.html">Plugin registry</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Contributing<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/youatstruts.html">You at Struts</a></li>
<li><a href="/helping.html">How to Help FAQ</a></li>
<li><a href="/dev-mail.html">Development Lists</a></li>
<li><a href="/contributors/">Contributors Guide</a></li>
<li class="divider"></li>
<li><a href="/submitting-patches.html">Submitting patches</a></li>
<li><a href="/builds.html">Source Code and Builds</a></li>
<li><a href="/coding-standards.html">Coding standards</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Contributors+Guide">Contributors Guide</a></li>
<li class="divider"></li>
<li><a href="/release-guidelines.html">Release Guidelines</a></li>
<li><a href="/bylaws.html">PMC Charter</a></li>
<li><a href="/volunteers.html">Volunteers</a></li>
<li><a href="https://gitbox.apache.org/repos/asf?p=struts.git">Source Repository</a></li>
<li><a href="/updating-website.html">Updating the website</a></li>
</ul>
</li>
<li class="apache"><a href="http://www.apache.org/"><img src="/img/apache.png"></a></li>
</ul>
</div>
</div>
</div>
</nav>
</header>
<article class="container">
<section class="col-md-12">
<a class="edit-on-gh" href="https://github.com/apache/struts-site/edit/master/source/getting-started/introducing-interceptors.md" title="Edit this page on GitHub">Edit on GitHub</a>
<a href="index.html" title="back to Getting started"><< back to Getting started</a>
<h1 class="no_toc" id="introducing-interceptors">Introducing Interceptors</h1>
<ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#introducing-interceptors-1" id="markdown-toc-introducing-interceptors-1">Introducing Interceptors</a></li>
<li><a href="#run-the-example" id="markdown-toc-run-the-example">Run The Example</a></li>
<li><a href="#create-your-own-interceptor" id="markdown-toc-create-your-own-interceptor">Create Your Own Interceptor</a></li>
<li><a href="#summary" id="markdown-toc-summary">Summary</a></li>
</ul>
<p>The example code for this tutorial, <strong>interceptors</strong>, is available at <a href="https://github.com/apache/struts-examples">struts-examples</a></p>
<h2 id="introduction">Introduction</h2>
<p>So far our tutorials have not delved into the inner workings of the Struts 2 framework. But in this tutorial we’ll
introduce a key set of classes the Struts 2 framework relies upon to do most of the work whenever an Action is executed.
In this tutorial’s example project there is a register link that is mapped in the Struts XML configuration file (<code class="highlighter-rouge">struts.xml</code>)
to the execute method of class Register. Before that execute method is called much work is done behind the scenes
by the Struts 2 framework. For example:</p>
<ol>
<li>Handling any exceptions generated</li>
<li>Converting the String request parameters to the Register class’s instance fields where the name values match</li>
<li>Calling the validate method and/or the external XML validation</li>
</ol>
<p>After the execute method is completed more work must be done</p>
<ol>
<li>Handling any exceptions generated</li>
<li>Converting the <code class="highlighter-rouge">Register</code> class’s instance fields to String values for display in the view page</li>
<li>Forwarding to the correct view page depending on the result String returned by the execute method</li>
</ol>
<p>The above list of tasks are not complete - several other tasks are done before and after the execution of the Action.</p>
<p>The benefit of using Struts 2 is all this work happens automatically. You can focus on the logic of the controller
(the Struts 2 ActionSupport class), the Service layer, the data access layer, your domain models, etc.</p>
<p>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a> is an excellent place to get help. If you are
having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find
an answer to your problem, post a question on the mailing list.</p>
<h2 id="introducing-interceptors-1">Introducing Interceptors</h2>
<p>The tasks that are done by the Struts 2 framework before and after an Action is executed are done by Struts 2 interceptors.
Interceptors are standard Java classes included in the Struts 2 core jar which are executed in a specific order.</p>
<p>In our example application there is a package node in struts.xml. The package node has an attribute of extends with
a value of “struts-default.” The value “struts-default” identifies to the framework the specific stack of interceptors
that will be executed before and after the Actions in that package.</p>
<p>If you want to learn more about the inner workings of interceptors, what interceptors belong to the struts default stack,
and what are all the interceptors included with Struts 2, visit <a href="../core-developers/interceptors.html">Understanding Interceptors</a> .</p>
<p>Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want
to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire
package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is
how you would specify that the register Action should use both the <a href="../core-developers/logging-interceptor.html">logger</a>
and <a href="../core-developers/timer-interceptor.html">timer</a> interceptors in addition to the interceptors provided by the default stack.</p>
<p><strong>Specify Specific Interceptors For An Action</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;action</span> <span class="na">name=</span><span class="s">"register"</span> <span class="na">class=</span><span class="s">"org.apache.struts.register.action.Register"</span> <span class="na">method=</span><span class="s">"execute"</span><span class="nt">&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"timer"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"logger"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span><span class="nt">&gt;</span>
<span class="nt">&lt;param</span> <span class="na">name=</span><span class="s">"exception.logEnabled"</span><span class="nt">&gt;</span>true<span class="nt">&lt;/param&gt;</span>
<span class="nt">&lt;param</span> <span class="na">name=</span><span class="s">"exception.logLevel"</span><span class="nt">&gt;</span>ERROR<span class="nt">&lt;/param&gt;</span>
<span class="nt">&lt;/interceptor-ref&gt;</span>
<span class="nt">&lt;result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">&gt;</span>thankyou.jsp<span class="nt">&lt;/result&gt;</span>
<span class="nt">&lt;result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">&gt;</span>register.jsp<span class="nt">&lt;/result&gt;</span>
<span class="nt">&lt;/action&gt;</span>
</code></pre></div></div>
<p>The logger interceptor logs the start and end of the execution of an Action. The timer interceptor logs the amount
of time (in milliseconds) for execution of the Action. These two interceptors used together can provide developers
useful feedback.</p>
<p>In the code example above note the three interceptor-ref nodes. Each one has a value for the name attribute. For the register
Action we are instructing the framework to use the <code class="highlighter-rouge">timer</code>, <code class="highlighter-rouge">logger</code>, and <code class="highlighter-rouge">defaultStack</code> interceptors. The <code class="highlighter-rouge">defaultStack</code>
are all the interceptors normally executed for an Action.</p>
<p>How did I know to use the value of timer for the name attribute and even that there is a timer interceptor?
On the <a href="../core-developers/interceptors.html">Interceptors</a> web page in the Struts 2 documentation are a list of interceptors
that come with the Struts 2 framework and what the name value is for each interceptor.</p>
<p>How did I know that the timer interceptor isn’t part of the defaultStack of interceptors already? Again on the Interceptors
documentation web page is a list of which interceptors belong to the <code class="highlighter-rouge">defaultStack</code>.</p>
<p>Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods
of the <a href="../core-developers/exception-interceptor.html">Exception Interceptor</a>. Providing the values of true and ERROR
will cause the Struts 2 framework to log any exceptions not caught by the application’s code and to log those exceptions
at the ERROR level.</p>
<h2 id="run-the-example">Run The Example</h2>
<p>In the example application follow the README instructions to build, deploy, and run the application. View the output
sent to the JVM console to see the log messages generated by the logger and timer interceptors. You should see log messages
similar to the following:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>INFO: Starting execution stack for action //register
Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Finishing execution stack for action //register
Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
INFO: Executed action /register!execute took 177 ms.
</code></pre></div></div>
<p>If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use
the following in <code class="highlighter-rouge">struts.xml</code>:</p>
<p><strong>Specify Specific Interceptors For A Package</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;package</span> <span class="na">name=</span><span class="s">"basicstruts2"</span> <span class="na">extends=</span><span class="s">"struts-default"</span> <span class="nt">&gt;</span>
<span class="nt">&lt;interceptors&gt;</span>
<span class="nt">&lt;interceptor-stack</span> <span class="na">name=</span><span class="s">"appDefault"</span><span class="nt">&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"timer"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"logger"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/interceptor-stack&gt;</span>
<span class="nt">&lt;/interceptors&gt;</span>
<span class="nt">&lt;default-interceptor-ref</span> <span class="na">name=</span><span class="s">"appDefault"</span> <span class="nt">/&gt;</span>
<span class="c">&lt;!-- rest of package omitted --&gt;</span>
<span class="nt">&lt;/package&gt;</span>
</code></pre></div></div>
<p>In the code above we use the interceptors node to define a new stack of interceptors that includes the <code class="highlighter-rouge">timer</code>, <code class="highlighter-rouge">logger</code>,
and <code class="highlighter-rouge">defaultStack</code> interceptors. We give this new interceptor stack a name of appDefault. Then we use the <code class="highlighter-rouge">default-interceptor-ref</code>
node to specify that for all Actions defined inside this package node the <code class="highlighter-rouge">appDefault</code> stack of interceptors are to be used.
Thus the <code class="highlighter-rouge">timer</code> and <code class="highlighter-rouge">logger</code> interceptor will be executed for each Action in this package.</p>
<p>Note that in both examples we are still executing all the other interceptors by including the defaultStack as one
of the <code class="highlighter-rouge">interceptor-ref</code> nodes. When you specify what interceptors you want to use for an Action or a package then only
those interceptors are executed. So if in the example we had left out the <code class="highlighter-rouge">interceptor-ref</code> for <code class="highlighter-rouge">defaultStack</code> only
the <code class="highlighter-rouge">logger</code> and <code class="highlighter-rouge">timer</code> interceptors would have executed.</p>
<h2 id="create-your-own-interceptor">Create Your Own Interceptor</h2>
<p>In addition to specifying your own stack of interceptors, you can also write your own new interceptor and add it to
the stack that is executed. The Struts <a href="../core-developers/writing-interceptors.html">Writing Interceptors</a> guide explains
how to do this. For example, you could create your own interceptor to handle authentication and authorization.</p>
<h2 id="summary">Summary</h2>
<p>Interceptors provide the Struts 2 framework with both power and flexibility. Developers may add additional interceptors
(either ones provided by Struts 2 or ones they create) to the stack of interceptors executed when an Action class is called.</p>
<table>
<tbody>
<tr>
<td>Return to <a href="annotations.html">Annotations</a></td>
<td>or</td>
<td>onward to <a href="unit-testing.html">Unit Testing</a></td>
</tr>
</tbody>
</table>
</section>
</article>
<footer class="container">
<div class="col-md-12">
Copyright &copy; 2000-2018 <a href="http://www.apache.org/">The Apache Software Foundation </a>.
All Rights Reserved.
</div>
<div class="col-md-12">
Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are
trademarks of The Apache Software Foundation.
</div>
<div class="col-md-12">Logo and website design donated by <a href="https://softwaremill.com/">SoftwareMill</a>.</div>
</footer>
<script>!function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = "//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");</script>
<script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script>
<div id="fb-root"></div>
<script>(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
</html>