| |
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 : ISHTEK Date (Year/Month/Date): 2009-03-20
Struts2-Tiles-Example code explained | |
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 Second learning example using Apache Struts2 and Tiles Frameworks :
References:
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
To continue with my earlier example of using Struts2:
http://www.interview-questions-tips-forum.net/index.php/Apache-Struts/Struts2-example-tutorial/My-First-example-using-Apache-Struts2
I have tried to use Tiles Framework and Struts2 Plugin for Tiles
integration to Struts2.
There are some of the more number of JAR files required for
this example to run.
Software environment used for this example :
1. JDK 5.0
2. Tomcat 5.5.x
3. Eclipse Genymede
4. Apache Struts 2.1.6 (example Web application should have
following JAR files in WEB-INF\lib folder)
(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\tiles.xml
3. MyExample\WEB-INF\web.xml
4. MyExample\WEB-INF\classes\sample\MyExample.java
5. MyExample\good.jsp
6. MyExample\bad.jsp
7. MyExample\visitor.jsp
8. 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>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
</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="tiles-default">
<action name="myexample" class="sample.MyExample">
<result name="good" type="tiles">good-sample</result>
<result name="bad">/bad.jsp</result>
<result name="visitor">/visitor.jsp</result>
</action>
</package>
</struts>
Corresponding tiles.xml file would look like as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="good-sample" template="/good.jsp">
<put-attribute name="message" value="Good JSP"/>
</definition>
</tiles-definitions>
You might have noticed that the tiles definition name "good-sample"
is referred in the result forward in struts.xml file. This way
tiles attribute name "message" will be visible in the good.jsp
file.
This good.jsp file will be something like as follows:
with the tiles related tag library included with a prefix and
this prefix is used to retrieve the required message "Good JSP"
in the JSP file.
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t1" %>
<font color="green"><b><t1:getAsString name="message"/></b></font>
There is no change in Action class used in this example, and is same as
in the earlier example, shown as follows:
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, solution is,
by using ParameterAware interface to get all the request parameters
packed in Map, if you are using JSP file with HTML tags
If you are using Struts2 Tags from Tag Library, then just using
tag library and making the key same as the field defined in Action POJO,
and Action POJO can be just plain POJO.
By defining instance variable in Action POJO, there won't be any issue
from con currency point of view, as in Struts2 for every request
a new Action class is initialized and used, not like in Struts 1.x version
where there is one instance of Action class used for multiple request
concurrently, thus posing risk if instance variable is used in Action class.
Please don't ask questions 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).
|
|
|
Commented By -> Tanaka | There is no result type defined for type 'tiles' mapped with name 'good'. Did you mean 'tiles'? |
| | |
Commented By -> ISHTEK | Hi Tanaka,
Can you please explain your question a bit more clearly??
If I understood your question properly, in struts.xml file,
you can very well see the result type as "good" pointing to
a string value "good-sample". "good-sample" is refering to good.jsp
page in tiles.xml file.
Hope this answers your question.. |
| | Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts2 Tiles Example >>> with Code and article |
|
|
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.
|  |
|
|
|
|
|