File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Help! Form to update multiple records upon save Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help! Form to update multiple records upon save" Watch "Help! Form to update multiple records upon save" New topic
Author

Help! Form to update multiple records upon save

Greg C Robinson
Greenhorn

Joined: Jan 30, 2009
Posts: 4
Hello! Hoping someone can help:

I have a JSP containing a checklist -type form where I'm trying to present a list of employees and two checkboxes beside each name, where the checkboxes correspond to employee.check1 (boolean) and employee.check2 (boolean). I want to be able to scroll down the list of employees and check off box 1 and/or box 2 for each employee, hit Save (once) at the bottom and have it update each employee's record and set check1 and check2 accordingly. I have a viewChecklist.jsp and an editChecklist.jsp. Here is the display only list on viewChecklist.jsp:

<table border="0" width="400">
<tr>
<td><b>Last, First</b></td>
<td><b>ID</b></td>
<td><b>-1-</b></td>
<td><b>-2-</b></td>
</tr>
<c:forEach items="${employeesForChecklist}" var="employee">
<%-- nitrox:varType="com.xyz.forms.EmployeeForm" --%>
<tr>
<td><c:out value="${employee.lastName}"/>, <c:out value="${employee.firstName}"/></td>
<td><c:out value="${employee.idNumber}"/></td>
<td><html:checkbox disabled="true" name="employee" property="check1"/></td>
<td><html:checkbox disabled="true" name="employee" property="check2"/></td>
</tr>
</c:forEach>
</table>

On the editChecklist.jsp, I want something similar but with the checkboxes enabled. assuming I may need to switch from forEach (which iterates my session variable employeesForChecklist) to a logic:iterate ? The app uses Nitrox but not sure I need to use that here. I'm rusty with this stuff, creating a row index and how to build the array of employee IDs that need updating is just eluding me. Tips? Suggestions? Code samples? thanks in advance!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In order to do this, you will need to use "indexed properties". Please refer to question 6 of our FAQ for more information on indexed properties along with a working example.


Merrill
Consultant, Sima Solutions
Greg C Robinson
Greenhorn

Joined: Jan 30, 2009
Posts: 4
thanks!!
Greg C Robinson
Greenhorn

Joined: Jan 30, 2009
Posts: 4
OK, I'm real close here. Using that FAQ link - which was VERY helpful, thanks! - I was able to get my checklist utility working. However, I have one minor quirk. The first time I ran it, all checkboxes were empty/false. Which was expected. I then went down and selectively checked a few records and they all set properly. Nice. However, UNchecking those checkboxes does not set the boolean back to false. When I step through the debugger, I see the iteration's row is showing TRUE for those checkboxes. My thought was that it was seeing null and I might need to explicity check for null and if so, set to false - but I cannot see a null coming in. All I see is true. My code is very closesly modeled after what's found in that FAQ page, so I won't paste it all here. Any thoughts on what I might need to add?

Here is my iteration on the JSP:

<logic:iterate id="employeeForm" name="checklistItemForm" property="employeeList">
<html:hidden name="employeeForm" property="employeeID" indexed="true" />
<tr>
<td><bean:write name="employeeForm" property="lastName" />,
<bean:write name="employeeForm" property="firstName" /></td>
<td><bean:write name="employeeForm" property="crdNumber" /></td>
<td><html:checkbox name="employeeForm" property="check1" indexed="true" /></td>
<td><html:checkbox name="employeeForm" property="check2" indexed="true" /></td>
<td><html:checkbox name="employeeForm" property="check3" indexed="true" /></td>
</tr>
</logic:iterate>

here is checklistItemForm.getEmployeeForm:

public EmployeeForm getEmployeeForm(int index)
{
// make sure that orderList is not null
if(this.employeeList == null)
{
this.employeeList = new ArrayList();
}

// indexes do not come in order, populate empty spots
while(index >= this.employeeList.size())
{
this.employeeList.add(new EmployeeForm());
}

// return the requested item
return (EmployeeForm) employeeList.get(index);
}

Here is the relevant portion of the action called upon save/submit:

List employeeList = checklistForm.getEmployeeList();

try {

IEmployeeManager employeeManager = (IEmployeeManager)getServiceLocator().getEmployeeManager();

//employeeManager.updateChecklistValues(employeeList);

for(Iterator employeeIter = employeeList.iterator(); employeeIter.hasNext() ;)
{
EmployeeForm employeeRow = (EmployeeForm) employeeIter.next();
String empIDkey = employeeRow.getEmployeeID();
EmployeeImpl employee = (EmployeeImpl)employeeManager.findEmployeeByID(empIDkey);
if(employee != null)
{
employee.setCheck1(employeeRow.isCheck1());
employee.setCheck2(employeeRow.isCheck2());
employee.setCheck3(employeeRow.isCheck3());

employeeManager.saveEmployee(employee);
failed = false;
}
}
}

The red text is where I'm scratching my head. It works going false to true but I can't UNcheck the boxes to get back to false.

Help? thanks!
Greg C Robinson
Greenhorn

Joined: Jan 30, 2009
Posts: 4
One additional note to add: on my EmployeeForm, I do have a reset method where my boolean values get explicitly set to false. However, while this does get called and works fine for individual employee record updates, that reset method is NOT getting when using this checklist. the iteration is a collection of EmployeeForm beans, but the reset does not get referenced. I do not currently have a reset method in my ChecklistItemForm.

Hoping someone can help! thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! Form to update multiple records upon save
 
Similar Threads
html Checkbox value not getting
MultiActionController issue
Read table values in Action
Extracting a parameter from c:out value
Passing check box value to servlet.