Newsletter

Struts 2 Iterator Tag Example

Struts » on Oct 29, 2011 { 1 Comment } By Sivateja

Let us see how to use iterator tag in struts 2, actually iterator is the best tag to use rather writing while loop to print collection of objects in jsp’s.  if we use while loop we may need to write 4 5 lines but we can get the same result with iterator with 1 line hah.

Example

required files….

  • index.jsp
  • success.jsp
  • web.xml
  • struts.xml
  • LogingEx.java

Directory Structure

index.jsp

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=iteAction.action">

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

  <h1>Iterator_Values</h1>
  <s:iterator value="countries">

     <s:property /><br>

  </s:iterator>
  </body>
</html>

LogingEx.java

package java4s;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private List countries;

    public List getCountries()
    {
          return countries;
    }

    public String execute()
    {
        countries = new ArrayList();

        countries.add("Uganda");
        countries.add("Ukraine");
        countries.add("United States");
        countries.add("United Arab Emirates");
        countries.add("United Kingdom");
        countries.add("Uruguay");
        countries.add("Uzbekistan");

        return SUCCESS;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="iteAction" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

Output

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

One Response to “Struts 2 Iterator Tag Example”
  1. man says:

    Hi,

    Is it possible to use only struts 2 taglibs for my JSP without using action class and filter dispatcher.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.