Author: lukaszlenart
Date: Mon Apr 3 19:57:11 2017
New Revision: 1009714
Log:
Updates production
Modified:
websites/production/struts/content/getting-started/hello-world-using-struts2.html
Modified:
websites/production/struts/content/getting-started/hello-world-using-struts2.html
==============================================================================
---
websites/production/struts/content/getting-started/hello-world-using-struts2.html
(original)
+++
websites/production/struts/content/getting-started/hello-world-using-struts2.html
Mon Apr 3 19:57:11 2017
@@ -138,38 +138,27 @@
<p>Create an Action class to control the interaction between the user, the
model, and the view (the controller)</p>
</li>
<li>
- <p>Create a mapping (struts.xml) to couple the Action class and view</p>
+ <p>Create a mapping (<code class="highlighter-rouge">struts.xml</code>) to
couple the Action class and view</p>
</li>
</ol>
<p>By creating these components, we are separating the work flow into three
well-known concerns: the View, the Model, and the Controller. Separating
concerns makes it easier to manage applications as they become more complex.</p>
-<table>
- <tbody>
- <tr>
- </tr>
- </tbody>
-</table>
-
<p>Letâs look at an example model class, Action, server page, and mapping.
If you like, fire up your Java IDE, and enter the code as we go.</p>
<blockquote>
<p>This tutorial assumes youâve completed the <a href="#PAGE_14811860">How
To Create A Struts 2 Web Application</a> tutorial and have a working basic
Struts project. The example code for this tutorial, helloworld, is available
for checkout from the Struts 2 GitHub repository at <a
href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.
The example projects use Maven to manage the artifact dependencies and to
build the .war files.</p>
</blockquote>
-<p><strong>The Code</strong></p>
+<h3 id="the-code">The Code</h3>
<p>Letâs modify either the basic_struts project to add a model class to
store our message, a view that displays our message, an Action class to act as
the controller, and a configuration that ties everything together.</p>
-<table>
- <tbody>
- <tr>
- <td>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 this application 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.</td>
- </tr>
- </tbody>
-</table>
+<blockquote>
+ <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
this application 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>
+</blockquote>
-<p><strong>Step 1 - Create The Model Class MessageStore.java</strong></p>
+<h4 id="step-1---create-the-model-class-messagestorejava">Step 1 - Create The
Model Class MessageStore.java</h4>
<p>If youâre using the Basic_Struts2_Ant project to start with create the
MessageStore class in the src folder and if youâre using the
Basic_Struts2_Mvn class create the MessageStore class in src/main/java. Be sure
to note the package statement below.</p>
@@ -201,11 +190,11 @@
</code></pre>
</div>
-<p>In the model class above note the use of public set and get methods to
allow access to the private message String attribute. The Struts 2 framework
requires that objects you want to expose to the view (HelloWorld.jsp) follow
the <a
href="http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions">JavaBean-style
conventions</a>.</p>
+<p>Note the use of public set and get methods to allow access to the private
message String attribute. The Struts 2 framework requires that objects you want
to expose to the view (<code class="highlighter-rouge">HelloWorld.jsp</code>)
follow the <a
href="http://en.wikipedia.org/wiki/JavaBean#JavaBean_conventions">JavaBean-style
conventions</a>.</p>
-<p><strong>Step 2 - Create The Action Class HelloWorldAction.java</strong></p>
+<h4 id="step-2---create-the-action-class-helloworldactionjava">Step 2 - Create
The Action Class HelloWorldAction.java</h4>
-<p>We need an Action class to act as the Controller. The Action class responds
to a user action (in this example that action will be clicking an HTML
hyperlink and sending a specific URL to the Servlet container). One or more of
the Action classâs methods are executed and a String result is returned.
Based on the value of the result, a specific view page (in this example that
view page is HelloWorld.jsp) is rendered.</p>
+<p>We need an Action class to act as the Controller. The Action class responds
to a user action (in this example that action will be clicking an HTML
hyperlink and sending a specific URL to the Servlet container). One or more of
the Action classâs methods are executed and a String result is returned.
Based on the value of the result, a specific view page (in this example that
view page is <code class="highlighter-rouge">HelloWorld.jsp</code>) is
rendered.</p>
<p>Note the package and import statements below.</p>
@@ -245,7 +234,7 @@
<p>Note also the public getter and setter methods for the private <code
class="highlighter-rouge">MessageStore</code>Â object. Since we want to make
the <code class="highlighter-rouge">MessageStore</code>Â object available to
the view page, <code class="highlighter-rouge">HelloWorld.jsp</code>, we need
to follow the <a
href="http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions">JavaBean-style</a>
of providing get and set methods.</p>
-<p><strong>Step 3 - Create The View HelloWorld.jsp</strong></p>
+<h4 id="step-3---create-the-view-helloworldjsp">Step 3 - Create The View
HelloWorld.jsp</h4>
<p>We need a server page to present the message that is stored in the model
class <code class="highlighter-rouge">MessageStore</code>. Create the below JSP
in the <code class="highlighter-rouge">WebContent</code>Â folder (if using Ant)
or in <code class="highlighter-rouge">src/main/webapp</code> (if using
Maven).</p>
@@ -273,7 +262,7 @@
<p>Weâll learn more about tags in the next tutorial. See the <em>Struts
Tags</em> for more information about tags.</p>
-<p><strong>Step 4 - Add The Struts Configuration In struts.xml</strong></p>
+<h4 id="step-4---add-the-struts-configuration-in-strutsxml">Step 4 - Add The
Struts Configuration In struts.xml</h4>
<p>We need a mapping to tie the URL, the <code
class="highlighter-rouge">HelloWorldAction</code>Â class (controller), and the
<code class="highlighter-rouge">HelloWorld.jsp</code>Â (the view) together. The
mapping tells the Struts 2 framework which class will respond to the userâs
action (the URL), which method of that class will be executed, and what view to
render based on the String result that method returns.</p>
@@ -303,7 +292,7 @@
</code></pre>
</div>
-<p><strong>Step 5 - Create The URL Action</strong></p>
+<h4 id="step-5---create-the-url-action">Step 5 - Create The URL Action</h4>
<p>In index.jsp (see WebContent folder for Ant project and src/main/webapp for
Mvn project) letâs add an Action URL the user can click on to tell the Struts
2 framework to run the execute method of the HelloWorldAction class and render
the HelloWorld.jsp view.</p>
@@ -329,7 +318,7 @@
<p>The Struts url tag creates the URL with an action of hello. The hello
action was mapped to the HelloWorldAction class and its execute method. When
the user clicks on the above URL it will cause the Struts 2 framework to run
the execute method of the HelloWorldAction class. After that method returns the
String success, the view page HelloWorld.jsp will be rendered.</p>
-<p><strong>Step 6 - Build the WAR File and Run The Application</strong></p>
+<h4 id="step-6---build-the-war-file-and-run-the-application">Step 6 - Build
the WAR File and Run The Application</h4>
<p>Execute <code class="highlighter-rouge">mvn clean package</code> to create
the war file.</p>
@@ -341,17 +330,13 @@
<p><img src="attachments/att14974993_Hello.png" alt="Hello.png" /></p>
-<p><strong>Getting Help</strong></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
this application 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>
-
-<p><strong>How the Code Works</strong></p>
+<h3 id="how-the-code-works">How the Code Works</h3>
<p>Your browser sends to the web server a request for the URL <a
href="http://localhost:8080/Hello_World_Struts2_Ant/hello.action">http://localhost:8080/Hello_World_Struts2_Ant/hello.action</a>.</p>
<ol>
<li>
- <p>The container receives from the web server a request for the resource
<code class="highlighter-rouge">hello.action</code>. According to the settings
loaded from the <em>web.xml</em> , the container finds that all requests are
being routed to <code
class="highlighter-rouge">org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</code>,
including the <code class="highlighter-rouge">*.action</code> requests. The
StrutsPrepareAndExecuteFilter is the entry point into the framework.</p>
+ <p>The container receives from the web server a request for the resource
<code class="highlighter-rouge">hello.action</code>. According to the settings
loaded from the <a href="//struts.apache.org/docs/webxml.html">web.xml</a> ,
the container finds that all requests are being routed to <code
class="highlighter-rouge">org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</code>,
including the <code class="highlighter-rouge">*.action</code> requests. The
StrutsPrepareAndExecuteFilter is the entry point into the framework.</p>
</li>
<li>
<p>The framework looks for an action mapping named âhelloâ, and it
finds that this mapping corresponds to the class âHelloWorldActionâ. The
framework instantiates the Action and calls the Actionâs <code
class="highlighter-rouge">execute</code> method.</p>
@@ -367,7 +352,7 @@
</li>
</ol>
-<p><strong>What to Remember</strong></p>
+<h3 id="what-to-remember">What to Remember</h3>
<p>The framework uses Actions to process HTML forms and other requests. The
<code class="highlighter-rouge">Action</code> class returns a result-name such
as <code class="highlighter-rouge">SUCCESS</code>, <code
class="highlighter-rouge">ERROR</code> or <code
class="highlighter-rouge">INPUT</code>. Based on the mappings loaded from the
<code class="highlighter-rouge">struts.xml</code>, a given result-name may
select a page (as in this example), another action, or some other web resource
(image, PDF).</p>