I am trying to display the report using ArrayList in Struts. This is my Action File.
package com.travel.action;
import java.sql.ResultSet;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
*
* @author Mahesh
*/
public class SettlementConveyanceReportAction extends DispatchAction {
private static String SUCCESS = "success";
DataBaseConnector dbc = new DataBaseConnector();
java.util.ArrayList travelId = null;
java.util.ArrayList travelDetails = null;
java.sql.Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date().getTime());
String selectType = "";
String status = "";
String fromDate = "";
String toDate = "";
String query = "";
String fDate = "";
String tDate = "";
String condition = "";
int empId=0;
public ActionForward GenerateReport(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession(true);
if ("OK".equals(session.getAttribute("login"))) {
GregorianCalendar cal = new GregorianCalendar();
dbc.openConnection();
ReportBean rb = (ReportBean) form;
selectType = rb.gettravelType();
if (selectType.equals("TravelReport")) {
if (rb.getstatus().equals("Approved")) {
fromDate = rb.getfromDate();
toDate = rb.gettoDate();
if (fromDate.equals("")) {
int year, month, day;
year = cal.get(GregorianCalendar.YEAR);
month = cal.get(GregorianCalendar.MONTH) + 1;
day = cal.get(GregorianCalendar.DATE);
fromDate = day + "-" + month + "-" + year;
fDate = DateUtil.stringToSQLDateFormat(fromDate, "dd-MM-yyyy", "yyyy-MM-dd");
}
if (toDate.equals("")) {
int year, month, day;
year = cal.get(GregorianCalendar.YEAR);
month = cal.get(GregorianCalendar.MONTH) + 1;
day = cal.get(GregorianCalendar.DATE);
toDate = day + "-" + month + "-" + year;
tDate = DateUtil.stringToSQLDateFormat(toDate, "dd-MM-yyyy", "yyyy-MM-dd");
}
fDate = DateUtil.stringToSQLDateFormat(fromDate, "dd-MM-yyyy", "yyyy-MM-dd");
tDate = DateUtil.stringToSQLDateFormat(toDate, "dd-MM-yyyy", "yyyy-MM-dd");
query = "SELECT e.emp_id,e.first_name,d.name,l.pending_id,t.starting_date,t.return_date, t.noofdays,tm.tem_place,tm.tem_purpose,te.team_total_expense,"
+ " te.team_balance,t.advancerequested,e1.first_name,l.closed_date"
+ " FROM travel_logtable l,emp_master e,dept_master d,travel_master t,travel_expense_master tm,travel_expense_amount_master te,emp_master e1"
+ " WHERE l.pending_for=329"
+ " AND l.pending_job = 'TravelSettlement'"
+ " AND l.closed_date BETWEEN DATE('2018-02-01') AND DATE('2018-02-09')"
+ " AND l.status=0 AND l.forward_to IN ('admin','finance')"
+ " AND e.emp_id=l.created_by AND d.dept_no=e.dept_no AND t.travel_id=l.pending_id"
+ " AND te.team_travel_id=l.pending_id AND tm.tem_travel_id=t.travel_id "
+ " AND tm.tem_id=(SELECT tem_id FROM travel_expense_master WHERE `tem_travel_id`=tm.tem_travel_id LIMIT 1)"
+ " AND e.manager1=e1.emp_id ORDER BY l.forward_to,e.emp_id+0";
ResultSet rs = dbc.processSelect(query);
ReportBean rb1 = null;
travelDetails=new java.util.ArrayList();
while (rs.next()) {
String fName="", dName="",tId="",sDate="",eDate="",placeTraveled="",purpose="";
int NoOfDays=0;
double tExpense=0.0,balance=0.0;
rb1=(ReportBean)form;
empId=rs.getInt(1);
fName=rs.getString(2);
dName=rs.getString(3);
rb1.setempId(rs.getInt(1));
rb1.setempName(rs.getString(2));
rb1.setdeptName(rs.getString(3));
rb1.settravelId(rs.getInt(4));
rb1.setstartDate(rs.getString(5));
rb1.setreturnDate(rs.getString(6));
rb1.setnoOfDays(rs.getInt(7));
rb1.setplaceTraveled(rs.getString(8));
rb1.setpurpose(rs.getString(9));
rb1.settotalExpense(rs.getDouble(10));
rb1.setbalence(rs.getDouble(11));
rb1.setadvanceRequested(rs.getDouble(12));
rb1.setapprovedBy(rs.getString(13));
rb1.setapprovedDate(rs.getString(14));
travelDetails.add(rb1);
}
request.setAttribute("travelDetails", travelDetails);
mail.send("pradeep.y@csmsoftware.com", "pradeep.y@csmsoftware.com", "Date", fDate + " " + tDate);
}
if (rb.getstatus().equals("Pending")) {
}
}
if (selectType.equals("ConveyanceReport")) {
if (rb.getstatus().equals("Approved")) {
}
if (rb.getstatus().equals("Pending")) {
}
}
return mapping.findForward("form");
} else {
return mapping.findForward("login");
}
}
}
In the above code i am trying to fetch the records from the query to the Java Bean variable and at each iteration i am adding bean object to the ArrayList and i am setting the ArrayList attribute to get output to the JSP Page.
My bug is, at each iteration bean object is setting the value to the bean properties, but while adding bean object to the ArrayList it is taking Last iterated item to the ArrayList with that many number if iterations.
If While loop iterated four times it is printing Last iterated value 4 times. And this is my JSP Code
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Travel Report</title>
</head>
<body>
<div id="dvHeaderArea">
</div>
<div id="dvarea" style="height:550px;">
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Dept Name</th>
<th>Travel ID</th>
<th>From Date</th>
<th>To Date</th>
<th>No of Days</th>
<th>Place Traveled</th>
<th>Purpose</th>
<th>Total Expense</th>
<th>Balance</th>
<th>Advance Requested</th>
<th>RM Name</th>
<th>Approved Date</th>
</tr>
<logic:iterate id="travelDetails" scope="request" name="travelDetails">
<tr>
<td>
<bean:write property="empId" name="travelDetails"/>
</td>
<td><bean:write property="empName" name="travelDetails"/></td>
<td><bean:write property="deptName" name="travelDetails"/></td>
<td><bean:write property="travelId" name="travelDetails"/></td>
<td><bean:write property="startDate" name="travelDetails"/></td>
<td><bean:write property="returnDate" name="travelDetails"/></td>
<td><bean:write property="noOfDays" name="travelDetails"/></td>
<td><bean:write property="placeTraveled" name="travelDetails"/></td>
<td><bean:write property="purpose" name="travelDetails"/></td>
<td><bean:write property="totalExpense" name="travelDetails"/></td>
<td><bean:write property="balence" name="travelDetails"/></td>
<td><bean:write property="advanceRequested" name="travelDetails"/></td>
<td><bean:write property="approvedBy" name="travelDetails"/></td>
<td><bean:write property="approvedDate" name="travelDetails"/></td>
</tr>
</logic:iterate>
</table>
</div>
</body>
</html>
ArrayList attribute has been used to display the content of ArrayList in the JSP File, which i have set using request.setAttribute()