Struts2

Writing a Struts 2 Hello World Application

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

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago