Wednesday, November 6, 2013

How to iterate user details through ArrayList with Iterator tag in Struts2 using Oracle 10g XE and Eclipse

In this tutorial we are working with iterator tag but the code is different (from previous post) to print user details. Here we are working with temporary list and adding values to this list. finally adding temporary list to another list and this list will contain all the rows( in the form of smaller lists) , actual data in the ArrayList we are retrieving through oracle database table.

Iterator will iterate over a value. An iterable value can be any of java.util.Collection, java.util.Iterator.
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. 

Project View in Project Explorer

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)

This is page for user's input as well as output.
 <%@ 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>  
 <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 style="background-color: orange;">Column One(Users Name)</th>  
                <th style="background-color: orange;">Column Two(Users ID)</th>  
      <s:iterator id="lstUsers" var="usr" value="lstUsers" status="counter">  
             <td align="center" style="background: grey;"><s:property value="#usr[0]" /></td>  
       <td align="center" style="background: yellow;"><s:property value="#usr[1]" /></td>  
                <td align="center"><a href="jsp/some_other_page.jsp">Go to some other page</a></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<String> lstTemp = null;  
           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 ( {  
                   lstTemp = new ArrayList();          
           } 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="2.5">  

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="nav" />  
      <constant name="struts.ui.templateDir" value="template" />  
      <package name="finduser" extends="struts-default">  
           <action name="finduser" class="">  
                <result name="success">jsp/homepage.jsp</result>  

user data output

No comments:

Popular Posts