Advertisement
Home > Struts Struts 2 Example Tutorial > Struts2 > Tags such as IF Iterator TagsPlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Struts Struts 2 Example Tutorial > Struts2 > Tags such as IF Iterator Tags
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 : Amit
Title :
Struts2 example tutorial
Description : My First example using Apache Struts2
More...


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...

Struts 2 Tutorial, Struts 2 Example
Author : ISHTEK
Date (Year/Month/Date): 2009-02-19
Struts2 Tag Reference - http://struts.apache.org/2.0.14/docs/tag-reference.html

All the Struts2 Tags , those can be found in the above link are under my scanner
for providing appropriate sample/example on how to use these Struts2 Tags:

-> IF Tag : (http://struts.apache.org/2.0.14/docs/if.html)
-> ITERATOR Tag: http://struts.apache.org/2.0.14/docs/iterator.html
As in Struts2 , for each request a new action class instance is created and
associated, it is not an issue from Thread-Safe perspective to use an instance
variable in Action class POJO.

So we shall be using sample  POJO action class with variables defined at instance
level and use these variables to test various Struts2 tags, such as IF Tag,
Iterator Tag and many more.

I named this action class as IfExample.java, as this is having code for both
if and iterator tags implementations.
-----------------------------------------------------------------------------------
package sample;
import java.util.List;
import java.util.ArrayList;

public class IfExample {
//for if tag test

    private boolean status = false;

    public boolean getStatus() {
        return status;
    }
    public void setStatus(boolean st) {
        this.status = st;
    }


    //for iterator tag test
    private Sample sample;

    public Sample getSample() {
        return sample;
    }
    public void setSample(Sample sample) {
        this.sample = sample;
    }
    public String execute() throws Exception {
        Sample sample1 = new Sample();
        List names = new ArrayList();
        names.add("test");
        names.add("test1");
        names.add("test2");
        names.add("test3");
        sample1.setNames(names);
        setSample(sample1);
        return "okay";
    }

}
-----------------------------------------------------------------------------------

The struts.xml file as following configurations for these two Tags:

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="iftest" namespace="/iftest" extends="struts-default">
       <action name="ifexample" class="sample.IfExample">
           <result name="okay">/if-test.jsp</result>
       </action>
    </package>
    <package name="iteratortest" namespace="/iteratortest"
                                    extends="struts-default">
       <action name="iteratorexample" class="sample.IfExample">
           <result name="okay">/iterator-test.jsp</result>
       </action>
    </package>
</struts>
-----------------------------------------------------------------------------------

Corresponding JSP files having test code for IF and ITERATOR tags are as follows:

if-test.jsp
-----------------------------------------------------------------------------------
<%@ taglib prefix="st2" uri="/struts-tags" %>
<st2:if test="status">
This line is within if tag
</st2:if>
<st2:else>
This line is within else tag
</st2:else>
-----------------------------------------------------------------------------------
As you can see status is the boolean variable in IfExample action class,
and depending on the value of this field, appropriate message gets printed out
on browser, in this case as status value is 'false', so the message gets printed
on is:
This line is within else tag

and for iterator tag specific JSP file iterator-test.jsp
-----------------------------------------------------------------------------------
<%@ taglib prefix="st2" uri="/struts-tags" %>
<st2:iterator value="sample.names" var="n">
<st2:text name="%{n}"/><br>
</st2:iterator>
-----------------------------------------------------------------------------------

sample is the variable name in IfExample of type Sample, and names is a variable in
Sample class and of type List.
So this JSP file prints values as stored in execute method of IfExample Action class
as follows:

test
test1
test2
test3


Advertisement


Commented By ->
Prashanth
Hi,

Very nicely explained.. Thanks

Can you provide any example on how to use Tiles with Struts2 ?


Commented By ->
Amit
Hi Prashanth,

You can read an article in this site at
http://www.interview-questions-tips-forum.net/index.php/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-Example/with-Code-and-article

and Tiles with Struts2 and I18N can befound at

http://www.interview-questions-tips-forum.net/index.php/Struts-Struts-2-Example-Tutorial/Struts2-Tiles-I18N-Example/with-code-explained-article
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
9,18
Enter bigger number from above :*
Home > Struts Struts 2 Example Tutorial > Struts2 > Tags such as IF Iterator Tags
Visitor/User submitted related resources:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
31,29
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
This List is generated as on 2009-07-12 (YYYY-MM-DD)
#Discuss-these : questions-and-answer : Interview-Questions-on-Java #Some-facts-about-MVC-Framework-and-Struts : MVC-Struts : Struts-Struts-2-Example-Tutorial #My-First-example-using-Apache-Struts2 : Struts2-example-tutorial : Struts-Struts-2-Example-Tutorial This article discusses steps of creating a Struts2 based example web application With lots of easy to understand explaination and source code for reference. #Tags-such-as-IF-Iterator-Tags : Struts2 : Struts-Struts-2-Example-Tutorial #Step-by-step-demonstration : Struts-Eclipse-MVC : Struts-Struts-2-Example-Tutorial #Code-with-Sample-Action : Struts-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #open-for-discussion : Struts2-questions : Struts-Struts-2-Example-Tutorial #with-Code-and-article : Struts2-Tiles-Example : Struts-Struts-2-Example-Tutorial #with-code-explained-article : Struts2-Tiles-I18N-Example : Struts-Struts-2-Example-Tutorial #Tutorial : Struts2-Tag-Library-Example : Struts-Struts-2-Example-Tutorial #Code-and-Tutorial : Struts2-Tags-Example : Struts-Struts-2-Example-Tutorial #Step-By-Step-Code : Radio-Tag-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Code : Struts2-Validation-Example : Struts-Struts-2-Example-Tutorial #Struts-2-Example-with-Code : Struts2-HibernateSession-Plugin : Struts-Struts-2-Example-Tutorial
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER

This web site provides some of the information about various technologies, example 
code, tips, tutorials etc. Like any printed matterials, 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.