| <!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>How to create a Struts 2 web application</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/getting-started/how-to-create-a-struts2-web-application.md" title="Edit this page on GitHub">Edit on GitHub</a> |
| |
| <a href="index.html" title="back to Getting started"><< back to Getting started</a> |
| |
| <h1 class="no_toc" id="how-to-create-a-struts-2-web-application">How To Create A Struts 2 Web Application</h1> |
| |
| <ul id="markdown-toc"> |
| <li><a href="#java-requirements" id="markdown-toc-java-requirements">Java Requirements</a></li> |
| <li><a href="#our-first-application" id="markdown-toc-our-first-application">Our First Application</a></li> |
| <li><a href="#create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application" id="markdown-toc-create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application">Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application</a> <ul> |
| <li><a href="#step-1---create-a-java-web-application" id="markdown-toc-step-1---create-a-java-web-application">Step 1 - Create A Java Web Application</a> <ul> |
| <li><a href="#to-run-the-application-using-maven-add-the-jetty-maven-plugin-to-your-pomxml" id="markdown-toc-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</a></li> |
| </ul> |
| </li> |
| <li><a href="#step-2---add-indexjsp" id="markdown-toc-step-2---add-indexjsp">Step 2 - Add index.jsp</a></li> |
| <li><a href="#step-3---add-struts-2-jar-files-to-class-path" id="markdown-toc-step-3---add-struts-2-jar-files-to-class-path">Step 3 - Add Struts 2 Jar Files To Class Path</a></li> |
| <li><a href="#step-4---add-logging" id="markdown-toc-step-4---add-logging">Step 4 - Add Logging</a></li> |
| <li><a href="#step-5---add-struts-2-servlet-filter" id="markdown-toc-step-5---add-struts-2-servlet-filter">Step 5 - Add Struts 2 Servlet Filter</a></li> |
| <li><a href="#step-6---create-strutsxml" id="markdown-toc-step-6---create-strutsxml">Step 6 - Create struts.xml</a></li> |
| <li><a href="#step-7---build-and-run-the-application" id="markdown-toc-step-7---build-and-run-the-application">Step 7 - Build and Run the Application</a></li> |
| </ul> |
| </li> |
| <li><a href="#getting-help" id="markdown-toc-getting-help">Getting Help</a></li> |
| </ul> |
| |
| <p>This tutorial walks through installing the framework and creating a simple application.</p> |
| |
| <p>While the Struts 2 framework is simple to use, creating non-trivial applications assumes a working knowledge |
| of many J2EE technologies, including:</p> |
| |
| <ul> |
| <li>Java</li> |
| <li>Filters, JSP, and Tag Libraries</li> |
| <li>JavaBeans</li> |
| <li>HTML and HTTP</li> |
| <li>Web Containers (such as Tomcat)</li> |
| <li>XML</li> |
| </ul> |
| |
| <blockquote> |
| <p>For more about supporting technologies, see the <a href="../primer.html">Key Technologies Primer</a>.</p> |
| </blockquote> |
| |
| <h2 id="java-requirements">Java Requirements</h2> |
| |
| <p>Struts 2 requires Servlet API 2.4 or higher, JSP 2.0 or higher, and Java 7 or higher.</p> |
| |
| <h2 id="our-first-application">Our First Application</h2> |
| |
| <p>To get started using Struts 2 we will create a web application using Maven to manage the artifact dependencies. |
| You can checkout all the example applications from the Struts 2 GitHub repository |
| at <a href="https://github.com/apache/struts-examples">struts-examples</a>.</p> |
| |
| <h2 id="create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application">Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application</h2> |
| |
| <blockquote> |
| <p>This tutorial assumes you know how to create a Java web application that uses Maven to manage artifacts and build |
| the web application archive (war) file.</p> |
| </blockquote> |
| |
| <h3 id="step-1---create-a-java-web-application">Step 1 - Create A Java Web Application</h3> |
| |
| <p>In your Java IDE create a Java web application with a project name of <code class="highlighter-rouge">basic_struts</code> that follows the standard Maven |
| 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 class="language-xml highlighter-rouge"><div class="highlight"><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></div> |
| |
| <h5 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</h5> |
| |
| <p><strong>pom.xml jetty plugin</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><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.eclipse.jetty<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>jetty-maven-plugin<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>9.4.7.v20170914<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></div> |
| <p>The above plugin will enable you to run the application using <code class="highlighter-rouge">mvn jetty:run</code></p> |
| |
| <h3 id="step-2---add-indexjsp">Step 2 - Add index.jsp</h3> |
| |
| <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><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>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/basic_struts2_maven_jsp.png" alt="basic_struts2_maven_jsp.png" /></p> |
| |
| <h3 id="step-3---add-struts-2-jar-files-to-class-path">Step 3 - Add Struts 2 Jar Files To Class Path</h3> |
| |
| <p>Now that we know we have a working Java web application, let’s 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> |
| |
| <p><strong>pom.xml dependency node</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.struts<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>struts2-core<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${struts2.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| </code></pre></div></div> |
| |
| <p>Of course replace the <code class="highlighter-rouge">${struts2.version}</code> with the current Struts 2 version (or define within pom <code class="highlighter-rouge">properties</code>). |
| Maven will get the <code class="highlighter-rouge">struts2-core</code> jar and the other jar |
| files struts2-core requires (transitive dependencies).</p> |
| |
| <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> |
| |
| <h3 id="step-4---add-logging">Step 4 - Add Logging</h3> |
| |
| <p>To see what’s happening under the hood, the example application for this tutorial uses log4j2. |
| 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> |
| |
| <p><strong>log4j2.xml</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><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></div> |
| |
| <p>Note the above log4j2 configuration specifies the console as the log target.</p> |
| |
| <p>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="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></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>${log4j2.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.logging.log4j<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>log4j-api<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${log4j2.version}<span class="nt"></version></span> |
| <span class="nt"></dependency></span> |
| </code></pre></div></div> |
| |
| <p>Using both <code class="highlighter-rouge">log4j-core</code> and <code class="highlighter-rouge">log4j-api</code> allows to use <a href="//logging.apache.org/log4j/2.x/maven-artifacts.html">the latest version of Log4j2</a> without a clash with version provided by the framework.</p> |
| |
| <p>Optionally, if using maven bom “bill of materials” in <code class="highlighter-rouge">dependencyManagement</code> section for both Struts and log4j2, |
| <code class="highlighter-rouge">pom.xml</code> will look like. Note that this way you can omit <code class="highlighter-rouge">version</code> line for every used module, |
| and all <code class="highlighter-rouge">struts2-*</code> and <code class="highlighter-rouge">log4j-*</code> modules are managed to be of the same version. |
| The <code class="highlighter-rouge">struts2-bom</code> is available since 2.3.20.</p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><properties></span> |
| <span class="nt"><project.build.sourceEncoding></span>UTF-8<span class="nt"></project.build.sourceEncoding></span> |
| <span class="nt"><project.reporting.outputEncoding></span>UTF-8<span class="nt"></project.reporting.outputEncoding></span> |
| <span class="nt"><java.version></span>1.8<span class="nt"></java.version></span> |
| |
| <span class="nt"><struts2.version></span>2.5.14.1<span class="nt"></struts2.version></span> |
| <span class="nt"><log4j2.version></span>2.10.0<span class="nt"></log4j2.version></span> |
| <span class="nt"></properties></span> |
| |
| <span class="nt"><dependencyManagement></span> |
| <span class="nt"><dependencies></span> |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.struts<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>struts2-bom<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${struts2.version}<span class="nt"></version></span> |
| <span class="nt"><type></span>pom<span class="nt"></type></span> |
| <span class="nt"><scope></span>import<span class="nt"></scope></span> |
| <span class="nt"></dependency></span> |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.logging.log4j<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>log4j-bom<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>${log4j2.version}<span class="nt"></version></span> |
| <span class="nt"><scope></span>import<span class="nt"></scope></span> |
| <span class="nt"><type></span>pom<span class="nt"></type></span> |
| <span class="nt"></dependency></span> |
| <span class="nt"></dependencies></span> |
| <span class="nt"></dependencyManagement></span> |
| |
| <span class="nt"><dependencies></span> |
| <span class="c"><!-- Struts 2 --></span> |
| <span class="nt"><dependency></span> |
| <span class="nt"><groupId></span>org.apache.struts<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>struts2-core<span class="nt"></artifactId></span> |
| <span class="nt"></dependency></span> |
| |
| <span class="nt"><dependency></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"></dependency></span> |
| <span class="nt"></dependencies></span> |
| </code></pre></div></div> |
| |
| <h3 id="step-5---add-struts-2-servlet-filter">Step 5 - Add Struts 2 Servlet Filter</h3> |
| |
| <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 how the <code class="highlighter-rouge">web.xml</code> may look after adding the filter and filter-mapping nodes. <code class="highlighter-rouge">web.xml</code> is to be under <code class="highlighter-rouge">src/main/webapp/WEB-INF</code> folder.</p> |
| |
| <p><strong>web.xml Servlet Filter</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp"><?xml version="1.0" encoding="UTF-8"?></span> |
| <span class="nt"><web-app</span> <span class="na">id=</span><span class="s">"WebApp_ID"</span> <span class="na">version=</span><span class="s">"2.4"</span> |
| <span class="na">xmlns=</span><span class="s">"http://java.sun.com/xml/ns/j2ee"</span> |
| <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span> |
| <span class="na">xsi:schemaLocation=</span><span class="s">"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span><span class="nt">></span> |
| <span class="nt"><display-name></span>Basic Struts2<span class="nt"></display-name></span> |
| <span class="nt"><welcome-file-list></span> |
| <span class="nt"><welcome-file></span>index<span class="nt"></welcome-file></span> |
| <span class="nt"></welcome-file-list></span> |
| |
| <span class="nt"><filter></span> |
| <span class="nt"><filter-name></span>struts2<span class="nt"></filter-name></span> |
| <span class="nt"><filter-class></span>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter<span class="nt"></filter-class></span> |
| <span class="nt"></filter></span> |
| |
| <span class="nt"><filter-mapping></span> |
| <span class="nt"><filter-name></span>struts2<span class="nt"></filter-name></span> |
| <span class="nt"><url-pattern></span>/*<span class="nt"></url-pattern></span> |
| <span class="nt"></filter-mapping></span> |
| |
| <span class="nt"></web-app></span> |
| </code></pre></div></div> |
| |
| <p>For more information about configuring the deployment descriptor for Struts 2 see <a href="/core-developers/web-xml.html">Core Developers Guide / web.xml page</a>. 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> |
| |
| <h3 id="step-6---create-strutsxml">Step 6 - Create struts.xml</h3> |
| |
| <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 |
| configuration. Note the file name is <code class="highlighter-rouge">struts.xml</code> and it should be in the <code class="highlighter-rouge">src/main/resources</code> folder (<code class="highlighter-rouge">struts.xml</code> |
| must be on the web application’s root class path).</p> |
| |
| <p><strong>struts.xml</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><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.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"><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> |
| </code></pre></div></div> |
| |
| <p>This minimal Struts 2 configuration file tells the framework that if the URL ends in <code class="highlighter-rouge">index.action</code> to redirect |
| the browser to <code class="highlighter-rouge">index.jsp</code>.</p> |
| |
| <p>For more information about the struts.xml configuration file see <code class="highlighter-rouge">struts.xml</code>.</p> |
| |
| <h3 id="step-7---build-and-run-the-application">Step 7 - Build and Run the Application</h3> |
| |
| <p>Run <code class="highlighter-rouge">mvn jetty:run</code> to run the web-application using the jetty maven-plugin.</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-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-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"><div class="highlight"><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 |
| ... |
| </code></pre></div></div> |
| |
| <h2 id="getting-help">Getting Help</h2> |
| |
| <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, |
| post a question on the mailing list.</p> |
| |
| <table> |
| <tbody> |
| <tr> |
| <td>Return to <a href="index.html">Getting started</a></td> |
| <td>or</td> |
| <td>onward to <a href="hello-world-using-struts2.html">Hello World using Struts 2</a></td> |
| </tr> |
| </tbody> |
| </table> |
| |
| </section> |
| </article> |
| |
| |
| <footer class="container"> |
| <div class="col-md-12"> |
| Copyright © 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> |