Author: lukaszlenart
Date: Tue Apr 18 05:38:20 2017
New Revision: 1010651
Log:
Updates production
Added:
websites/production/struts/content/getting-started/attachments/basic_struts2_maven_jsp.png
(with props)
websites/production/struts/content/getting-started/attachments/helloworld_hello_action.png
(with props)
websites/production/struts/content/getting-started/attachments/helloworld_index_action.png
(with props)
Modified:
websites/production/struts/content/getting-started/hello-world-using-struts2.html
websites/production/struts/content/getting-started/how-to-create-a-struts2-web-application.html
Added:
websites/production/struts/content/getting-started/attachments/basic_struts2_maven_jsp.png
==============================================================================
Binary file - no diff available.
Propchange:
websites/production/struts/content/getting-started/attachments/basic_struts2_maven_jsp.png
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added:
websites/production/struts/content/getting-started/attachments/helloworld_hello_action.png
==============================================================================
Binary file - no diff available.
Propchange:
websites/production/struts/content/getting-started/attachments/helloworld_hello_action.png
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added:
websites/production/struts/content/getting-started/attachments/helloworld_index_action.png
==============================================================================
Binary file - no diff available.
Propchange:
websites/production/struts/content/getting-started/attachments/helloworld_index_action.png
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
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
Tue Apr 18 05:38:20 2017
@@ -126,7 +126,7 @@
<p>When you click on a hyperlink or submit an HTML form in a Struts 2 web
application, the input is not sent to another server page, but to a Java class
that you provide. These classes are called Actions. After the Action fires, a
Result selects a resource to render the response. The resource is generally a
server page, but it can also be a PDF file, an Excel spreadsheet, or a Java
applet window.</p>
-<p>Suppose you want to create a simple âHello Worldâ example that displays
a welcome message. After setting up an empty basic Struts 2 web application
(see <a href="#PAGE_14811860">How To Create A Struts 2 Web Application</a>), to
create a âHello Worldâ example, you need to do four things:</p>
+<p>Suppose you want to create a simple âHello Worldâ example that displays
a welcome message. After setting up an empty basic Struts 2 web application
(see <a href="how-to-create-a-struts2-web-application.html">How To Create A
Struts 2 Web Application</a>), to create a âHello Worldâ example, you need
to do four things:</p>
<ol>
<li>
@@ -148,12 +148,19 @@
<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>
+ <p>This tutorial assumes youâve completed the <a
href="how-to-create-a-struts2-web-application.html">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>
<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>
+<p>Letâs modify the basic-struts project to add the following:</p>
+
+<ul>
+ <li>a model class to store our message</li>
+ <li>a view that displays our message</li>
+ <li>an Action class to act as the controller</li>
+ <li>a configuration that ties everything together</li>
+</ul>
<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>
@@ -161,7 +168,7 @@
<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>
+<p>Create the <code class="highlighter-rouge">MessageStore</code> class in
<code class="highlighter-rouge">src/main/java</code>. Be sure to note the
package statement below.</p>
<blockquote>
<p>Note that in the code shown below the JavaDoc comments are omitted. In
the download example, JavaDoc comments are included.</p>
@@ -172,26 +179,20 @@
<div class="highlighter-rouge"><pre class="highlight"><code><span
class="kn">package</span> <span class="n">org</span><span
class="o">.</span><span class="na">apache</span><span class="o">.</span><span
class="na">struts</span><span class="o">.</span><span
class="na">helloworld</span><span class="o">.</span><span
class="na">model</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MessageStore</span> <span class="o">{</span>
-
<span class="kd">private</span> <span class="n">String</span> <span
class="n">message</span><span class="o">;</span>
-
+
<span class="kd">public</span> <span class="n">MessageStore</span><span
class="o">()</span> <span class="o">{</span>
- <span class="n">setMessage</span><span class="o">(</span><span
class="s">"Hello Struts User"</span><span class="o">);</span>
+ <span class="n">message</span> <span class="o">=</span> <span
class="s">"Hello Struts User"</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">String</span> <span
class="n">getMessage</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">message</span><span
class="o">;</span>
<span class="o">}</span>
-
- <span class="kd">public</span> <span class="kt">void</span> <span
class="n">setMessage</span><span class="o">(</span><span
class="n">String</span> <span class="n">message</span><span class="o">)</span>
<span class="o">{</span>
- <span class="k">this</span><span class="o">.</span><span
class="na">message</span> <span class="o">=</span> <span
class="n">message</span><span class="o">;</span>
- <span class="o">}</span>
-
<span class="o">}</span>
</code></pre>
</div>
-<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>Note the use of the public getter method 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>
<h4 id="step-2---create-the-action-class-helloworldactionjava">Step 2 - Create
The Action Class HelloWorldAction.java</h4>
@@ -204,58 +205,50 @@
<div class="highlighter-rouge"><pre class="highlight"><code><span
class="kn">package</span> <span class="n">org</span><span
class="o">.</span><span class="na">apache</span><span class="o">.</span><span
class="na">struts</span><span class="o">.</span><span
class="na">helloworld</span><span class="o">.</span><span
class="na">action</span><span class="o">;</span>
<span class="kn">import</span> <span
class="nn">org.apache.struts.helloworld.model.MessageStore</span><span
class="o">;</span>
+
<span class="kn">import</span> <span
class="nn">com.opensymphony.xwork2.ActionSupport</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">HelloWorldAction</span> <span class="kd">extends</span> <span
class="n">ActionSupport</span> <span class="o">{</span>
-
- <span class="kd">private</span> <span class="kd">static</span> <span
class="kd">final</span> <span class="kt">long</span> <span
class="n">serialVersionUID</span> <span class="o">=</span> <span
class="mi">1L</span><span class="o">;</span>
-
<span class="kd">private</span> <span class="n">MessageStore</span> <span
class="n">messageStore</span><span class="o">;</span>
- <span class="kd">public</span> <span class="n">String</span> <span
class="n">execute</span><span class="o">()</span> <span
class="kd">throws</span> <span class="n">Exception</span> <span
class="o">{</span>
+ <span class="kd">public</span> <span class="n">String</span> <span
class="n">execute</span><span class="o">()</span> <span class="o">{</span>
<span class="n">messageStore</span> <span class="o">=</span> <span
class="k">new</span> <span class="n">MessageStore</span><span
class="o">()</span> <span class="o">;</span>
+
<span class="k">return</span> <span class="n">SUCCESS</span><span
class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">MessageStore</span> <span
class="n">getMessageStore</span><span class="o">()</span> <span
class="o">{</span>
<span class="k">return</span> <span class="n">messageStore</span><span
class="o">;</span>
<span class="o">}</span>
-
- <span class="kd">public</span> <span class="kt">void</span> <span
class="n">setMessageStore</span><span class="o">(</span><span
class="n">MessageStore</span> <span class="n">messageStore</span><span
class="o">)</span> <span class="o">{</span>
- <span class="k">this</span><span class="o">.</span><span
class="na">messageStore</span> <span class="o">=</span> <span
class="n">messageStore</span><span class="o">;</span>
- <span class="o">}</span>
-
<span class="o">}</span>
</code></pre>
</div>
-<p>The Struts 2 framework will create an object of the <code
class="highlighter-rouge">HelloWorldAction</code>Â class and call the execute
method in response to a userâs action (clicking on a hyperlink that sends a
specific URL to the Servlet container).</p>
+<p>The Struts 2 framework will create an object of the <code
class="highlighter-rouge">HelloWorldAction</code> class and call the execute
method in response to a userâs action (clicking on a hyperlink that sends a
specific URL to the Servlet container).</p>
-<p>In this example, the execute method creates an object of class <code
class="highlighter-rouge">MessageStore</code>Â and then returns the String
constant <code class="highlighter-rouge">SUCCESS</code>.</p>
+<p>In this example, the execute method creates an object of class <code
class="highlighter-rouge">MessageStore</code> and then returns the String
constant <code class="highlighter-rouge">SUCCESS</code>.</p>
-<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>Note also the public getter method 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 getter and setter methods where needed.</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>
+<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">src/main/webapp</code> folder.</p>
<p><strong>HelloWorld.jsp</strong></p>
-<div class="highlighter-rouge"><pre class="highlight"><code><span
class="err"><</span>%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
-<span class="err"><</span>%@ taglib prefix="s" uri="/struts-tags" %>
-<span class="cp"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"></span>
-<span class="nt"><html></span>
- <span class="nt"><head></span>
- <span class="nt"><meta</span> <span class="na">http-equiv=</span><span
class="s">"Content-Type"</span> <span class="na">content=</span><span
class="s">"text/html; charset=ISO-8859-1"</span><span class="nt">></span>
- <span class="nt"><title></span>Hello World!<span
class="nt"></title></span>
- <span class="nt"></head></span>
-
- <span class="nt"><body></span>
- <span class="nt"><h2><s:property</span> <span
class="na">value=</span><span class="s">"messageStore.message"</span> <span
class="nt">/></h2></span>
- <span class="nt"></body></span>
-<span class="nt"></html></span>
+<pre><code class="language-jsp"><!DOCTYPE html>
+<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Hello World!</title>
+ </head>
+ <body>
+ <h2><s:property value="messageStore.message" /></h2>
+ </body>
+</html>
</code></pre>
-</div>
<p>The taglib directive tells the Servlet container that this page will be
using the Struts 2 tags and that these tags will be preceded by an <code
class="highlighter-rouge">s</code>.</p>
@@ -267,86 +260,83 @@
<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>
-<p>Edit the <code class="highlighter-rouge">struts.xml</code> file (in the Mvn
project that file is in the src/main/resources folder) to add the action
mapping. Place the action node (action name=âhelloâ) between the opening
and closing package node, just after the action mapping with the
name=âindexâ. Your complete struts.xml should look like:</p>
+<p>Edit the <code class="highlighter-rouge">struts.xml</code> file (in the Mvn
project that file is in the src/main/resources folder) to add the action
mapping. Place the action node (action name=âhelloâ) between the opening
and closing package node, just after the action mapping with the
name=âindexâ. Your complete <code
class="highlighter-rouge">struts.xml</code> should look like:</p>
<p><strong>struts.xml</strong></p>
<div class="highlighter-rouge"><pre class="highlight"><code><span
class="cp"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="cp"><!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd"></span>
-
+ "-//Apache Software Foundation//DTD Struts Configuration
2.5//EN"
+ "http://struts.apache.org/dtds/struts-2.5.dtd"></span>
<span class="nt"><struts></span>
+ <span class="nt"><constant</span> <span class="na">name=</span><span
class="s">"struts.devMode"</span> <span class="na">value=</span><span
class="s">"true"</span> <span class="nt">/></span>
- <span class="nt"><constant</span> <span class="na">name=</span><span
class="s">"struts.devMode"</span> <span class="na">value=</span><span
class="s">"true"</span> <span class="nt">/></span>
-
- <span class="nt"><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">></span>
- <span class="nt"><action</span> <span class="na">name=</span><span
class="s">"index"</span><span class="nt">></span>
- <span class="nt"><result></span>/index.jsp<span
class="nt"></result></span>
- <span class="nt"></action></span>
+ <span class="nt"><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">></span>
+ <span class="nt"><action</span> <span class="na">name=</span><span
class="s">"index"</span><span class="nt">></span>
+ <span class="nt"><result></span>/index.jsp<span
class="nt"></result></span>
+ <span class="nt"></action></span>
- <span class="nt"><action</span> <span class="na">name=</span><span
class="s">"hello"</span> <span class="na">class=</span><span
class="s">"org.apache.struts.helloworld.action.HelloWorldAction"</span> <span
class="na">method=</span><span class="s">"execute"</span><span
class="nt">></span>
- <span class="nt"><result</span> <span class="na">name=</span><span
class="s">"success"</span><span class="nt">></span>/HelloWorld.jsp<span
class="nt"></result></span>
- <span class="nt"></action></span>
- <span class="nt"></package></span>
+ <span class="nt"><action</span> <span class="na">name=</span><span
class="s">"hello"</span> <span class="na">class=</span><span
class="s">"org.apache.struts.helloworld.action.HelloWorldAction"</span> <span
class="na">method=</span><span class="s">"execute"</span><span
class="nt">></span>
+ <span class="nt"><result</span> <span
class="na">name=</span><span class="s">"success"</span><span
class="nt">></span>/HelloWorld.jsp<span class="nt"></result></span>
+ <span class="nt"></action></span>
+ <span class="nt"></package></span>
<span class="nt"></struts></span>
</code></pre>
</div>
<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>
+<p>Letâs add an Action URL inside <code
class="highlighter-rouge">index.jsp</code> (see <code
class="highlighter-rouge">src/main/webapp</code> folder) so the user can click
on a link to tell the Struts 2 framework to run the execute method of the <code
class="highlighter-rouge">HelloWorldAction</code> class and render the <code
class="highlighter-rouge">HelloWorld.jsp</code> view.</p>
<p>First add the taglib directive at the top of the jsp <code
class="highlighter-rouge"><%@ taglib prefix="s" uri="/struts-tags"
%></code>. Next add this p tag <code
class="highlighter-rouge"><p><a href="<s:url
action='hello'/>">Hello World</a></p></code> after the h1
tag. Your new index.jsp should look like:</p>
<p><strong>index.jsp</strong></p>
-<div class="highlighter-rouge"><pre class="highlight"><code><span
class="err"><</span>%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
-<span class="err"><</span>%@ taglib prefix="s" uri="/struts-tags" %>
-<span class="cp"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"></span>
-<span class="nt"><html></span>
- <span class="nt"><head></span>
- <span class="nt"><meta</span> <span
class="na">http-equiv=</span><span class="s">"Content-Type"</span> <span
class="na">content=</span><span class="s">"text/html;
charset=ISO-8859-1"</span><span class="nt">></span>
- <span class="nt"><title></span>Basic Struts 2 Application -
Welcome<span class="nt"></title></span>
- <span class="nt"></head></span>
- <span class="nt"><body></span>
- <span class="nt"><h1></span>Welcome To Struts 2!<span
class="nt"></h1></span>
- <span class="nt"><p><a</span> <span
class="na">href=</span><span class="s">"<s:url
action='hello'/>"</span><span class="nt">></span>Hello World<span
class="nt"></a></p></span>
- <span class="nt"></body></span>
-<span class="nt"></html></span>
+<pre><code class="language-jsp"><!DOCTYPE html>
+<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
+ <title>Basic Struts 2 Application - Welcome</title>
+ </head>
+ <body>
+ <h1>Welcome To Struts 2!</h1>
+ <p><a href="<s:url action='hello'/>">Hello
World</a></p>
+ </body>
+</html>
</code></pre>
-</div>
-<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>The Struts url tag creates the URL with an action of hello. The hello
action was mapped to the <code
class="highlighter-rouge">HelloWorldAction</code> class and its <code
class="highlighter-rouge">execute</code> method. When the user clicks on the
above URL it will cause the Struts 2 framework to run the execute method of the
<code class="highlighter-rouge">HelloWorldAction</code> class. After that
method returns the String <code class="highlighter-rouge">success</code>
(constant <code class="highlighter-rouge">SUCCESS</code>), the view page <code
class="highlighter-rouge">HelloWorld.jsp</code> will be rendered.</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>
+<p>Execute <code class="highlighter-rouge">mvn jetty:run</code> to run the
application.</p>
-<p>Copy the war file to your Servlet container. After your Servlet container
successfully deploys the war file go to this URL <a
href="http://localhost:8080/helloworld/index.action">http://localhost:8080/helloworld/index.action</a>
where you should see the following:</p>
+<p>Go to this URL <a
href="http://localhost:8080/helloworld/index.action">http://localhost:8080/helloworld/index.action</a>
where you should see the following:</p>
-<p><img src="attachments/att14974994_Struts2HelloWorld.png"
alt="Struts2HelloWorld.png" /></p>
+<p><img src="attachments/helloworld_index_action.png"
alt="helloworld_index_action.png" /></p>
<p>Click on the Hello World link and you should get the HelloWorld.jsp
page:</p>
-<p><img src="attachments/att14974993_Hello.png" alt="Hello.png" /></p>
+<p><img src="attachments/helloworld_hello_action.png"
alt="helloworld_hello_action.png" /></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>
+<p>Your browser sends to the web server a request for the URL <a
href="http://localhost:8080/helloworld/hello.action">http://localhost:8080/helloworld/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 <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>
+ <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.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>
</li>
<li>
- <p>The <code class="highlighter-rouge">execute</code> method creates the
MessageStore object and returns <code class="highlighter-rouge">SUCCESS</code>.
The framework checks the action mapping to see what page to load if <code
class="highlighter-rouge">SUCCESS</code> is returned. The framework tells the
container to render as the response to the request, the resource <code
class="highlighter-rouge">HelloWorld.jsp</code>.</p>
+ <p>The <code class="highlighter-rouge">execute</code> method creates the
MessageStore object and returns <code class="highlighter-rouge">SUCCESS</code>
(=<code class="highlighter-rouge">"success"</code>). The framework checks the
action mapping to see what page to load if <code
class="highlighter-rouge">SUCCESS</code> is returned. The framework tells the
container to render as the response to the request, the resource <code
class="highlighter-rouge">HelloWorld.jsp</code>.</p>
</li>
<li>
- <p>As the page <code class="highlighter-rouge">HelloWorld.jsp</code> is
being processed, the <code class="highlighter-rouge"><s:property
value="messageStore.message" /></code> tag calls the getter <code
class="highlighter-rouge">getMessageStore</code> of the <code
class="highlighter-rouge">HelloWorld</code> Action and then calls the <code
class="highlighter-rouge">getMessage</code> of the MessageStore object returned
by <code class="highlighter-rouge">getMessageStore</code>, and the tag merges
into the response the value of the message attribute.</p>
+ <p>As the page <code class="highlighter-rouge">HelloWorld.jsp</code> is
being processed, the <code class="highlighter-rouge"><s:property
value="messageStore.message" /></code> tag calls the getter <code
class="highlighter-rouge">getMessageStore</code> of the <code
class="highlighter-rouge">HelloWorld</code> Action and then calls the <code
class="highlighter-rouge">getMessage</code> of the MessageStore object returned
by <code class="highlighter-rouge">getMessageStore</code>. The tag merges the
value of the message attribute into the response.</p>
</li>
<li>
<p>A pure HTML response is sent back to the browser.</p>
Modified:
websites/production/struts/content/getting-started/how-to-create-a-struts2-web-application.html
==============================================================================
---
websites/production/struts/content/getting-started/how-to-create-a-struts2-web-application.html
(original)
+++
websites/production/struts/content/getting-started/how-to-create-a-struts2-web-application.html
Tue Apr 18 05:38:20 2017
@@ -162,32 +162,76 @@ at <a href="https://github.com/apache/st
the web application archive (war) file.</p>
</blockquote>
-<p><strong>Step 1 - Create A Java Web Application</strong></p>
+<h4 id="step-1---create-a-java-web-application">Step 1 - Create A Java Web
Application</h4>
<p>In your Java IDE create a Java web application with a project name of
basic_struts that follows the standard Maven
-project folder structure. In your pom.xml include the following:</p>
+project folder structure. In your <code
class="highlighter-rouge">pom.xml</code> include the following:</p>
<p><strong>pom.xml build node</strong></p>
-<div title="Final name" class="highlighter-rouge"><pre
class="highlight"><code><span class="nt"><build></span>
- <span class="nt"><finalName></span>basic_struts<span
class="nt"></finalName></span>
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="nt"><build></span>
+ <span class="nt"><finalName></span>basic-struts<span
class="nt"></finalName></span>
<span class="nt"></build></span>
</code></pre>
</div>
-<p><strong>Step 2 - Add index.jsp</strong></p>
+<h6
id="to-run-the-application-using-maven-add-the-jetty-maven-plugin-to-your-pomxml">to
run the application using maven, add the jetty maven-plugin to your
pom.xml</h6>
+
+<p><strong>pom.xml jetty plugin</strong></p>
+
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="nt"><build></span>
+ ...
+ <span class="nt"><plugins></span>
+ <span class="nt"><plugin></span>
+ <span class="nt"><groupId></span>org.mortbay.jetty<span
class="nt"></groupId></span>
+ <span class="nt"><artifactId></span>jetty-maven-plugin<span
class="nt"></artifactId></span>
+ <span class="nt"><version></span>8.1.16.v20140903<span
class="nt"></version></span>
+ <span class="nt"><configuration></span>
+ <span class="nt"><webApp></span>
+ <span
class="nt"><contextPath></span>/${build.finalName}<span
class="nt"></contextPath></span>
+ <span class="nt"></webApp></span>
+ <span class="nt"><stopKey></span>CTRL+C<span
class="nt"></stopKey></span>
+ <span class="nt"><stopPort></span>8999<span
class="nt"></stopPort></span>
+ <span class="nt"><scanIntervalSeconds></span>10<span
class="nt"></scanIntervalSeconds></span>
+ <span class="nt"><scanTargets></span>
+ <span
class="nt"><scanTarget></span>src/main/webapp/WEB-INF/web.xml<span
class="nt"></scanTarget></span>
+ <span class="nt"></scanTargets></span>
+ <span class="nt"></configuration></span>
+ <span class="nt"></plugin></span>
+ <span class="nt"></plugins></span>
+<span class="nt"></build></span>
+</code></pre>
+</div>
+<p>The above plugin will enable you to run the application using <code
class="highlighter-rouge">mvn jetty:run</code></p>
+
+<h4 id="step-2---add-indexjsp">Step 2 - Add index.jsp</h4>
<p>Our next step is to add a simple <code
class="highlighter-rouge">index.jsp</code> to this web application. Create an
<code class="highlighter-rouge">index.jsp</code> under <code
class="highlighter-rouge">src/main/webapp</code>
with a title of <strong>Basic Struts 2 Application - Welcome</strong> and in
the body add an h1 heading of <strong>Welcome to Struts 2!</strong></p>
-<p>Run <code class="highlighter-rouge">mvn clean package</code> to create the
war file. Copy the war file into your Servlet container so that it will deploy
the war file.</p>
+<p><strong>index.jsp</strong></p>
+
+<pre><code class="language-jsp"><!DOCTYPE html>
+<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>Basic Struts 2 Application - Welcome</title>
+ </head>
+ <body>
+ <h1>Welcome To Struts 2!</h1>
+ </body>
+</html>
+</code></pre>
-<p>Start up your Servlet container and in a web browser go to <a
href="http://localhost:8080/Basic_Struts2_Mvn/index.jsp">http://localhost:8080/Basic_Struts2_Mvn/index.jsp</a>.
+<p>Run <code class="highlighter-rouge">mvn jetty:run</code> to run the
application.</p>
+
+<p>In a web browser go to <a
href="[http://localhost:8080/basic-struts/index.jsp">http://localhost:8080/basic-struts/index.jsp</a>.
You should see the following:</p>
-<p><img src="attachments/att14974992_Basic_Struts2_Mvn.png"
alt="Basic_Struts2_Mvn.png" /></p>
+<p><img src="attachments/basic_struts2_maven_jsp.png"
alt="basic_struts2_maven_jsp.png" /></p>
-<p><strong>Step 3 - Add Struts 2 Jar Files To Class Path</strong></p>
+<h4 id="step-3---add-struts-2-jar-files-to-class-path">Step 3 - Add Struts 2
Jar Files To Class Path</h4>
<p>Now that we know we have a working Java web application, lets add the
minimal required Struts 2 framework Jar files
to our web applicationâs class path. In <code
class="highlighter-rouge">pom.xml</code> add the following dependency node:</p>
@@ -208,58 +252,45 @@ files struts2-core requires (transitive
<p><i class="glyphicon glyphicon-info-sign alert-info" aria-hidden="true"></i>
Beginning with Struts version 2.2.3
you do not need to specify a separate dependency node for javassist.</p>
-<p><strong>Step 4 - Add Logging</strong></p>
+<h4 id="step-4---add-logging">Step 4 - Add Logging</h4>
-<p>To see whatâs happening under the hood, the example application for this
tutorial uses log4j. Youâll need to add to pom.xml a dependency node for the
log4j jar file:</p>
+<p>To see whatâs happening under the hood, the example application for this
tutorial uses log4j2. Youâll need to add a dependency node for log4j2 to the
pom:</p>
<p><strong>pom.xml log4j dependency node</strong></p>
<div class="highlighter-rouge"><pre class="highlight"><code><span
class="nt"><dependency></span>
- <span class="nt"><groupId></span>log4j<span
class="nt"></groupId></span>
- <span class="nt"><artifactId></span>log4j<span
class="nt"></artifactId></span>
- <span class="nt"><version></span>1.2.14<span
class="nt"></version></span>
+ <span class="nt"><groupId></span>org.apache.logging.log4j<span
class="nt"></groupId></span>
+ <span class="nt"><artifactId></span>log4j-core<span
class="nt"></artifactId></span>
+ <span class="nt"><version></span>2.8.2<span
class="nt"></version></span>
<span class="nt"></dependency></span>
</code></pre>
</div>
-<p>Setup a <code class="highlighter-rouge">log4j.xml</code> configuration in
the <code class="highlighter-rouge">src/main/resources</code> folder. You can
copy the one from the example application,
-which contains the following</p>
-
-<p><strong>log4j.xml</strong></p>
+<p>Setup a <code class="highlighter-rouge">log4j2.xml</code> configuration in
the <code class="highlighter-rouge">src/main/resources</code> folder which
contains the following</p>
-<div class="highlighter-rouge"><pre class="highlight"><code><span
class="cp"><?xml version="1.0" encoding="UTF-8" ?></span>
-<span class="cp"><!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j
Configuration//EN" "log4j.dtd"></span>
+<p><strong>log4j2.xml</strong></p>
-<span class="nt"><log4j:configuration</span> <span
class="na">xmlns:log4j=</span><span
class="s">"http://jakarta.apache.org/log4j/"</span><span class="nt">></span>
-
- <span class="nt"><appender</span> <span class="na">name=</span><span
class="s">"STDOUT"</span> <span class="na">class=</span><span
class="s">"org.apache.log4j.ConsoleAppender"</span><span class="nt">></span>
- <span class="nt"><layout</span> <span class="na">class=</span><span
class="s">"org.apache.log4j.PatternLayout"</span><span class="nt">></span>
- <span class="nt"><param</span> <span class="na">name=</span><span
class="s">"ConversionPattern"</span> <span class="na">value=</span><span
class="s">"%d %-5p %c.%M:%L - %m%n"</span><span class="nt">/></span>
- <span class="nt"></layout></span>
- <span class="nt"></appender></span>
-
- <span class="c"><!-- specify the logging level for loggers from other
libraries --></span>
- <span class="nt"><logger</span> <span class="na">name=</span><span
class="s">"com.opensymphony"</span><span class="nt">></span>
- <span class="nt"><level</span> <span class="na">value=</span><span
class="s">"DEBUG"</span> <span class="nt">/></span>
- <span class="nt"></logger></span>
-
- <span class="nt"><logger</span> <span class="na">name=</span><span
class="s">"org.apache.struts2"</span><span class="nt">></span>
- <span class="nt"><level</span> <span class="na">value=</span><span
class="s">"DEBUG"</span> <span class="nt">/></span>
- <span class="nt"></logger></span>
-
- <span class="c"><!-- for all other loggers log only info and above log
messages --></span>
- <span class="nt"><root></span>
- <span class="nt"><priority</span> <span
class="na">value=</span><span class="s">"INFO"</span><span
class="nt">/></span>
- <span class="nt"><appender-ref</span> <span
class="na">ref=</span><span class="s">"STDOUT"</span> <span
class="nt">/></span>
- <span class="nt"></root></span>
-
-<span class="nt"></log4j:configuration></span>
+<div class="highlighter-rouge"><pre class="highlight"><code><span
class="cp"><?xml version="1.0" encoding="UTF-8"?></span>
+<span class="nt"><Configuration></span>
+ <span class="nt"><Appenders></span>
+ <span class="nt"><Console</span> <span class="na">name=</span><span
class="s">"STDOUT"</span> <span class="na">target=</span><span
class="s">"SYSTEM_OUT"</span><span class="nt">></span>
+ <span class="nt"><PatternLayout</span> <span
class="na">pattern=</span><span class="s">"%d %-5p [%t] %C{2} (%F:%L) -
%m%n"</span><span class="nt">/></span>
+ <span class="nt"></Console></span>
+ <span class="nt"></Appenders></span>
+ <span class="nt"><Loggers></span>
+ <span class="nt"><Logger</span> <span class="na">name=</span><span
class="s">"com.opensymphony.xwork2"</span> <span class="na">level=</span><span
class="s">"debug"</span><span class="nt">/></span>
+ <span class="nt"><Logger</span> <span class="na">name=</span><span
class="s">"org.apache.struts2"</span> <span class="na">level=</span><span
class="s">"debug"</span><span class="nt">/></span>
+ <span class="nt"><Root</span> <span class="na">level=</span><span
class="s">"warn"</span><span class="nt">></span>
+ <span class="nt"><AppenderRef</span> <span
class="na">ref=</span><span class="s">"STDOUT"</span><span
class="nt">/></span>
+ <span class="nt"></Root></span>
+ <span class="nt"></Loggers></span>
+<span class="nt"></Configuration></span>
</code></pre>
</div>
-<p>Note the above log4j configuration specifies the console as the log
target.</p>
+<p>Note the above log4j2 configuration specifies the console as the log
target.</p>
-<p><strong>Step 5 - Add Struts 2 Servlet Filter</strong></p>
+<h4 id="step-5---add-struts-2-servlet-filter">Step 5 - Add Struts 2 Servlet
Filter</h4>
<p>To enable the Struts 2 framework to work with your web application you need
to add a Servlet filter class and filter
mapping to <code class="highlighter-rouge">web.xml</code>. Below is the filter
and filter-mapping nodes you should add to <code
class="highlighter-rouge">web.xml</code>.</p>
@@ -281,7 +312,7 @@ mapping to <code class="highlighter-roug
<p>For more information about configuring the deployment descriptor for Struts
2 see <code class="highlighter-rouge">web.xml</code>. Note the url-pattern node
value is <code class="highlighter-rouge">/*</code> meaning the Struts 2 filter
will be applied to all URLs for this web application.</p>
-<p><strong>Step 6 - Create struts.xml</strong></p>
+<h4 id="step-6---create-strutsxml">Step 6 - Create struts.xml</h4>
<p>Struts 2 can use either an XML configuration file or annotations (or both)
to specify the relationship between a URL,
a Java class, and a view page (such as <code
class="highlighter-rouge">index.jsp</code>). For our basic Struts 2
application, weâll use a minimal xml
@@ -300,11 +331,9 @@ must be on the web applicationâs ro
<span class="nt"><constant</span> <span class="na">name=</span><span
class="s">"struts.devMode"</span> <span class="na">value=</span><span
class="s">"true"</span> <span class="nt">/></span>
<span class="nt"><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">></span>
-
<span class="nt"><action</span> <span class="na">name=</span><span
class="s">"index"</span><span class="nt">></span>
<span class="nt"><result></span>/index.jsp<span
class="nt"></result></span>
<span class="nt"></action></span>
-
<span class="nt"></package></span>
<span class="nt"></struts></span>
@@ -316,32 +345,28 @@ the browser to <code class="highlighter-
<p>For more information about the struts.xml configuration file see <code
class="highlighter-rouge">struts.xml</code>.</p>
-<p><strong>Step 7 - Build and Run the Application</strong></p>
+<h4 id="step-7---build-and-run-the-application">Step 7 - Build and Run the
Application</h4>
-<p>With all of the above in place run mvn clean package to create the war
file. Remove the previously created war file
-and exploded web application folder from your Servlet containerâs webapps
folder. Copy to your Servlet containerâs webapps
-folder the new war you just created.</p>
+<p>Run <code class="highlighter-rouge">mvn jetty:run</code> to run the
web-application using the jetty maven-plugin.</p>
-<p>Start up the Servlet container. View the console where you should see
numerous debug messages that tell you
-the Struts 2 framework is being included in the <code
class="highlighter-rouge">Basic_Struts2_Mvn</code> web application.</p>
+<p>View the console where you should see numerous debug messages that tell you
the Struts 2 framework is being included in the <code
class="highlighter-rouge">basic-struts2</code> web application.</p>
-<p>Open a web browser and go to <a
href="http://localhost:8080/Basic_Struts2_Mvn/index.action">http://localhost:8080/Basic_Struts2_Mvn/index.action</a>
+<p>Open a web browser and go to <a
href="http://localhost:8080/basic-struts/index.action">http://localhost:8080/basic-struts/index.action</a>
(note thatâs <code class="highlighter-rouge">index.action</code> not <code
class="highlighter-rouge">index.jsp</code> at the end of the URL). You should
see the same web page as when going to
-<a
href="http://localhost:8080/Basic_Struts2_Mvn/index.jsp">http://localhost:8080/Basic_Struts2_Mvn/index.jsp</a>.
View the log
+<a
href="http://localhost:8080/basic-struts/index.jsp">http://localhost:8080/basic-struts/index.jsp</a>.
View the log
messages written to the console and you should find several that discuss <code
class="highlighter-rouge">index.action</code> and <code
class="highlighter-rouge">index.jsp</code>:</p>
<p><strong>Struts 2 Log Messages</strong></p>
-<div class="highlighter-rouge"><pre
class="highlight"><code>com.opensymphony.xwork2.DefaultActionProxy.debug:57 -
Creating an DefaultActionProxy for namespace / and action name index
+<div class="highlighter-rouge"><pre class="highlight"><code>...
+2017-04-17 11:16:01,084 DEBUG [qtp1723848804-22] xwork2.DefaultActionProxy
(DefaultActionProxy.java:89) - Creating an DefaultActionProxy for namespace [/]
and action name [index]
+...
+2017-04-17 11:16:01,172 DEBUG [qtp1723848804-22]
result.ServletDispatcherResult (ServletDispatcherResult.java:131) - Forwarding
to location: /index.jsp
...
-org.apache.struts2.dispatcher.ServletDispatcherResult.debug:57 - Forwarding to
location /index.jsp
</code></pre>
</div>
-<p>Note that the xwork2 artifact is one of the Jar files that is a transitive
dependency for the struts2-core Jar file.
-The xwork2 library is used heavily by the Struts 2 framework.</p>
-
-<p><strong>Getting Help</strong></p>
+<h3 id="getting-help">Getting Help</h3>
<p>The <a href="/mail.html">Struts 2 user mailing list</a> is an excellent
place to get help. If you are having a problem getting
this Basic Struts 2 application to work search the Struts 2 mailing list. If
you donât find an answer to your problem,