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:
- CREATE TABLE "USER3333"
- ( "ID" NUMBER,
- "NAME" VARCHAR2(4000),
- "PASSWORD" VARCHAR2(4000),
- "EMAIL" VARCHAR2(4000),
- CONSTRAINT "USER3333_PK" PRIMARY KEY ("ID") ENABLE
- )
Example to fetch all the records of the table
In this example, we are creating 5 pages :
- index.jsp invoking action.
- Register.java for storing data of the table in the collection.
- User.java for representing table.
- struts.xml for defining the action and result.
- 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
- <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
- package com.javatpoint;
- import java.sql.*;
- import java.util.ArrayList;
-
- public class FetchRecords {
- ArrayList<User> list=new ArrayList<User>();
-
- public ArrayList<User> getList() {
- return list;
- }
- public void setList(ArrayList<User> list) {
- this.list = list;
- }
- public String execute(){
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection con=DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
-
- PreparedStatement ps=con.prepareStatement("select * from user3333");
- ResultSet rs=ps.executeQuery();
-
- while(rs.next()){
- User user=new User();
- user.setId(rs.getInt(1));
- user.setName(rs.getString(2));
- user.setPassword(rs.getString(3));
- user.setEmail(rs.getString(4));
- list.add(user);
- }
-
- con.close();
- }catch(Exception e){e.printStackTrace();}
-
- return "success";
- }
- }
3) Create the class to represent table
This is the simple bean class containing 4 fields.
User.java
- package com.javatpoint;
-
- public class User {
- private int id;
- private String name,password,email;
-
- }
4) Create struts.xml
This xml file defines action and result.
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-
- Struts Configuration 2.1
- "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
-
- <package name="anbc" extends="struts-default">
- <action name="viewrecords" class="com.javatpoint.FetchRecords">
- <result name="success">displayrecords.jsp</result>
- </action>
- </package>
-
- </struts>
5) Create view component
It is the simple jsp file displaying the information of the user.
welcome.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- <h3>All Records:</h3>
- <s:iterator value="list">
- <fieldset>
- <s:property value="id"/><br/>
- <s:property value="name"/><br/>
- <s:property value="password"/><br/>
- <s:property value="email"/><br/>
- </fieldset>
- </s:iterator>