Tech I Enjoy Logo

Custom Search




  Home >> Struts


My First learning example using Apache Struts2 :

I have tried to put/assemble a step by step methods to build
a sample/example web application on Struts2 MVC Architecture.

I guess many software professional working on projects using
earlier versions of Struts, may find this writeup as a simple
start or a blank web application to start using Struts2.

Prior to this writing I had used Apache Struts 1.x version,
but quite interestingly I have managed to come up with ways to
put various pieces of code to seamlessly flow request from
browser to Struts2 filter , then to the appropriate Action,
and then to the appropriate JSP file for display.

-> In Struts2, Action can be a plain POJO and could be coded to be aware of many different ways, such as ParameterAware for receiving HTTP POST/GET request parameters, SessionAware for receiving HTTP Session values/objects stored within, ApplicationWare for managing application level properties of web application. -> In Struts2, There can be no mention of Struts-Config file, instead a struts.xml file stored in WEB-INF\classes folder. -> In Struts2, there can be module aware code by using packages and namespace values. Software environment used for this example : 1. JDK 5.0 2. Tomcat 5.5.x 3. Eclipse 4. Apache Struts 2.1.6 (example Web application should have following JAR files in WEB-INF\lib folder) 4.1. commons-fileupload-1.2.1.jar 4.2. commons-io-1.3.2.jar 4.3. commons-logging-1.1.jar 4.4. freemarker-2.3.13.jar 4.5. ognl-2.6.11.jar 4.6. struts2-core-2.1.6.jar 4.7. xwork-2.1.2.jar (Note: This example is not tested with any other version of Apache Struts ) I have to write following files for this example: 1. MyExample\WEB-INF\classes\struts.xml 2. MyExample\WEB-INF\web.xml 3. MyExample\WEB-INF\classes\sample\MyExample.java 4. MyExample\good.jsp 5. MyExample\bad.jsp 6. MyExample\visitor.jsp 7. MyExample\index.html index.html file is just having HTML tags for now, I shall be using Struts2 tag library in my forth comming tutorials.
<html>
<body>
<h4>Apache Struts2 Sample Application</h4>
<form method="post" action="/MyExample/sample/myexample">
<input type="text" name="txtUserName"><br>
<input type="submit">
</form>
</body>
</html>
For above action "MyExample/sample/myexample" can be devided into three parts, such as follows: 1. MyExample as the web context root in web.xml 2. sample as the namespace in struts.xml file 3. myexample is the action name in struts.xml file. So HTTP request is intercepted by the customized Filter (defined in web.xml file) web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <filter>
  <filter-name>sample-filter</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sample-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
Then appropriate Action POJO is called as mentioned in struts.xml file: struts.xml

<?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>
    <package name="goodexample" namespace="/sample"
	                            extends="struts-default">
        <action name="myexample" class="sample.MyExample">
	        <result name="good">/good.jsp</result>
	        <result name="bad">/bad.jsp</result>
            <result name="visitor">/visitor.jsp</result>
	    </action>
    </package>
</struts>
These results in the action, those are something like, "forwards" in earlier versions of Struts (like in Struts 1.x version) Action POJO is as follows: sample.MyExample.java

package sample;

/**
 * This code is provided "AS IS" without any guaranty
 * Use of this code only for educational and learning
 * purpose only.
 * Author: Amit
 * Contact : usingframeworks@gmail.com
 * Date: 16-Feb-2009
 */
 
import org.apache.struts2.interceptor.ParameterAware;
import java.util.Map;

/**
 * This is the Action POJO class
 */
public class MyExample implements ParameterAware {
	private Map parameters;
	public Map getParameters() {
        return parameters;
    }
    public void setParameters(Map param) {
        this.parameters = param;
    }
    public String execute() throws Exception {
        Map map = getParameters();
        String[] tmp = (String[])map.get("txtUserName");
        if(tmp[0].contains("good"))
          return "good";
        if(tmp[0].contains("bad"))
          return "bad";
        else
          return "visitor";
    }
}
For those professionals who have already used Struts 1.x version, might have many questions (even I had many questions too) in mind after looking at this Action class, neither it is extends any Action class from Struts nor is it having any sign of using ActionMapping and ActionForward and ActionErrors in the method signature of execute method. If ActionForm is not there in this Action POJO, then how am I going to receive the request parameters?? to answer this question, one of the solution is, by using ParameterAware interface to get all the request parameters packed in Map, but there is another solution as well, whereby using Struts2 Tag Library and thus making Action a plain POJO, and just by using similar names in Tag key and in Action class for all the fields used. More on this will be coming soon on some other example in this site. Please be informed that implementation of execute method is made as simple as possible, as it is just doing a parsing on the txtUserName request parameter value to find "good"/"bad"/"visitor" word and direct request to appropriate result / JSP pages (as defined in struts.xml file). I know, you might be thinking that How about using Spring to inject domain objects to the Action class and Validator Frameworks with Tiles. All these will be incorporated in this simple example, but gradually. Please visit again and let us know your valuable comments on this to improve. Thanks, :) If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /Struts2-example-steps.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
and code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.


References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions