| <!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>Nutshell</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> |
| |
| <!-- Matomo --> |
| <script> |
| var _paq = window._paq = window._paq || []; |
| /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ |
| /* We explicitly disable cookie tracking to avoid privacy issues */ |
| _paq.push(['disableCookies']); |
| _paq.push(['trackPageView']); |
| _paq.push(['enableLinkTracking']); |
| (function() { |
| var u="//analytics.apache.org/"; |
| _paq.push(['setTrackerUrl', u+'matomo.php']); |
| _paq.push(['setSiteId', '41']); |
| var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| })(); |
| </script> |
| <!-- End Matomo Code --> |
| </head> |
| <body> |
| |
| <a href="https://github.com/apache/struts" class="github-ribbon"> |
| <img decoding="async" loading="lazy" style="position: absolute; right: 0; border: 0;" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_red_aa0000.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1"> |
| </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-2023.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> |
| <li><a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</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><a href="/commercial-support.html">Commercial Support</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 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/">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 href="index.html" title="back to Core Developers Guide"><< back to Core Developers Guide</a> |
| <a class="edit-on-gh" href="https://github.com/apache/struts-site/edit/master/source/core-developers/nutshell.md" title="Edit this page on GitHub">Edit on GitHub</a> |
| <h1 id="nutshell">Nutshell</h1> |
| |
| <p>The framework documentation is written for active web developers and assumes a working knowledge about how |
| Java web applications are built. For more about the underlying nuts and bolts, see |
| the <a href="../primer">Key Technologies Primer</a>.</p> |
| |
| <h2 id="apache-struts-2-architecture-in-a-nutshell">Apache Struts 2 Architecture in a Nutshell</h2> |
| |
| <p><img src="attachments/struts2-arch.png" alt="struts2-arch.png" /></p> |
| |
| <ol> |
| <li>The web browser requests a resource - <code class="language-plaintext highlighter-rouge">/mypage.action</code>, <code class="language-plaintext highlighter-rouge">/reports/myreport.pdf</code>, etc</li> |
| <li>The Filter Dispatcher looks at the request and determines the appropriate Action</li> |
| <li>The Interceptors automatically apply common functionality to the request, like workflow, validation, and file upload handling</li> |
| <li>The Action method executes, usually storing and/or retrieving information from a database</li> |
| <li>The Result renders the output to the browser, be it HTML, images, PDF, or something else</li> |
| </ol> |
| |
| <h2 id="struts-tags-in-a-nutshell">Struts Tags in a nutshell</h2> |
| |
| <p>The Struts Tags help you create rich web applications with a minimum of coding. Often, much of the coding effort |
| in a web application goes into the pages. The Struts Tags reduce effort by reducing code.</p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code> |
| <span class="nt"><</span><span class="err">%</span> <span class="na">User</span> <span class="na">user = </span><span class="s">...</span> <span class="err">%</span><span class="nt">></span> |
| <span class="nt"><form</span> <span class="na">action=</span><span class="s">"Profile_update.action"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> |
| <span class="nt"><table></span> |
| <span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">align=</span><span class="s">"right"</span><span class="nt">><label></span>First name:<span class="nt"></label></td></span> |
| <span class="nt"><td><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">name=</span><span class="s">"user.firstname"</span> <span class="na">value=</span><span class="s">"<%=user.getFirstname() %> /></td> |
| </tr> |
| <tr> |
| <td><input type="</span><span class="na">radio</span><span class="err">"</span> <span class="na">name=</span><span class="s">"user.gender"</span> <span class="na">value=</span><span class="s">"0"</span> <span class="na">id=</span><span class="s">"user.gender0"</span> |
| <span class="err"><%</span> <span class="na">if</span> <span class="na">(user.getGender()=</span><span class="s">=0)</span> <span class="err">{</span> <span class="err">%</span><span class="nt">></span> checked="checked" %> } %> /> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"user.gender0"</span><span class="nt">></span>Female<span class="nt"></label></span> |
| <span class="nt"></tr></span> |
| <span class="nt"></table></span> |
| <span class="nt"></form></span> |
| ... |
| </code></pre></div></div> |
| |
| <p>Looking over the markup, it’s easy to see why Java web development without the aid from a modern framework is hard! |
| So far, we’ve only coded two controls, and there are six more to go! Let’s rewrite and finish the form using Struts Tags.</p> |
| |
| <p><img src="attachments/att1846_nutshell.gif" alt="nutshell.gif" /></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:actionerror/></span> |
| <span class="nt"><s:form</span> <span class="na">action=</span><span class="s">"Profile\_update"</span> <span class="na">validate=</span><span class="s">"true"</span><span class="nt">></span> |
| <span class="nt"><s:textfield</span> <span class="na">label=</span><span class="s">"Username"</span> <span class="na">name=</span><span class="s">"username"</span><span class="nt">/></span> |
| <span class="nt"><s:password</span> <span class="na">label=</span><span class="s">"Password"</span> <span class="na">name=</span><span class="s">"password"</span><span class="nt">/></span> |
| <span class="nt"><s:password</span> <span class="na">label=</span><span class="s">"(Repeat) Password"</span> <span class="na">name=</span><span class="s">"password2"</span><span class="nt">/></span> |
| <span class="nt"><s:textfield</span> <span class="na">label=</span><span class="s">"Full Name"</span> <span class="na">name=</span><span class="s">"fullName"</span><span class="nt">/></span> |
| <span class="nt"><s:textfield</span> <span class="na">label=</span><span class="s">"From Address"</span> <span class="na">name=</span><span class="s">"fromAddress"</span><span class="nt">/></span> |
| <span class="nt"><s:textfield</span> <span class="na">label=</span><span class="s">"Reply To Address"</span> <span class="na">name=</span><span class="s">"replyToAddress"</span><span class="nt">/></span> |
| <span class="nt"><s:submit</span> <span class="na">value=</span><span class="s">"Save"</span> <span class="na">name=</span><span class="s">"Save"</span><span class="nt">/></span> |
| <span class="nt"><s:submit</span> <span class="na">action=</span><span class="s">"Register_cancel"</span> <span class="na">value=</span><span class="s">"Cancel"</span> <span class="na">name=</span><span class="s">"Cancel"</span> <span class="na">onclick=</span><span class="s">"form.onsubmit=null"</span><span class="nt">/></span> |
| <span class="nt"></s:form></span> |
| </code></pre></div></div> |
| |
| <p>The Struts Tags also support validation and localization as first-class features. So not only is there less code, |
| but there is <em>more</em> utility. In about the same amount of code as two conventional controls, the Struts Tags can create |
| an entire data-input form with eight controls. Not only is there less code, but the code is easier to read and maintain.</p> |
| |
| <h2 id="struts-configuration-in-a-nutshell">Struts Configuration in a Nutshell</h2> |
| |
| <p>A web application uses a deployment descriptor to initialize resources like filters and listeners. The web deployment |
| descriptor is formatted as a XML document and named <code class="language-plaintext highlighter-rouge">web.xml</code>. Struts can either initialize its resources by scanning |
| your classes using Java packages declared in this <code class="language-plaintext highlighter-rouge">web.xml</code> file, or you can have full control over the configuration |
| via a configuration file, named <code class="language-plaintext highlighter-rouge">struts.xml</code>. These resources include action mappings, to direct input to server-side |
| Action classes, and result types, to select output pages.</p> |
| |
| <p>Here’s a typical configuration (<code class="language-plaintext highlighter-rouge">struts.xml</code>) for a login workflow:</p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><struts></span> |
| <span class="nt"><package</span> <span class="na">name=</span><span class="s">"default"</span> <span class="na">extends=</span><span class="s">"struts-default"</span><span class="nt">></span> |
| <span class="nt"><action</span> <span class="na">name=</span><span class="s">"Logon"</span> <span class="na">class=</span><span class="s">"mailreader2.Logon"</span><span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>/pages/Logon.jsp<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"cancel"</span> <span class="na">type=</span><span class="s">"redirectAction"</span><span class="nt">></span>Welcome<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">type=</span><span class="s">"redirectAction"</span><span class="nt">></span>MainMenu<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"expired"</span> <span class="na">type=</span><span class="s">"chain"</span><span class="nt">></span>ChangePassword<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| |
| <span class="nt"><action</span> <span class="na">name=</span><span class="s">"Logoff"</span> <span class="na">class=</span><span class="s">"mailreader2.Logoff"</span><span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">type=</span><span class="s">"redirectAction"</span><span class="nt">></span>Welcome<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| <span class="nt"></package></span> |
| <span class="nt"></struts></span> |
| </code></pre></div></div> |
| |
| <blockquote> |
| <p>The framework provides general-purpose defaults, so we can start using Struts right away, “out of the box”. |
| Any factory defaults can be overridden in an application’s configuration, as needed.</p> |
| </blockquote> |
| |
| <h2 id="struts-mvc-in-a-nutshell">Struts MVC in a Nutshell</h2> |
| |
| <p>Struts is a <a href="../primer.html#mvc">Model View Controller</a> framework. Struts provides Controller and View components, |
| and integrates with other technologies to provide the Model. The framework’s Controller acts as a bridge between |
| the application’s Model and the web View.</p> |
| |
| <p>To make it easier to present dynamic data, the framework includes a library of markup tags. The tags interact with |
| the framework’s validation and internationalization features, to ensure that input is correct and output is localized. |
| The tag library can be used with JSP, FreeMarker, or Velocity. Of course, other tag libraries, JSTL, and AJAX can also |
| be used, with or without the Struts tags. JavaServer Faces components are also supported.</p> |
| |
| <p>When a request is received, the Controller invokes an Action class. The Action class examines or updates |
| the application’s state by consulting the Model (or, preferably, an interface representing the Model). To transfer |
| data between the Model and the View, properties can be placed on the Action class, or on a plain old JavaBean.</p> |
| |
| <p>Most often, the Model is represented as a graph of JavaBean objects. The Model should do the “heavy lifting”, |
| and the Action will act as a “traffic cop” or adapter. The framework provides sophisticated, automatic type conversion |
| to simplify transferring data between rich domain objects and text-only HTTP requests.</p> |
| |
| <p>Struts is extensible. <em>Very</em> extensible. Every class deployed by the framework is based on an interface. We provide |
| all the base classes an application may ever need, but if we missed something, it’s easy to add your own. We provide |
| the general-purpose framework, but you can still write <em>your</em> application <em>your</em> way.</p> |
| |
| <h2 id="is-struts-the-best-choice-for-every-project">Is Struts the best choice for every project?</h2> |
| |
| <p>Apache Struts 2 helps you create an extensible development environment for enterprise-grade applications, based on |
| industry standards and proven design patterns. If you need to write a very simple application, with a handful of pages, |
| then you might consider a “Model 1” solution that uses only server pages.</p> |
| |
| <p>But, if you are writing a more complicated application, with dozens of pages, that need to be maintained over time, |
| then Struts can help. For more about whether Model 1 or MVC/Model 2 is right for you, |
| see <a href="http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc">Understanding JavaServer Pages Model 2 architecture</a>.</p> |
| |
| <h2 id="platform-requirements">Platform Requirements</h2> |
| |
| <p>Struts 2 requires</p> |
| |
| <ul> |
| <li>Servlet API 2.4</li> |
| <li>JSP API 2.0</li> |
| <li>Java 7</li> |
| </ul> |
| |
| <p>For a full list of requirements, including dependencies used by optional plugins, see <a href="../maven/dependencies">Project Dependencies</a>.</p> |
| |
| </section> |
| </article> |
| |
| |
| <footer class="container"> |
| <div class="col-md-12"> |
| Copyright © 2000-2022 <a href="https://www.apache.org/">The Apache Software Foundation</a>. |
| Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are |
| trademarks of The Apache Software Foundation. All Rights Reserved. |
| </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> |