blob: 5d5ddbb0ca09a3d48245a41cf449565f768a05d5 [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>Portlet Plugin</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/plugins/portlet/index.md" title="Edit this page on GitHub">Edit on GitHub</a>
<a href="../" title="back to Plugins"><< back to Plugins</a>
<h1 id="portlet-plugin">Portlet Plugin</h1>
<h2 id="documentation">Documentation</h2>
<p>The Portlet Plugin is used for developing JSR286 portlets using Struts 2.</p>
<h3 id="portlet-class">portlet-class</h3>
<p>To use the Struts 2 Portlet framework, use <strong><em>org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher</em></strong> as the portlet class in your portlet.xml file:</p>
<p><strong>portlet.xml</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="nt">&lt;portlet-app</span> <span class="na">id=</span><span class="s">"my-portlet-app"</span>
<span class="na">xmlns=</span><span class="s">"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"</span>
<span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="na">version=</span><span class="s">"2.0"</span>
<span class="na">xsi:schemaLocation=</span><span class="s">"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"</span><span class="nt">&gt;</span>
<span class="nt">&lt;portlet</span> <span class="na">id=</span><span class="s">"MyPortlet"</span><span class="nt">&gt;</span>
<span class="nt">&lt;description</span> <span class="na">xml:lang=</span><span class="s">"EN"</span><span class="nt">&gt;</span>My Portlet<span class="nt">&lt;/description&gt;</span>
<span class="nt">&lt;portlet-name&gt;</span>MyPortlet<span class="nt">&lt;/portlet-name&gt;</span>
<span class="nt">&lt;display-name</span> <span class="na">xml:lang=</span><span class="s">"EN"</span><span class="nt">&gt;</span>my-portlet<span class="nt">&lt;/display-name&gt;</span>
<span class="nt">&lt;portlet-class&gt;</span>org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher<span class="nt">&lt;/portlet-class&gt;</span>
<span class="c">&lt;!-- SNIP --&gt;</span>
<span class="nt">&lt;/portlet&gt;</span>
<span class="nt">&lt;/portlet-app&gt;</span>
</code></pre></div></div>
<h3 id="portlet-init-parameters">Portlet Init Parameters</h3>
<p>Below is the init-param elements that you can set up in <em>portlet.xml</em> for configuring the portlet mode -&gt; xwork namespace mappings for the portlet. Basically, you can think of the different portlet modes as different sub-applications, so it can be useful to set up the <code class="highlighter-rouge">struts.xml</code> configuration with different namespaces for the different portlets and modes:</p>
<table>
<thead>
<tr>
<th>Key</th>
<th>Description</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td>portletNamespace</td>
<td>The namespace for the portlet in the action configuration. This namespace is prepended to all action lookups, and makes it possible to host multiple portlets in the same portlet application. If this parameter is set, the complete namespace will be <em>/portletNamespace/modeNamespace/actionName</em></td>
<td>The default namespace.</td>
</tr>
<tr>
<td>viewNamespace</td>
<td>The namespace in the xwork config for the view portlet mode.</td>
<td>The default namespace.</td>
</tr>
<tr>
<td>editNamespace</td>
<td>The namespace in the xwork config for the edit portlet mode.</td>
<td>The default namespace.</td>
</tr>
<tr>
<td>helpNamespace</td>
<td>The namespace in the xwork config for the help portlet mode.</td>
<td>The default namespace.</td>
</tr>
<tr>
<td>defaultViewAction</td>
<td>Name of the action to use as default for the view portlet mode, when no action name is present.</td>
<td>default</td>
</tr>
<tr>
<td>defaultEditAction</td>
<td>Name of the action to use as default for the edit portlet mode, when no action name is present.</td>
<td>default</td>
</tr>
<tr>
<td>defaultHelpAction</td>
<td>Name of the action to use as default for the help portlet mode, when no action name is present.</td>
<td>default</td>
</tr>
</tbody>
</table>
<p>e.g.</p>
<p><strong>portlet.xml</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;init-param&gt;</span>
<span class="c">&lt;!-- Portlet namespace --&gt;</span>
<span class="nt">&lt;name&gt;</span>portletNamespace<span class="nt">&lt;/name&gt;</span>
<span class="nt">&lt;value&gt;</span>/portletA<span class="nt">&lt;/value&gt;</span>
<span class="nt">&lt;/init-param&gt;</span>
<span class="nt">&lt;init-param&gt;</span>
<span class="c">&lt;!-- The base namespace of the view portlet mode --&gt;</span>
<span class="nt">&lt;name&gt;</span>viewNamespace<span class="nt">&lt;/name&gt;</span>
<span class="nt">&lt;value&gt;</span>/view<span class="nt">&lt;/value&gt;</span>
<span class="nt">&lt;/init-param&gt;</span>
<span class="nt">&lt;init-param&gt;</span>
<span class="c">&lt;!-- The default action to invoke in view mode --&gt;</span>
<span class="nt">&lt;name&gt;</span>defaultViewAction<span class="nt">&lt;/name&gt;</span>
<span class="nt">&lt;value&gt;</span>index<span class="nt">&lt;/value&gt;</span>
<span class="nt">&lt;/init-param&gt;</span>
</code></pre></div></div>
<p>This snippet from <code class="highlighter-rouge">portlet.xml</code> will set up the portlet with a namespace of <em>/portletA/</em>. This means that all requests to this portlet will get the namespace prepended when looking up the action. In addition, the _view_ portlet mode will map to the <em>/view</em> namespace, so a request for action <code class="highlighter-rouge">myAction</code> will resolve to the namespace and action <em>/portletA/view/myAction</em> . It also means that if no action is requested, the default action of <code class="highlighter-rouge">index</code> will be used for the request.</p>
<h3 id="webxml">web.xml</h3>
<p>If you want to access to expose the value stack through request attributes (e.g. if you want to use regular JSTL tags to access values in the stack), you have to configure the dispatcher servlet in your web application descriptor:</p>
<p><strong>web.xml</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;servlet</span> <span class="na">id=</span><span class="s">"Struts2PortletDispatcherServlet"</span><span class="nt">&gt;</span>
<span class="nt">&lt;servlet-name&gt;</span>Struts2PortletDispatcherServlet<span class="nt">&lt;/servlet-name&gt;</span>
<span class="nt">&lt;servlet-class&gt;</span>org.apache.struts2.portlet.dispatcher.DispatcherServlet<span class="nt">&lt;/servlet-class&gt;</span>
<span class="nt">&lt;/servlet&gt;</span>
</code></pre></div></div>
<p><strong>If you’re only using Struts 2 tags, configuring the dispatcher servlet is optional</strong></p>
<h3 id="portlet-phases">Portlet Phases</h3>
<p>The portlet specification describes that a portlet request cycle can consist of two phases, an <em>event</em> phase and a <em>render</em> phase. In case of an <em>event</em> in the portlet, it will always execute before the <em>render</em> phase. The <em>event</em> phase is typically for changing the state of the application. In a portlet, this is typically when a form is submitted. The <em>render</em> phase will then prepare and dispatch to the view. It’s recommended that you set up the result from an action that is executed in the <em>event</em> phase to point to another action that is executed in the <em>render</em> phase, which again is responsible for dispatching to the actual view.</p>
<h3 id="portlet-result-dispatching">Portlet Result Dispatching</h3>
<p>The <em>struts-portlet-default</em> package defines a special default Result Type, which is responsible for performing the result logic of an Action execution. Typically, this involves including a JSP for rendering, or preparing a render action if one is configured for the current event action.</p>
<p>This result type has three main modes of execution.</p>
<ul>
<li>
<p>If the Action is executed in the render phase, it will perform a PortletRequestDispatcher.include(req, res) to the resource specified in the <em>location</em> attribute.</p>
</li>
<li>
<p>If the Action is executed in the event phase, and the result is an action mapping, it will set a render parameter on the ActionResponse to indicate which Action should be executed in the render phase. This follows good web application design, which promotes the use of a redirect after an event, which in this case means that an Action executed in the event phase will be followed by a redirect to an Action executed in the render phase.</p>
</li>
<li>
<p>If the Action is executed in the event phase, and the result is not an action mapping, the result will prepare a special Action called “renderDirect” (specified in the <em>struts-portlet-default</em> package) whose sole purpose is to render the specified web resource (usually a JSP).</p>
</li>
</ul>
<h4 id="redirectaction">redirectAction</h4>
<p>The action executed in event mode can pass render parameters to the next action to execute in render mode through parameters using the <em>redirectAction</em> result type:</p>
<p><strong>struts.xml</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;result</span> <span class="na">type=</span><span class="s">"redirectAction"</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">&gt;</span>
<span class="nt">&lt;param</span> <span class="na">name=</span><span class="s">"actionName"</span><span class="nt">&gt;</span>displayCart<span class="nt">&lt;/param&gt;</span>
<span class="nt">&lt;param</span> <span class="na">name=</span><span class="s">"userId"</span><span class="nt">&gt;</span>${userId}<span class="nt">&lt;/param&gt;</span>
<span class="c">&lt;!-- If you want to redirect to a different portlet mode, use the portletMode parameter
&lt;param name="portletMode"&gt;view&lt;/param&gt;
--&gt;</span>
<span class="nt">&lt;/result&gt;</span>
</code></pre></div></div>
<p>This will set up a <em>render parameter</em> called <em>userId</em> with the value of the <em>userId</em> property on the value stack.</p>
<p>You can also use the <em>portletMode</em> parameter to change to a different portlet mode.</p>
<h3 id="surl-and-sform-tags">s:url and s:form tags</h3>
<p>URLs in a portlet is handled quite different than for a regular web application. There’s no such thing as “extension”, there’s simply an action name and a namespace, and they have to be specified using the respective tag attributes. URLs using the <em>value</em> attribute will be encoded as a resource URL and is only suitable for additional resources such as images and style sheets.</p>
<p>Example:</p>
<table>
<thead>
<tr>
<th>Servlet</th>
<th>Portlet</th>
</tr>
</thead>
<tbody>
<tr>
<td>&lt;s:url value=”hello.action”/&gt;</td>
<td>&lt;s:url action=”hello”/&gt;</td>
</tr>
<tr>
<td>&lt;s:url value=”style.css”/&gt;</td>
<td>&lt;s:url value=”style.css”/&gt;</td>
</tr>
</tbody>
</table>
<p>In addition, there are some additional tag attributes that are portlet specific. These are:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>portletMode</td>
<td>The resulting portlet mode of the url</td>
</tr>
<tr>
<td>windowState</td>
<td>The resulting window state of the url</td>
</tr>
<tr>
<td>portletUrlType</td>
<td>Specifies if this is a <em>render</em> or an <em>action</em> url</td>
</tr>
</tbody>
</table>
<p>Typical usage for the <em>portletUrlType</em> is if you have a <em>delete</em> link that removes something from a database. To ensure this is done in the event phase, as recommended by the portlet specification (since it is a change of state), the url can use this attribute, e.g. &lt;s:url action=”deleteEntry” portletUrlType=”action”/&gt;</p>
<h3 id="security">Security</h3>
<p>Basically the Struts2 Portlet Plugin doesn’t support different auth level in the same portlet. If you want to achieve that you must create two separated portlets and configure access level with portlet engine for each of them.</p>
<h3 id="other-resources">Other resources</h3>
<ul>
<li><a href="struts-2-portlet-tutorial.html">Struts 2 Portlet Tutorial</a></li>
<li><a href="http://jcp.org/aboutJava/communityprocess/final/jsr168/index.html">JSR168 Specification</a></li>
<li><a href="http://jcp.org/aboutJava/communityprocess/final/jsr286/index.html">JSR286 Specification</a></li>
</ul>
</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>