aspose file tools*
The moose likes Struts and the fly likes Submitting an Array of Forms in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Submitting an Array of Forms in Struts" Watch "Submitting an Array of Forms in Struts" New topic
Author

Submitting an Array of Forms in Struts

bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Hello there.

I have a form...which is actually an array of forms:

public class LineItemDisplayForm extends BaseForm
{
protected int lineItemCount;
protected LineItemForm[] lineItemForms;

...getters and setters...

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
boolean lineItemsEntered = false;

//line items
if (lineItemForms != null)
{
for (int i = 0; i < lineItemForms.length; i++)
{
lineItemsEntered = lineItemForms[i].validate(errors, "lineItemForms", Integer.toString(i + 1)) || lineItemsEntered;
}
}

return (errors);
}

}

When I submit this form I want to validate the submitted data. However, whenever I call validate() method above lineItemForms is always coming back null. Shouldn't this be coming back populated with the data that I've submitted? How can I make this happen so I can validate the data?

Thank you so much.
bryan

[ November 19, 2004: Message edited by: bryan nelson ]
[ November 22, 2004: Message edited by: bryan nelson ]

"...and the Truth will set you free."
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727



Sorry, I've seen an array of forms before.

When the user submits the page, an instance of LineItemDisplayForm is created (assuming it is in request scope). At this point protected DMSMSLineItemForm[] lineItemForms; is declared and defaults to null. It is never changed to anything other than null and that is why it is always null.


A good workman is known by his tools.
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
I was under the impression that when one submits a form in struts, that the getter associated with that field is automatically processed...thus populating lineItemForms for me? Is this wrong?

Here are my getters and setters:

/**
* @return Returns the lineItemCount.
*/
public int getLineItemCount()
{
return lineItemCount;
}

/**
* @param lineItemCount The lineItemCount to set.
*/
public void setLineItemCount(int theCount)
{
lineItemCount = theCount;

//make new array of lineitemforms with size of passed in count
LineItemForm[] tempLineItemForms = new LineItemForm[theCount];

//loop over existing lineitemforms array and copy into new lineitemforms array
for (int i = 0; i < theCount; i++)
{
tempLineItemForms[i] = lineItemForms[i];
}

//set existing array to null
lineItemForms = null;

//set existing array equal to new array
lineItemForms = tempLineItemForms;
}

/**
* @return Returns the lineItemForms.
*/
public LineItemForm[] getLineItemForms()
{
if (lineItemForms == null)
{
lineItemForms = new LineItemForm[lineItemCount];

for (int i = 0; i < lineItemCount; i++)
{
lineItemForms[i] = new LineItemForm();
}
}

return lineItemForms;
}

/**
* @param lineItemForms The lineItemForms to set.
*/
public void setLineItemForms(LineItemForm[] forms)
{
if (forms == null)
{
lineItemForms = new LineItemForm[lineItemCount];
}

lineItemForms = forms;

if (forms != null)
{
lineItemCount = lineItemForms.length;
}
}
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

OK. Your lineItemForms probably shouldn't be forms - ValueObjects, TransferObjects, beans, something else.

This
thread might help.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Originally posted by bryan nelson:
I was under the impression that when one submits a form in struts, that the getter associated with that field is automatically processed...thus populating lineItemForms for me? Is this wrong?


Your statement "when one submits a form in struts" leads me to believe that you have a misconception about Struts, one that seems to be fairly common. When the client submits an HTML form, the request is simply a stream of characters, part of which is a list of name-value pairs representing the form fields and their values. There are no objects involved here. You don't actually submit an ActionForm or any Java object.

When the HTTP request is received by the server, it gets translated into a Java object: an HttpServletRequest object. This object in turn gets passed to Struts, which then proceeds to do its "magic". Struts tries to match request parameter names to setters in the ActionForm. Since the parameters are still name-value pairs (see the HttpRequest.getParameterMap() method), which are Strings, there's a limit to what Struts can do in this matching up process. One thing Struts won't do for you is to instantiate member objects of your forms. You have to do that yourself. Thus, "populating lineItemForms for me" is not entirely true. Struts can populate them for you but they need to be there already; it won't instantiate LineItemForm objects for you.

If you post the JSP code that displays the line items, it will be easier for us to suggest a solution.
[ November 19, 2004: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
bryan nelson
Ranch Hand

Joined: Jun 16, 2003
Posts: 95
Thank you both very much for your time.

My jsp code is included below.
What I'm currently doing is reading lineItems in from an excel spreadsheet to populate this jsp (which is working fine).

<%@include file="/web/jsp/include/javaIncludes.jsp" %>
<%@ page import="java.util.*,java.lang.*,something.somethingelse.bean.dto.*"%>
<%@ page import="something.somethingelse.dao.*,something.somethingelse.util.*,java.io.*"%>
<%@ page import="something.somethingelse.logic.*"%>
<tr>
<td class="headingLeft" height="3%" colspan="2"><bean:message key='header.view.lineItemList'/></td>
</tr>
<html:form action="/notice/import/addExcelSubmitToUpload">
<tr>
<td colspan="4">
<html:errors/>
</td>
</tr>
<html:hidden property="lineItemCount" />
<tr>
<td class="top" height="3%" colspan="2">
<table class="standard" title="<bean:message key='title.view.lineItemList'/>" cellpadding="3">
<tr>
<th scope="col"><label for="pn"><acronym title="<bean:message key='acronym.verbiage.pn'/>"><bean:message key='acronym.abbreviation.pn'/></acronym></label></th>
<th scope="col"><label for="nomenclature"><bean:message key='field.partNomenclature'/></label></th>
<th scope="col"><label for="nsn"><acronym title="<bean:message key='acronym.verbiage.nsn'/>"><bean:message key='acronym.abbreviation.nsn'/></acronym></label></th>

</tr>
<tr>
<td class="lineBreak" colspan="15"></td>
</tr>

<c:forEach var="lineItemForms" items="${pageList.list}">
<tr>
<td>
<html:text name="lineItemForms" styleId="pn${status.count}" property="noticeLineItem.partNumber" tabindex="${((status.count - 1) * liTabIndexInputsPerRow) + liTabIndexOffset + 2}" size="14" maxlength="64" indexed="true"/>
</td>
<td>
<html:text name="lineItemForms" styleId="nomenclature${status.count}" property="noticeLineItem.nomenclature" tabindex="${((status.count - 1) * liTabIndexInputsPerRow) + liTabIndexOffset + 3}" size="14" maxlength="64" indexed="true"/>
</td>
<td>
<html:text name="lineItemForms" styleId="nsn${status.count}" property="noticeLineItem.nsn" tabindex="${((status.count - 1) * liTabIndexInputsPerRow) + liTabIndexOffset + 4}" size="14" maxlength="13" indexed="true"/>
</td>

</tr>

</c:forEach>
</table>
</td>
</tr>

<tr class="top">
<td class="left" width="1000">
<a href="#"><html:submit styleClass="formButton"><bean:message key='button.next'/></html:submit></a>
</td>
<td width="100%"> </td> <!--soaks up all resizing spaces-->
</tr>

</html:form>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submitting an Array of Forms in Struts