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