aspose file tools*
The moose likes Struts and the fly likes Retrieve Form Values - null form values returned Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Retrieve Form Values - null form values returned" Watch "Retrieve Form Values - null form values returned" New topic
Author

Retrieve Form Values - null form values returned

Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Hi All,
I�m having a little problem and was wondering if you could take a look. I�m trying to retrieve the data entered on my screen, but to no avail I can't find it. The following snippets show what I am doing. Any indication as to what I am missing? Do I need to subclass the DynaActionForm (I really wanted to keep the DynaActioForm definition in the xml file and not create a class) and add methods for getting and setting the collection object � this is the only thing I haven�t tried yet. The data displays on the screen and are indexed correctly.
Any help would be greatly ppreciated.
Thanks,
Annmarie
(1) struts-config.xml
<form-bean name="SetUpLicensesForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="hCompanyId" type="java.lang.String"/>
<form-property name="hListLength" type="java.lang.String"/>
<form-property name="licenses" type="java.util.Collection"/>
</form-bean>
<action name="SetUpLicensesForm"
path="/setUpLicensesAction"
type="qwbs.iseries.cclmWeb.cclm.actions.SetUpLicensesAction" scope="request">
<forward name="continue" path='/cclm/jsp/SetUpLicenses.jsp' redirect="false" contextRelative="true"/>
</action>

<action name="SetUpLicensesForm"
path="/setUpLicensesDispatchAction"
type="qwbs.iseries.cclmWeb.cclm.actions.SetUpLicensesDispatchAction"
parameter="method" scope="request">
<forward name="tryagain" path='/cclm/jsp/SetUpLicenses.jsp' redirect="false" contextRelative="true"/>
<forward name="success" path="/allCompaniesListDispatchAction.do" redirect="true" contextRelative="true" />
<forward name="cancel" path="/allCompaniesListDispatchAction.do" contextRelative="true" redirect="true"/>
</action>
(2) Action class snippet that sets up form
f.set("hCompanyId",companyLicenses.getCompanyId());
f.set("hListLength",Integer.toString(companyLicenses.getLicenses().size()));
f.set("licenses",(Collection) companyLicenses.getLicenses()); <-- THIS IS THE COLLECTION
(3) Jsp snippet (displays correctly)
<logic:iterate id="license" name="SetUpLicensesForm" property="licenses">
<tr>
<html:hidden name="license" property="applicationId" indexed="true" />
<html:hidden name="license" property="applicationFamilyId" indexed="true" />
<html:hidden name="license" property="applicationName" indexed="true" />
<html:hidden name="license" property="applicationVersion" indexed="true" />
<td class="tableText"><jsp:getProperty name="license" property="applicationName"/></td>
<td class="tableText"><jsp:getProperty name="license" property="applicationVersion" /></td>
<td class="tableSelect"> IGNORE THE CHECKBOXES FOR NOW
<logic:equal name="license" property="isGranted" value="true">
<input type="checkbox" name="grantedCheckbox" value="checkbox" checked >
</logic:equal>
<logic:notEqual name="license" property="isGranted" value="true">
<input type="checkbox" name="grantedCheckbox" value="checkbox" >
</logic:notEqual>
<html:checkbox name="license" property="isGranted" indexed="true" />
</td>
<td class="tableText">
<html:text styleId="calendar" name="license" property="validFromDate" indexed="true" styleClass="cellValue" size="10" />
</td>
<td class="tableText">
<html:text styleId="calendar2" name="license" property="validToDate" indexed="true" styleClass="cellValue" size="10" />
</td>
</tr>
</logic:iterate>
(4) Action that handles the response snippet. This is where the problem is � upon return the licenses collection is null.
String companyId = (String) setUpLicensesForm.get("hCompanyId");
String listLength = (String) setUpLicensesForm.get("hListLength");
Collection licenses = (Collection) setUpLicensesForm.get("licenses");
if (licenses == null) {
* * * * MY PROBLEM -- THE LICENSES COLLECTION IS NULL * * * *
log.log(XLevel.TRACE, " SetUpLicensesDispatchAction : licenses is null ");
}
else
{
License l = null;
for (Iterator it=licenses.iterator();it.hasNext(); ) {
l = (License) it.next();
//Do update here ...
}
}
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Your form has request scope. Because of that, a form is created when you enter the jsp and another form is created/recycled when you leave the jsp. Those are each a request from the user (enterring and leaving). You set values enterring the page and they "disappear" when you leave the page because it's not the same copy of the form that you had before.


