Home >> Struts
My First learning example using Apache Struts2 :
If you are using Struts2 Tag library for writing JSP or UI, then there is not need
for the Action POJO, either to extend ActionSupport or implement any ***Aware
interfaces (Such as ParameterAware), in order to be able to receive HTTP request
parameters from JSP pages.
In this Struts 2 Example, I am going to demonstrate step by step ways, Struts 2
Tag Library can be used to reflect in the Action POJO or class.
Software environment I have used for this example, so far as follows:
1. JDK 5.0
2. Tomcat 5.5.x
3. Eclipse 3.2
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 )
|
|  |
|
By this, unlike in Struts 1.x version, Action class could be having only
business related code, and not required to include code related HttpServletRequest,
HttpServletResponse, ActionMapping or ActionForm to receive User entered values
in Text Fields in UI (User Interface).
Like any other example, as shown/found in this site, one has to setup web application
folder structure inside TOMCAT_HOME\webapps folder, with appropriate JAR files inside
lib folder for Struts2 to execute successfully.
I have placed following set of JAR files for my example,

As discussed already, intent of this example is to show user a simple UI for entering
data, and this data has to go in instance variable of Action POJO class and then
a preview page is to be shown to user with all the values, those are entered by user.

Both the JSP pages, as shown below are using Apache Struts2 Tag library:
index.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h4>Example Registration Page</h4>
<struts2:form method="post" action="myexample" namespace="/sample">
<table>
<tr><td><struts2:textfield key="userName" label="Name "/></td></tr>
<tr><struts2:textfield key="roll" label="Roll "/></td></tr>
<tr><td><struts2:textfield key="section" label="Section "/></td></tr>
<tr><struts2:textfield key="stdClass" label="Std/Class "/></td></tr>
<tr><td colspan="2"><struts2:submit value="Register"/></td></tr>
</table>
</struts2:form>
</body>
</html>
|
and
preview.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<head></head>
<body>
<h2>Preview Page</h2>
<font color="green">
<b>Name : <struts2:property value="userName"/></b><br>
<b>Roll No. : <struts2:property value="roll"/></b><br>
<b>Section : <struts2:property value="section"/></b><br>
<b>Std/Class : <struts2:property value="stdClass"/></b><br>
</font>
|
One important point to notice here is that, all the properties
"userName", "roll", "section" and "stdClass" are same as the values for the
key element in index.jsp file.
On submit of index.jsp file, the action that is going to be called is "myexample"
for the namespace as "/sample", as defined in struts.xml file, as shown below:
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" method="preview">
<result name="preview">/preview.jsp</result>
</action>
</package>
</struts>
|
As per this struts.xml file, Action class is sample.MyExample, and preview
is the method that will be called upon.
MyExample is a plain POJO with fields as instance varibale with the same name
as defined in index.jsp and preview.jsp files.
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: 08-April-2009
*/
/**
* This is the Action POJO class
*/
public class MyExample {
private String userName;
private String roll;
private String section;
private String stdClass;
public String preview() throws Exception {
return "preview";
}
public String getUserName() {
return this.userName;
}
public void setUserName(String argUserName) {
userName = argUserName;
}
public String getRoll() {
return this.roll;
}
public void setRoll(String argRoll) {
roll = argRoll;
}
public String getSection() {
return this.section;
}
public void setSection(String argSection) {
section = argSection;
}
public String getStdClass() {
return this.stdClass;
}
public void setStdClass(String argStdClass) {
stdClass = argStdClass;
}
}
|
In order to setup Struts2 in a web application, following listener can be used
in web.xml deployment descriptor file , as follows:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<display-name>Struts 2 Example</display-name>
<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>
|
Once all the required files are placed in folder structure as per
this example , and as shown below:
<<TOMCAT_HOME>>/webapps/sample3/index.jsp
<<TOMCAT_HOME>>/webapps/sample3/preview.jsp
<<TOMCAT_HOME>>/webapps/sample3/WEB-INF/lib/All Jar files
<<TOMCAT_HOME>>/webapps/sample3/WEB-INF/web.xml
<<TOMCAT_HOME>>/webapps/sample3/WEB-INF/classes/struts.xml
<<TOMCAT_HOME>>/webapps/sample3/WEB-INF/classes/sample/MyExample.class
On successful starting Tomcat, without any error in log file, following URL
showed me my example main page.
http://localhost:8080/sample3
If you have any questions related to this example, please share your question
in this Page as comment or reply.
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-Tag-usage.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