Tech I Enjoy Logo

Custom Search




  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
Apache Struts 2 upload Example :
Uploading Example Steps of using Struts 2
and Example code explained.
Apache Struts 2 Tags Example :
Example of Struts 2 Tags and code explained.
Apache Struts 2 Tags Example :
Example Steps of using Struts 2 Tags
and code explained.
Apache Struts with DOJO Example Part-1 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts Validation with Example :
Example of Validation using Struts and 
code explained.
Struts Validation Example :
Example using Validation with Struts 2 and 
code explained.
Apache Struts with DOJO Example Part-2 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Custom Validator :
Example Steps of using Struts 2 with Custom Validator
and code explained.
Apache Struts 2 and XSLT With Example :
XSLT Example using Struts 2 and Example 
code explained.
Apache Struts Date Validation with Example :
Example of Date Validation using Struts and 
code explained.
Apache Struts 2 Result PlainText Example :
Example Steps of using Struts 2 PlainText Result
and code explained.
Struts 2 Tiles and I18N Example :
Example using Tiles and I18N with Struts 2 and 
code explained.
Apache Struts 2 PDF Result :
Example Steps of using Struts 2 with PDF Result
and code explained.
Apache Struts with DOJO Example Part-3 :
Example of DOJO Toolkit with Struts and 
code explained.
List of Struts Example :
Examples List using Struts 2 and 
code explained.
Apache Struts 2 Validation With Example :
Validation Example using Struts 2
and Example code explained.
Apache Struts 2 Validation Example :
Validation Example Steps of using Struts 2
and Example code explained.
Struts 2 Plugin Example :
Example using Struts 2 Plugin
Struts 2 with JSF Example :
Example using JSF with Struts 2 and 
code explained.
Apache Struts 2 Example Steps :
Example Steps of using Struts 2 and code explained.
Apache Struts 2 Validation With Expression :
Validation with Expression using Struts 2
and Example code explained.
Struts 2 Result Chain Example :
Example using Result Chain with Struts 2 and 
code explained.
Apache Struts 2 Tiles and I18N Example :
Example Steps of using Struts 2 With Tiles
and I18N code explained.
Apache Struts with DOJO Example Part-4 :
Example of DOJO Toolkit with Struts and 
code explained.
Apache Struts 2 Radio Tag Example :
Example Steps of using Struts 2 Radio Tags
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