A good workman is known by his tools.
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Marc,
Thanks for your response, but maybe I didn't state my question correctly.
I understand about request scope and as you pointed out - form values are re-filled when the page is submitted, therefore I should have all values. In my case I can retrieve other form values on the page, except for the values in my collection. That is the problem. The collection values (which are updated on the screen) are not returned.
Do I need to do something different with my collection in order for the values to be repopulated upon submit? Are there problems with using a DynaActionForm for this function? Should I subclass the DynaActionForm?
Thanks,
Annmarie
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Ah... ok.
Shouldn't licenses be an array?
<form-property name="licenses" type="com.mypackage.License[]" size="optionalToSpecifySizeOfArray"/>
I thought that was how indexed worked was by passing along the index of the array as part of the getter/setter.
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Hi Marc,
Not sure if that will work, but I'll give it a whirl. I have tried creating an actionForm class, added a list with the proper getter/setter methods for the License object, but still no data coming back ... on the submit I get an error within the actionform on a get of an License object...all very odd. I'll keep plugging away. If you think of anything else jot it down. When I come up with a solution I'll post it.
Thanks!
Annmarie
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Here's the thing, Annmarie. When your form is newly created the second time, your collection is set to NULL. The ActionForm can't set anything there because there's nothing to set it to. If you put your form in session instead of request, you won't have this problem.
The other thing you might want to do is use <nested:iterate> looping over <nested:hidden> (etc...) instead of <logic:iterate> looping over <html:hidden> (etc...). What this will do is give you the correct names for your form fields when trying to set them back to your ActionForm. So your jsp would look something like this:

What you want to notice is the difference in the HTML generated by the nested tags versus the logic tags. Using the nested tags, your generated HTML will look like:

This will make it so that Struts can automatically set these values to your ActionForm where they exist. That is, it will still fail if there is no "licenses[1]" object in your ActionForm, or if your licenses collection is null.
Doing it the way you did, without nested tags, your HTML would look like this:

Both input elements would have the same name and Struts would therefore try to simply call setApplicationId() on your ActionForm for each value (or set("applicationId", "some Value") on your DynaActionForm). It would know nothing of your nested ActionForm.
Using the nested tags, it will handle it correctly if your nested forms are already set to your main form. This is where things get hosed by keeping it in request scope. Your nested forms, or in your case collection of nested forms, are set to null and Struts can't call the proper setters. This in conjunction with not using the nested tags is the reason for your problem.
HTH
[ April 14, 2004: Message edited by: Jason Menard ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

MMmmmmm... some good points.
So, how does the html look when you view the source in your browser???
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Thanks Jason,
I have set my scope to session and sure enough I do get data back, but the values don't reflect the changes that have been entered on the screen. I am going to try the nested:iterate -- using the logic:iterate my html looks like the following:
<input type="hidden" name="license[1].applicationId" value="NOAH10">
<input type="hidden" name="license[1].applicationFamilyId" value="NOAH">
<input type="hidden" name="license[1].applicationName" value="New Omni Alert Handler">
<input type="hidden" name="license[1].applicationVersion" value="1.0">
Notice that it marks the license object not the list (licenses[0].setApplicationId). Should I use nested:iterate for my input fields as well (text and checkbox)?
Thanks again. The entire company is new to Struts (all 6 of us - and no one has done a listed form yet), so your help is GREATLY appreciated!!!
Annmarie
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Annmarie Ziegler:
Notice that it marks the license object not the list (licenses[0].setApplicationId).

Yeah, that doesn't look quite right to me. it would also explain why your changes aren't being reflected.
Should I use nested:iterate for my input fields as well (text and checkbox)?
Yep. You can safely replace these tags with their <nested> equivalents. Make sure you check the API docs because there are some subtle differences (for example, you mostly only need the "property" attribute, not the "name" attribute). Also, you can replace your <jsp:getProperty> tags with <nested:write> tags (or <bean:write> if you'd prefer). Look here for tons more information on <nested> tags.
[ April 14, 2004: Message edited by: Jason Menard ]
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Hi Jason,
Looks like it will work ... I'll have to test more thoroughly tomorrow ... our test servers are going down for maint. I truly appreciate the help.
Thanks,
Annmarie
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Annmarie Ziegler:
Looks like it will work

Glad to hear it!
Annmarie Ziegler
Greenhorn

Joined: Apr 06, 2001
Posts: 21
Jason/Marc,
I was able to test and it works like a charm. Off to other issues.
Thanks for your help.
Annmarie
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727


Take the rest of the day off!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve Form Values - null form values returned