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)

homepage.jsp
This is page for user's input as well as output.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <%@ taglib uri="/struts-tags" prefix="s"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Search Users</title>  
 </head>  
 <body>  
 <s:form id="adagstatus" name="adagstatus" theme="simple" method="POST">  
 <h3>Search Users with a Click on Alphabets</h3>  
      <table>  
           <tr>  
                <td>  
                     <a href="finduser.action?id=a">A</a>  
                     <a href="finduser.action?id=B">B</a>  
                     <a href="finduser.action?id=s">S</a>  
                </td>  
           </tr>  
      </table>  
      <br>  
      <table cellpadding="0" cellspacing="0" border="1" width="100%" >  
      <thead>  
           <tr>  
                <th style="background-color: orange;">Column One(Users Name)</th>  
                <th style="background-color: orange;">Column Two(Users ID)</th>  
           </tr>  
      </thead>  
      <s:iterator id="lstUsers" var="usr" value="lstUsers" status="counter">  
           <tr>  
             <td align="center" style="background: grey;"><s:property value="#usr[0]" /></td>  
       <td align="center" style="background: yellow;"><s:property value="#usr[1]" /></td>  
           </tr>  
      </s:iterator>  
 </table>  
 <br>  
      <table>  
           <tr>  
                <td align="center"><a href="jsp/some_other_page.jsp">Go to some other page</a></td>  
           </tr>  
      </table>  
 </s:form>  
 </body>  
 </html>  

FindUser.java
This is a POJO class and it is working as action class for Struts.
 package blog.webideaworld.in;  
 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 {  
              Class.forName("oracle.jdbc.OracleDriver");  
              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 (rs.next()) {  
                   lstTemp = new ArrayList();          
                   lstTemp.add(rs.getString(1));  
                   lstTemp.add(rs.getString(2));  
                   lst.add(lstTemp); 
              }  
           } catch (Exception e) {  
                System.out.println(e);  
           }  
           return lst;  
      }  
      public String getId() {  
           return id;  
      }  
      public void setId(String id) {  
           this.id = id;  
      }  
      public List getLstUsers() {  
           return lstUsers;  
      }  
      public void setLstUsers(List lstUsers) {  
           this.lstUsers = lstUsers;  
      }  
 }  

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">  
  <display-name>findUserIterate</display-name>  
  <welcome-file-list>  
   <welcome-file>jsp/homepage.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
   <filter-name>struts2</filter-name>  
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
   <filter-name>struts2</filter-name>  
   <url-pattern>/*</url-pattern>  
  </filter-mapping>  
 </web-app>  

struts.xml
This file contains information about which action class to be invoked.
 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">  
 <struts>  
      <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="blog.webideaworld.in.FindUser">  
                <result name="success">jsp/homepage.jsp</result>  
           </action>  
      </package>  
 </struts>  


output:
user data output

No comments:

Popular Posts