Friday, 10 July 2015

Struts 2 Fetching all records of a table

To fetch all the records, we have stored all the records in a collection (using List), and displaying the data of the collection using the iterator tag of struts2.
Here, we assume that you have a table in oracle database named user3333 that contains records. The table query is:
  1. CREATE TABLE  "USER3333"   
  2.    (    "ID" NUMBER,   
  3.     "NAME" VARCHAR2(4000),   
  4.     "PASSWORD" VARCHAR2(4000),   
  5.     "EMAIL" VARCHAR2(4000),   
  6.      CONSTRAINT "USER3333_PK" PRIMARY KEY ("ID") ENABLE  
  7.    )  

Example to fetch all the records of the table

In this example, we are creating 5 pages :
  1. index.jsp invoking action.
  2. Register.java for storing data of the table in the collection.
  3. User.java for representing table.
  4. struts.xml for defining the action and result.
  5. welcome.jsp for the view component to display records.

1) Create index.jsp for invoking action (optional)

This jsp page creates a link to invoke the action. But you can direct invoke the action class.
index.jsp
  1. <a href="viewrecords">View All Records</a>  

2) Create the action class

This action class contains ArrayList object as the datamember and execute method.
Register.java
  1. package com.javatpoint;  
  2. import java.sql.*;  
  3. import java.util.ArrayList;  
  4.   
  5. public class FetchRecords {  
  6. ArrayList<User> list=new ArrayList<User>();  
  7.   
  8. public ArrayList<User> getList() {  
  9.     return list;  
  10. }  
  11. public void setList(ArrayList<User> list) {  
  12.     this.list = list;  
  13. }  
  14. public String execute(){  
  15.  try{  
  16.   Class.forName("oracle.jdbc.driver.OracleDriver");  
  17.   Connection con=DriverManager.getConnection(  
  18.     "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
  19.               
  20.   PreparedStatement ps=con.prepareStatement("select * from user3333");  
  21.   ResultSet rs=ps.executeQuery();  
  22.   
  23.   while(rs.next()){  
  24.    User user=new User();  
  25.    user.setId(rs.getInt(1));  
  26.    user.setName(rs.getString(2));  
  27.    user.setPassword(rs.getString(3));  
  28.    user.setEmail(rs.getString(4));  
  29.    list.add(user);  
  30.   }  
  31.   
  32.   con.close();  
  33.  }catch(Exception e){e.printStackTrace();}  
  34.           
  35.  return "success";  
  36. }  
  37. }  

3) Create the class to represent table

This is the simple bean class containing 4 fields.
User.java
  1. package com.javatpoint;  
  2.   
  3. public class User {  
  4. private int id;  
  5. private String name,password,email;  
  6. //getters and setters  
  7. }  

4) Create struts.xml

This xml file defines action and result.
struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD   
  3. Struts Configuration 2.1//EN"   
  4. "http://struts.apache.org/dtds/struts-2.1.dtd">  
  5. <struts>  
  6.   
  7. <package name="anbc" extends="struts-default">  
  8. <action name="viewrecords" class="com.javatpoint.FetchRecords">  
  9. <result name="success">displayrecords.jsp</result>  
  10. </action>  
  11. </package>  
  12.   
  13. </struts>      

5) Create view component

It is the simple jsp file displaying the information of the user.
welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. <h3>All Records:</h3>  
  4. <s:iterator  value="list">  
  5. <fieldset>  
  6. <s:property value="id"/><br/>  
  7. <s:property value="name"/><br/>  
  8. <s:property value="password"/><br/>  
  9. <s:property value="email"/><br/>  
  10. </fieldset>  
  11. </s:iterator>