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)
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:
homepage.jsp
This is page for user's input
FindUser.java
This is a POJO class and it is working as action class for Struts.
web.xml
struts.xml
This file contains information about which action class to be invoked.
Download Full Code with Jars
Output:
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) |
homepage.jsp
This is page for user's input
<%@ 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>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
</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>Column One(Users Name)</th>
<th>Column Two(Users ID)</th>
</tr>
</thead>
<s:iterator id="lstUsers" var="usr" value="lstUsers" status="counter">
<s:if test="#counter.odd == true">
<tr>
<td align="center" style="background: grey;"><s:property /></td>
</s:if>
<s:else>
<td align="center" style="background: yellow;"><s:property /></td>
</tr>
</s:else>
</s:iterator>
</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 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()) {
lst.add(rs.getString(1));
lst.add(rs.getString(2));
}
} 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>finduser</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="naveen" />
<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>
Download Full Code with Jars
Output:
users data output |
More Struts Topics :
- How to Set Struts2 Environment for Eclipse with Oracle 10g Express Edition
- Simple struts2 program with Eclipse & Apache Tomcat Server
- Login Using Struts2 and Eclipse
- Database Access using Struts2,Eclipse and Oracle
- Basic form Validation using Struts2 Framework with validate() method and ActionSupport class
- Validation using Struts2 Framework and XML file on Radio button and DropDownList with Eclipse
- Validation using Struts2 Framework and XML file with Eclipse Indigo
- AJAX validation using Struts2 Framework and XML file with Eclipse Indigo
- Session Tracking using SessionAware Interface with Struts2, Oracle Database and Eclipse IDE
- Action Wildcards with Struts2
- How to Configure methods in Action mappings with or without Wildcards using Struts2
- Store and Retrieve Numeric, Alphanumeric and Blob (Image) data with Struts2, Oracle 10g XE and Eclipse Indigo as IDE
- How to use Struts2 Iterator tag to get Users Details with Oracle 10g XE and Eclipse
- How to iterate user details through ArrayList with Iterator tag in Struts2 using Oracle 10g XE and Eclipse
- How to upload Multiple Files- Images (blob data) with enctype="multipart/form-data" in Oracle Database using Struts2, JDBC and Eclipse IDE
No comments:
Post a Comment