This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Sending collection to Action" Watch "Sending collection to Action" New topic
Author

Sending collection to Action

Sreek Gupta
Greenhorn

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" %>

<html>
<head><title>Example</title></head>
<body>

<h3>Example Page</h3>

<html:errors/>

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

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

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

<html:form action="dept.do">
<table border=1>
<tr>
<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>
</tr>

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

</body>
</html>



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.

Regards,
Sreek
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" />
</logic:iterate>

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:

http://struts.apache.org/1.2.x/faqs/indexedprops.html

http://wiki.apache.org/struts/StrutsCatalogLazyList
[ July 24, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending collection to Action
 
Similar Threads
which jar file need to strug.taglib
doesnt display struts tags in normal html form
struts tag problem ?
doubt in Struts application
Pop-up in struts