blob: 692eac89fe018d11562fd6c019fdae9a6157e59a [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<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="//,400,600,700,400italic,600italic,700italic" rel="stylesheet" type="text/css">
<link href="//" 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="//"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="/js/community.js"></script>
<a href="" class="github-ribbon">
<img style="position: absolute; right: 0; border: 0;" src="" alt="Fork me on GitHub">
<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">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<a href="/index.html" class="navbar-brand logo"><img src="/img/struts-logo.svg"></a>
<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>
<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-2021.html">Announcements</a></li>
<li><a href="">License</a></li>
<li><a href="">Thanks!</a></li>
<li><a href="">Sponsorship</a></li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Support<b class="caret"></b>
<ul class="dropdown-menu">
<li><a href="/mail.html">User Mailing List</a></li>
<li><a href="">Issue Tracker</a></li>
<li><a href="/security.html">Reporting Security Issues</a></li>
<li class="divider"></li>
<li><a href="">Version Notes</a></li>
<li><a href="">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>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Documentation<b class="caret"></b>
<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="">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="">FAQs</a></li>
<li><a href="">Plugin registry</a></li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Contributing<b class="caret"></b>
<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="">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="">Source Repository</a></li>
<li><a href="/updating-website.html">Updating the website</a></li>
<li class="apache"><a href=""><img src="/img/apache.png"></a></li>
<article class="container">
<section class="col-md-12">
<a class="edit-on-gh" href="" 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>
<p>The example code for this tutorial, <strong>interceptors</strong>, is available at <a href="">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>
<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>
<p>After the execute method is completed more work must be done</p>
<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>
<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="">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>
<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.
<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>
<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>
<td>Return to <a href="annotations.html">Annotations</a></td>
<td>onward to <a href="unit-testing.html">Unit Testing</a></td>
<footer class="container">
<div class="col-md-12">
Copyright &copy; 2000-2018 <a href="">The Apache Software Foundation </a>.
All Rights Reserved.
<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 class="col-md-12">Logo and website design donated by <a href="">SoftwareMill</a>.</div>
<script>!function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (!d.getElementById(id)) {
js = d.createElement(s); = id;
js.src = "//";
fjs.parentNode.insertBefore(js, fjs);
}(document, "script", "twitter-wjs");</script>
<script src="" 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); = id;
js.src = "//";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>