<div dir="ltr" style="text-align: justify;">
<p>Hi in this tutorial we will discuss about the <b>Hello World Struts 2 Application</b>, there are the following minimum technical requirements for this <b>Hello World Application</b>.</p>
<p>Before we starts with our first Hello World Struts 2 Example, we will need few tools.</p>
<ol>
<li>JDK 1.5 above (<a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank" rel="noopener">download</a>)</li>
<li>Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (<a href="http://tomcat.apache.org/download-55.cgi" target="_blank" rel="noopener">download</a>)</li>
<li>Eclipse 3.2.x above (<a href="http://www.eclipse.org/downloads/" target="_blank" rel="noopener">download</a>)</li>
<li>Apache Struts2 JAR files:(<a href="http://mirror.switch.ch/mirror/apache/dist/struts/library/struts-2.0.14-lib.zip" target="_blank" rel="noopener">download</a>).</li>
</ol>
<p>Following are the list of JAR files required for this application.</p>
<ul>
<li>struts2-core-2.3.15</li>
<li>xwork-core-2.3.15</li>
<li>ognl-3.4</li>
<li>commons-io-2.0.1</li>
<li>commons-fileupload-1.2.2</li>
<li>javassist-3.11.0.GA</li>
<li>freemarker-2.3.18</li>
<li>commons-lang-2.5</li>
<li>commons-logging-1.1.1</li>
<li>commons-logging-api-1.1</li>
<li>struts2-dojo-plugin-2.3.15.jar</li>
</ul>
<p>Note that depending on the current version of Struts2, the version number of above jar files may change.</p>
<div id="ads-id" align="center"></div>
<p>Here we want to create a basic Struts2 application with a Login page. User will enter login credential and if authenticated successfully she will be redirected to a Welcome page which will display message &#8220;<i><b>Hello Welcome , </b><;username>;<i><b>&#8230;! Dineshonjava.com</b></i>&#8220;. If user is not authenticated, she will be redirected back to the login page.</i></p>
<h2><b>1. Final project structure</b></h2>
<p>In the <b><a href="https://dineshonjava.com/2013/07/setting-up-struts-2-in-eclipse.html" target="_blank" rel="noopener">previous chapter we already set up the struts 2 in the eclipse or STS</a></b> Now lets review the final project structure of this tutorial, in case you get lost in later steps.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/direStruts.png" border="0" /></div>
<h2><b>2. Mapping Struts2 in <i>web.xml</i></b></h2>
<p>As discussed in the<b><a href="https://dineshonjava.com/2013/07/introduction-to-struts-2-framework.html" target="_blank" rel="noopener"> previous article</a></b>, the entry point of Struts2 application will be the Filter define in deployment descriptor<i><b> (web.xml)</b></i>. Hence we will define an entry of <i><b>org.apache.struts2.dispatcher.FilterDispatcher</b></i> class in <i><b>web.xml</b></i>.</p>
<p><i><b>web.xml</b></i></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2MyFirstApp<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;Login.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
<p>The above code in <i><b>web.xml</b></i> will map Struts2 filter with url /*. The default url mapping for struts2 application will be <i><b>/*.action</b></i>. Also note that we have define <i><b>Login.jsp</b></i> as welcome file.</p>
<h2><b>3. Create Action Class:</b></h2>
<p>Action class is the key to Struts 2 application and we implement most of the business logic in action class. So let us create a java file<i><b> LoginAction.java</b></i> under <i><b>Java Resources >; src</b></i> with a package name <i><b>com.dineshonjava.struts2.login</b></i> with the contents given below.</p>
<p>The Action class responds to a user action when user clicks a URL. One or more of the Action class&#8217;s methods are executed and a String result is returned. Based on the value of the result, a specific JSP page is rendered.<br />
<i><b>LoginAction.java</b></i></p>
<pre class="highlight">package com.dineshonjava.struts2.login; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@SuppressWarnings("serial") 
public class LoginAction extends ActionSupport{ 
 private String username; 
 private String password; 
 
 public String execute() { 
 
 if (this.username.equals("dinesh") 
 &;&; this.password.equals("sweety")) { 
 return "success"; 
 } else { 
 addActionError(getText("error.login")); 
 return "error"; 
 } 
 } 
 
 public String getUsername() { 
 return username; 
 } 
 
 public void setUsername(String username) { 
 this.username = username; 
 } 
 
 public String getPassword() { 
 return password; 
 } 
 
 public void setPassword(String password) { 
 this.password = password; 
 } 
 
} 
</pre>
<p><b>Note that,</b> above action class contains two fields, <b>username </b>and <b>password </b>which will hold the values from form and also contains an <i><b>execute()</b></i> method that will authenticate the user. In this simple example, we are checking if username is &#8220;<i><b>dinesh</b></i>&#8221; and password is &#8220;<i><b>sweety</b></i>&#8220;.<br />
Also note that unlike Action class in Struts1, Struts2 action class is a simple POJO class with required attributes and method.<br />
<i><b>Note: </b></i>The <i><b>execute() </b></i>method returns a String value which will determine the result page. <b>Also, in Struts2 the name of the method is not fixed</b>. In this example we have define method execute(). You may want to define a method <i><b>doAuthenticate()</b></i> instead.</p>
<h2><b>4. Create a View</b></h2>
<p>We need a JSP to present the final message, this page will be called by Struts 2 framework when a predefined action will happen and this mapping will be defined in <i><b>struts.xml</b></i> file. So let us create the below jsp file <i><b>Login.jsp and Welcome.jsp </b></i>in the <b>WebRoot</b> folder in your eclipse project. To do this, right click on the <b>WebRoot</b> folder in the project explorer and select <b>New >;JSP</b> File.<br />
<i><b>Login.jsp</b></i></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Struts 2 - Login Application | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
<;h2>;Struts 2 - Login Application<;/h2>; 
<;s:actionerror />; 
<;s:form action="login.action" method="post">; 
 <;s:textfield name="username" key="label.username" size="20" />; 
 <;s:password name="password" key="label.password" size="20" />; 
 <;s:submit method="execute" key="label.login" align="center" />; 
<;/s:form>; 
<;/body>; 
<;/html>; 
</pre>
<p><i><b>Welcome.jsp</b></i></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Welcome<;/title>; 
<;/head>; 
 
<;body>; 
 <;h2>;Hello Welcome , <;s:property value="username" />;...! Dineshonjava.com<;/h2>; 
<;/body>; 
<;/html>; 
</pre>
<p>Note that we have used struts2<b> <;s:>;</b> tag to render the <b>textboxes </b>and <b>labels</b>. Struts2 comes with a powerful built-in tag library to render UI elements more efficiently.</p>
<h2><b>5. The <i>ResourceBundle</i></b></h2>
<p><i><b>ResourceBundle </b></i>is very useful Java entity that helps in putting the static content away from the source file. Most of the application define a resource bundle file such as <i><b>myapp.properties</b></i> file which contains static messages such as <i><b>Username or Password </b></i>and include this with the application.</p>
<p><i><b>ResourceBundle </b></i>comes handy when we want to add <i><b>Internationalization (I18N)</b></i> support to an application.</p>
<p>We will define an <i><b>myapp.properties</b></i> file for our application. This property file should be present in <i><b>WEB-INF/classes</b></i> folders when the source is compiled. Thus we will create a source folder called resources and put the ApplicationResources.properties file in it.</p>
<p>To create a source folder, right click on your project in Project Explorer and select <i><b>New ->; Source Folder</b></i>.</p>
<p><i><b>myapp.properties</b></i></p>
<pre class="highlight">label.username= Username 
label.password= Password 
label.login= Login 
error.login= Invalid Username/Password. Please try again.</pre>
<p>We need to add logic in <i><b>LoginAction </b></i>to add error message if user is not authenticated. But there is one problem. Our error message is specified in <i><b>myapp.properties </b></i>file. We must specify key <i><b>error.login</b></i> in <i><b>LoginAction </b></i>and the message should be displayed on JSP page.</p>
<p>For this we must implement <i><b>com.opensymphony.xwork2.TextProvider</b></i> interface which provides method <i><b>getText()</b></i>. This method returns String value from resource bundle file. We just have to pass the key value as argument to <i><b>getText()</b></i> method. The <i><b>TextProvider </b></i>interface defines several method that we must implement in order to get hold on<i><b> getText() </b></i>method. But we don’t want to spoil our code by adding all those methods which we do not intend to use. There is a good way of dealing with this problem.</p>
<p>Struts2 comes with a very useful class<i><b> com.opensymphony.xwork2.ActionSupport</b></i>. We just have to extend our <i><b>LoginAction </b></i>class with this class and directly use methods such as <i><b>getText(), addActionErrors()</b></i> etc. Thus we will extend the <i><b>LoginAction </b></i>class with <i><b>ActionSupport </b></i>class and add the logic for error reporting into it.</p>
<h2><b>6. Configuration Files</b></h2>
<p>We need a mapping to tie the URL, the <i><b>LoginAction class (Model)</b></i>, and the <i><b>Login.jsp (the view)</b></i> together. The mapping tells the Struts 2 framework which class will respond to the user&#8217;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>So let us create a file called <i><b>struts.xml</b></i>. Since Struts 2 requires <i><b>struts.xml</b></i> to be present in classes folder. So create <i><b>struts.xml </b></i>file under the <i><b>WebRoot/WEB-INF/classes</b></i> folder. <i><b>Eclipse or STS</b></i> does not create the &#8220;classes&#8221; folder by default, so you need to do this yourself. To do this, right click on the <i><b>WEB-INF</b></i> folder in the project explorer and select<i><b> New >; Folder</b></i>.<br />
<b>Your <i>struts.xml</i> should look like:</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" ?>; 
<;!DOCTYPE struts PUBLIC 
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
 "http://struts.apache.org/dtds/struts-2.0.dtd">; 
 
<;struts>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="login" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;Welcome.jsp<;/result>; 
 <;result name="error">;Login.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
<;/struts>; 
</pre>
<p>Note that in above configuration file, we have defined <i><b>Login </b></i>action of our application. Two result paths are mapped with <i><b>LoginAction </b></i>depending on the outcome of <i><b>execute()</b></i> method. If <i><b>execute() </b></i>method returns success, user will be redirected to <i><b>Welcome.jsp else to Login.jsp</b></i>.</p>
<p>Also note that a constant is specified with name <i><b>struts.custom.i18n.resources</b></i>. This constant specify the resource bundle file that we created in above steps. We just have to specify name of resource bundle file without extension (<b><i>myapp </i>without .properties</b>).</p>
<p>Our <i><b>LoginAction </b></i>contains the method <i><b>execute()</b></i> which is the default method getting called by Sturts2. If the name of method is different, e.g.<i><b> doAuthenticate()</b></i>; then we should specify the method name in <i><b><;action>; </b></i>tag.</p>
<pre class="highlight"><;action name="login" method="doAuthenticate" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;Welcome.jsp<;/result>; 
 <;result name="error">;Login.jsp<;/result>; 
 <;/action>; 
</pre>
<h2><b>7. Execute the Application</b></h2>
<p>Right click on the project name and click <i><b>Export >; WAR </b></i>File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access URL<br />
<i><b>http://localhost:8080/doj/Login.jsp</b></i>. This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/stepst.png" border="0" /></div>
<p><b>Fill username &#8220;dinesh&#8221; and password &#8220;sweety&#8221; and submit then go to welcome page.</b><br />
<i><b> </b></i><b><i>http://localhost:8080/doj/login.action</i></b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/stepst2.png" border="0" /></div>
<p><b><i>I</i></b><i>f you <b>put wrong username and password</b></i><b><i> then return error login page with validation message.</i></b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/stepst3.png" border="0" /></div>
<p><b><i> </i> </b></p>
<h2><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2MyFirstApp.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>Struts2MyFirstApp.zip</b></a></h2>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/setting-up-struts-2-in-eclipse/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts-2-configuration-file/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/setting-up-struts-2-in-eclipse/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts-2-configuration-file/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '399'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…