Fetch Values from Database into ArrayList then Display on JSP page in Struts 2
This example shows how to fetch values from the database into ArrayList then displaying these values on the JSP page in Struts2 iterator tag.
We have a “college” database with “student” table contains two records that we want to display on the JSP page using Struts2 framework.
For this we will first fetch the data from the database using StudentDAO
class which uses the StudentBean
to encapsulate each database row as an object. And this same StudentDAO
class will insert each StudentBean
object into an ArryList of StudentBean
type and return this ArrayList
to StudentsAction
class. Lastly, the fetched ArrayList
is displayed on the JSP page using Struts2 tag.
Let’s see how the code looks like
index.jsp – First page contains the action to get the student details
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Fetch values from database into ArrayList then display on JSP page in Struts2</title> </head> <body> <h1>Fetch values from database into ArrayList then display on JSP page in Struts2</h1> <s:form action="viewStudents"> <s:submit value="Click to view students details"/> </s:form> </body> </html>
struts.xml – This is the mapping file to map the form action we defined on the index.jsp page to the Java class
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> <action name="viewStudents" class="com.p3lang.struts2.action.StudentsAction"> <result name="success">web/success.jsp</result> <result name="error">web/error.jsp</result> </action> </package> </struts>
StudentsAction.java – Struts2 actions class that will execute the required action. We are not doing data access coding in this class instead we call the StudentDAO to do that for us and return the appropriate response.
package com.p3lang.struts2.action; import java.sql.SQLException; import java.util.List; import org.apache.struts2.dispatcher.DefaultActionSupport; import com.p3lang.struts2.Beans.StudentBean; import com.p3lang.struts2.DAO.StudentDAO; /** * * @author Abhishek */ @SuppressWarnings("serial") public class StudentsAction extends DefaultActionSupport { private StudentDAO sDAO = null; private List<StudentBean> studentsList = null; public StudentDAO getsDAO() { return sDAO; } public void setsDAO(StudentDAO sDAO) { this.sDAO = sDAO; } public List<StudentBean> getStudentsList() { return studentsList; } public void setStudentsList(List<StudentBean> studentsList) { this.studentsList = studentsList; } @Override public String execute() throws ClassNotFoundException, SQLException { try { sDAO = new StudentDAO(); studentsList = sDAO.getStudentsDAO(); System.out.println(studentsList.toString()); } catch (Exception e) { e.printStackTrace(); return "error"; } return "success"; } }
StudentDAO.java – This class fetch the data from database and store the fetched data in ArrayList. This DAO class uses two other classes DBConnection (to make the connection to the MySQL DB) and StudentBean (to store the values fetched from DB).
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.p3lang.struts2.DAO; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.p3lang.struts2.Beans.StudentBean; import com.p3lang.struts2.database.DBConnection; /** * * @author Abhishek */ public class StudentDAO { private StudentBean sb = null; private List<StudentBean> al = null; public StudentBean getSb() { return sb; } public void setSb(StudentBean sb) { this.sb = sb; } public List<StudentBean> getAl() { return al; } public void setAl(List<StudentBean> al) { this.al = al; } public List<StudentBean> getStudentsDAO() { Connection connection = null; try { connection = DBConnection.getConnection(); PreparedStatement ps = connection.prepareStatement("select * from students"); ResultSet rs = ps.executeQuery(); al = new ArrayList<StudentBean>(); // Initialize ArrayList to store the students details while (rs.next()) { sb = new StudentBean(); // Initialize the bean class to hold the values sb.setStudentName(rs.getString(1)); sb.setStudentEmail(rs.getString(2)); sb.setStudentProgram(rs.getString(3)); sb.setStudentGrade(rs.getString(4)); al.add(sb); } connection.close(); } catch (Exception e) { e.printStackTrace(); return null; } return al; } }
StudentBean.java – Bean class to hold the values we fetch in the StudentDAO.java class
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.p3lang.struts2.Beans; /** * * @author Abhishek */ public class StudentBean { private String studentName; private String studentEmail; private String studentProgram; private String studentGrade; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentEmail() { return studentEmail; } public void setStudentEmail(String studentEmail) { this.studentEmail = studentEmail; } public String getStudentProgram() { return studentProgram; } public void setStudentProgram(String studentProgram) { this.studentProgram = studentProgram; } public String getStudentGrade() { return studentGrade; } public void setStudentGrade(String studentGrade) { this.studentGrade = studentGrade; } }
success.jsp – The JSP page with struts2 tags to display the ArrayList
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="t" uri="/struts-tags"%> <!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>Student Details</title> </head> <body> <h1>Student Details</h1> <table border="1" bordercolor="gray"> <tr bgcolor="light-gray" style="color:white;"> <td>Student Name</td> <td>Student Email</td> <td>Student Program</td> <td>Student Grade</td> </tr> <t:iterator value="studentsList" status="studentListStatus"> <tr> <td><t:property value="%{studentName}" /></td> <td><t:property value="%{studentEmail}" /></td> <td><t:property value="%{studentProgram}" /></td> <td><t:property value="%{studentGrade}" /></td> </tr> </t:iterator> </table> </body> </html>
Output
Download the project from GitHub