File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Sending collection to Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Sending collection to Action" Watch "Sending collection to Action" New topic

Sending collection to Action

Sreek Gupta

Joined: Mar 08, 2006
Posts: 24
Hi ,
I have a jsp as follows:

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>


<h3>Example Page</h3>


<html:form action="">
Dept No: <html:select property="deptNumber">
<html ption value="100">100</html ption>
<html ption value="200">200</html ption>
<html:submit property="whichButton"> <bean:message key="button.fetch"/></html:submit>

<logic resent name="empDetailTable" scope="session">

<% java.util.ArrayList lst = (java.util.ArrayList) session.getAttribute("empDetailTable");

<html:form action="">
<table border=1>
<td class="txtBld headerClr" width="12%" height="26">First Name</td>
<td class="txtBld headerClr" width="12%" height="26">Middle Name</td>
<td class="txtBld headerClr" width="12%" height="26">Last Name</td>
<td class="txtBld headerClr" width="11%" height="26">Induction Id</td>
<td class="txtBld headerClr" width="9%" height="26">Gender</td>
<td class="txtBld headerClr" width="8%" height="26">Attendance</td>

<logic:iterate id="emp" name="empDetailTable">
<td><html:text property="firstName" value="<%=((employee.DeptFetchBean)emp).getFirstName()%>"/></td>
<td><html:text property="lastName" value="<%=((employee.DeptFetchBean)emp).getLastName()%>"/></td>
<html:submit property="whichButton"> <bean:message key=""/></html:submit>
</logic resent>


When i click on Fetch button, I need to get all the employess belonging to a particular department from database, And i have show them up in a table in the same page. This part i have done.
Once i get the employees i should be able to edit any employee details and click on Save button should save these details in the database again.
Since each department will have many employees, when i click on Save button how can i propagate all the updated entries further (to my action servlet)?
It would be helpful if you can provide me some example code for this scenario.

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The firs thing you'd need to do is create an ActionForm that looks like this:

You would then use indexed properties for your <html:text> tags within the iterate loop. If you do this, Struts will populate the form bean for each item in the list if changes are made.

Your JSP code would look something like this:

<logic:iterate id="employee" name="myActionForm" property="employees>
<html:text name="employee" property="firstName" indexed="true" />
<html:text name="employee" property="lastName" indexed="true" />

Note: you do not have to code a value attribute. Struts automatically gets and displays the value.

For more information on how to used indexed properties, see the following links:
[ July 24, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: Sending collection to Action
It's not a secret anymore!