How to use Struts2 Iterator tag to get Users Details with Oracle 10g XE and Eclipse

In this Tutorial we are retrieving users details from oracle database table USERS which is in TEST schema. We are using Struts2 Iterator tag in jsp page to show all retrieved details from database table by clicking hyperlink on same jsp page and sending users names first letter to Action Java Class using url Rewriting.

Iterator will iterate over a value. An iterable value can be any of java.util.Collection, java.util.Iterator. 

The following example retrieves the value of the current object on the value stack and uses it to iterate over. The <s:property/> tag prints out the current value of the iterator.

Here we are using Apache Tomcat 7.0 and Java 6 (but you can use them as other versions too available on your system).

you can also Download full code with required Jars. 
Download Full Code with Jars 

Project View in Navigator :

Create a table with name USERS and here we are using TEST Schema(but you can use others too for example HR Schema)
users Table
users Table Data(showing some values)
In following example the iterator tag will retrieve value object from the ActionContext and the status attribute is used to create an IteratorStatus object, which in this example, its odd() method is used to alternate row colors:

This is page for user's input
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
 <%@ taglib uri="/struts-tags" prefix="s"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Search Users</title>  
 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>  
 <s:form id="adagstatus" name="adagstatus" theme="simple" method="POST">  
 <h3>Search Users with a Click on Alphabets</h3>  
                     <a href="finduser.action?id=a">A</a>  
                     <a href="finduser.action?id=B">B</a>  
                     <a href="finduser.action?id=s">S</a>  
      <table cellpadding="0" cellspacing="0" border="1" width="100%" >  
                <th>Column One(Users Name)</th>  
                <th>Column Two(Users ID)</th>  
      <s:iterator id="lstUsers" var="usr" value="lstUsers" status="counter">  
             <s:if test="#counter.odd == true">  
                <td align="center" style="background: grey;"><s:property /></td>  
                <td align="center" style="background: yellow;"><s:property /></td>  
This is a POJO class and it is working as action class for Struts.
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 import java.util.ArrayList;  
 import java.util.List;  
 import com.opensymphony.xwork2.ActionSupport;  
 public class FindUser extends ActionSupport {  
      List lstUsers = new ArrayList();  
      String id = "";  
      public String execute() {  
           if (id != null) {  
                lstUsers = finduser();  
           return SUCCESS;  
      public List finduser() {  
           List lst = new ArrayList();  
           Connection conn = null;  
           Statement stmt = null;  
           try {  
              conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","test","test");  
              stmt = conn.createStatement();  
              ResultSet rs = stmt.executeQuery("SELECT * FROM USERS WHERE LOWER(USERNAME) LIKE LOWER('"+ id +"%')");  
              while ( {  
           } catch (Exception e) {  
           return lst;  
      public String getId() {  
           return id;  
      public void setId(String id) {  
  = id;  
      public List getLstUsers() {  
           return lstUsers;  
      public void setLstUsers(List lstUsers) {  
           this.lstUsers = lstUsers;  

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">  

This file contains information about which action class to be invoked. 
 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "">  
      <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
      <constant name="struts.devMode" value="false" />  
      <constant name="struts.custom.i18n.resources" value="ApplicationResources" />  
      <constant name="struts.ui.theme" value="naveen" />  
      <constant name="struts.ui.templateDir" value="template" />  
      <package name="finduser" extends="struts-default">  
           <action name="finduser" class="">  
                <result name="success">jsp/homepage.jsp</result>  

Download Full Code with Jars

users data output

More Struts Topics :

No comments: