| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : ISHTEK
Title :
Struts2
Description :
Tags such as IF Iterator Tags More...
|
Written By : guddu
Title :
Struts Eclipse MVC
Description :
Step by step demonstration More...
|
Written By : Girish
Title :
Struts Tiles I18N Example
Description :
Code with Sample Action More...
|
Written By : ISHTEK
Title :
Struts2 questions
Description :
open for discussion More...
|
Written By : ISHTEK
Title :
Struts2 Tiles Example
Description :
with Code and article More...
|
Written By : ISHTEK
Title :
Struts2 Tiles I18N Example
Description :
with code explained article More...
|
Written By : Amit
Title :
Struts2 Tag Library Example
Description :
Tutorial More...
|
| Tags/Keywords : Struts, Struts2 Tutorial, Struts 2 Example, Struts2 Author : Amit Date (Year/Month/Date): 2009-02-16
My First example using Apache Struts2 : | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
My First learning example using Apache Struts2 :
Struts2 Tag Library Example
/Struts-Struts-2-Example-Tutorial/Struts2-example-tutorial/My-First-example-using-Apache-Struts2
/Struts-Struts-2-Example-Tutorial/Struts-Tiles-I18N-Example/Code-with-Sample-Action
/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-I18N-Example/with-code-explained-article
/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-Example/with-Code-and-article
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
to 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 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 are like forwards in earlier versions of
Struts (like 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 professional who have already used Struts 1.x version
will have many questions (I had too) in mind after looking at this
Action class, neither 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, one of the solution is,
by using ParameterAware interface to get all the request parameters
packed in Map, but there is another solution, 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 comming soon on some other example in this site.
Please don't ask question on implementation of execute method, 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, :)
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
|
| | Replied By -> guddu | Can you provide some of the example on how to use basic Struts2 Tags like
IF, ELSEIF, ITERATOR, LABEL etc.?
|
| | | | Replied By -> Girish | Is there any other way available other than using ParameterAware for passing data to JSP/UI??
I feel Action class to be defined as POJO only without extending
ParameterAware.
Any ideas, how can this be done?? |
| | | | Replied By -> Amit | Yes Girish,
It is very much possible to retrieve HTTP request parameter
from JSP/UI, not by using ParameterAware , but by getting
HttpServletRequest object from org.apache.struts2.ServletActionContext.getRequest() method will return HTTP
request Object.
From this request object one can very well retrieve all request parameters from UI, and
one can set request attribute to this HTTP request object, to
be able to use in JSP/UI.
I hope this answers your query. One more thing I would like to add is that
you can user ApplicationAware, CookieAware, PrincipalAware, RequestAware, SessionAware,
ServletRequestAware, ServletResponseAware interfaces from
org.apache.struts2.interceptor apart from ParameterAware (as shown in this example).
As these ...Aware are interfaces, one can use all these for a single Action POJO if required.
|
| | |
Commented By -> Girish | Hi,
I have a doubt/query that I would like to ask here:
If the Action class is a POJO (Plain Old Java Object) in Struts 2, then Can I be able to use any domain
class as Action, I mean if I
am using Hibernate as Persistence Mapping, then Can I be able to use Hibernate domain class directly as
Action class
and thus be able to remove Action layer all together???
Suppose there is a web application which is not having much business
logic, but only data flowing from Screen to database and back from Database
to screen, then Is it possible/advisable to use domain class as
Action class, thus combining Action and Domain class together??
Just a out of my mind thinking, anyone please comment on this.
Thanks,
Girish |
| | |
Commented By -> Amit | Hi Girish,
I am not sure if i understood your question properly,
but I would like to evaluate Hibernate Session Plugin
so far as using or injecting Hibernate Session to Struts2 Action class
is concerned.
Hibernate Session Plugin
Hibernate Session
Plugin |
| | |
Commented By -> Amit | It is easy to understand if you are using Struts 2 Tags, then automatically
values from UI or JSP will be available in Action class instance
variables on sumit of form, provided both the variable name in Action class and the
key used in Struts 2 TAG are same.
But if any Java client is making a call over HTTP to web application
with Struts2 as Framework for UI tier, then by using this way one can
receive HTTP Servlet Request parameters by making Action class
ParameterAware.
Thanks for this way of solution to very common task of passing
parameter values from UI to Action class. |
| | |
Commented By -> Good123 | If validation has to be implemented in this very example,
and How can one handle validation errors, if any example code available??
Thanks |
| | |
| | |
Commented By -> Nitin | Can you please suggest me a book(easy to understand) to start with struts 2 ? |
| | |
Commented By -> Amit | Hi Nitin,
I guess, "Struts 2 in Action" could be the one for someone
who really wants to start leanring Struts 2 with some sample
source code, and example chapters available at
http://www.manning.com/dbrown/
Hope this helps.
Please keep reading my articles in this site and please
share your thought, ideas on Struts 2, on this Page.
In my free time I keep updating my articles and try to
answer as many questions I can on Struts 2.
|
| | |
Commented By -> good123 | Can you write step by step example using Struts2 and client/browser
side Validation?
For example using JavaScript with Validator in Struts2 Framework.
While using Struts1 and validator framework, I used to define Javascript
code in validation-rule.xml file, and use this along with the browser-
side validation.
But using Struts2 also, is this the way one can define Javascript
or any other way of achieving this.
Thanks. |
| | |
Commented By -> Amit | You can find an nice discussion thread here
http://www.coderanch.com/t/420655/Struts/Struts-Validation
Can you please try defining JavaScript function in the validation
XML descriptor file for this example and let us know any
difficulties/issues if you are facing on this?
|
| | |
Commented By -> saleem | ya its good for beginners and y u peoples
confusing with good bad and visitor and all
simply specify SUCCESS r ERROR. if suc move to suc.jsp or err.jsp. and whats the purpose of
temp here. U can directly write those things
which happens in real world. k good
|
| | |
Commented By -> Amit | Thanks Saleem for your insightful thought on
the topic.
Intention of this writeup is to explain how
a very simple example works...
|
| | |
Commented By -> rupesh paunikar | i have executed one program based on execAndWait interceptor,
ok that is working fine and i got wait.jsp(i.e. wait page,
i have been created). But my problem is that after completion
of execution of action class's execute() method, i want to
display success.jsp page. i have written all the code fine
but i didn't get that success.jsp.
Here, i got that success.jsp page only after refreshing
the browser.But i want that page automatically to be displayed
after wait.jsp page displayed.
this is the code i have written in strut.xml file.
<action name="LoginCheck" class="p1.MyClass">
<interceptor-ref name="completeStack"/>
<interceptor-ref name="execAndWait">
<param name="delay">100</param>
<param name="delaySleepTimer>50</param>
</interceptor-ref>
<result name="wait">/wait.jsp</result>
<result name="success">/success.jsp</result>
<result name="input">/LoginPage.jsp</result>
</action>
And in MyClass.java, i have write following code in execute()
method.
public String execute()
{
for(long i=1;i<=30000;i++)
{
System.out.println(i);
}
return "success";
}
So what is wrong in this why i didnt get success.jsp page
after completion of execution of execute method automatically.
Please send me answer, i will be very thankful to u.
Thanking you. |
| | |
Commented By -> Amit | I think, you might have to just put the appropriate
META tag in the wait.jsp file that you have created,
in order to refresh the page at a regular within limit
time interval, something like as follows:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="refresh"
content="5;url=<s:url includeParams="all"/>"/>
</head>
<body>
----
----
</body>
</html>
For more details please refer the api documentation of Struts2
"ExecuteAndWaitInterceptor"
Hope this help.
Thanks for sharing your interesting question. |
| | Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts2 example tutorial >>> My First example using Apache Struts2 |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|