Hi Hubert,

Yes, I get nulls for af1 and af2... I do have getters and setters for them.

Lemme throw some data your way... I was hoping to avoid posting all this,
but I suspect there to be something my eyes just aren't catching, so...


Here's ActionForm1:


package app.test.formbeans;
import org.apache.struts.action.ActionForm;
public class Action1Form extends ActionForm {
  private String field1;
  public void setField1(String inField1) {
    this.field1 = inField1;
  }
  public String getField1() {
    return this.field1;
  }
}


ActionForm2 is identical, just replacing the number 1 with 2 everywhere. 
Here's MasterForm:


package app.test.formbeans;
import org.apache.struts.action.ActionForm;
public class MasterForm extends ActionForm {
  public Action1Form action1Form;
  public Action2Form action2Form;
  public void setAction1Form(Action1Form inAction1Form) {
    this.action1Form = inAction1Form;
  }
  public Action1Form getAction1Form() {
    return this.action1Form;
  }
  public void setAction2Form(Action2Form inAction2Form) {
    this.action2Form = inAction2Form;
  }
  public Action2Form getAction2Form() {
    return this.action2Form;
  }
}


The Action everything gets submitted to is just a ForwardAction, which
forwards to:


<%@ page language="java" import="app.test.formbeans.*" %>
<%
  MasterForm masterForm = (MasterForm)request.getAttribute("masterForm");
%>
<%="masterForm = " + masterForm + "<br>"%>
<%
  Tab1Form tab1Form = null;
  Tab2Form tab2Form = null;
  if (masterForm != null) {
    tab1Form = masterForm.getTab1Form();
    tab2Form = masterForm.getTab2Form();
  }
%>
<%="tab1Form = " + tab1Form + "<br>"%>
<%="tab2Form = " + tab2Form + "<br>"%>
<% if (tab1Form != null) { %>
  <%="field1 = " + tab1Form.getField1() + "<br>"%>
<% } %>
<% if (tab2Form != null) { %>
  <%="field2 = " + tab2Form.getField2() + "<br>"%>
<% } %>


The result I see from this is:


masterForm = [EMAIL PROTECTED]
tab1Form = null
tab2Form = null


The page that does that submit, in its final rendered form on the client is:


<html>
<head>
  <script>
    var finalSubmit = null;
    function getXHR() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    function submitAll() {
      var tab1Form = dojo.io.encodeForm(
        document.getElementById("tab1Form"));
      var tab2Form = dojo.io.encodeForm(
        document.getElementById("tab2Form"));
      finalSubmit = getXHR();
      finalSubmit.onreadystatechange = finalSubmitCallback;
      finalSubmit.open("POST", "final.do", true);
      finalSubmit.send(tab1Form + tab2Form);
    }
    function finalSubmitCallback() {
      if (finalSubmit.readyState == 4) {
        document.getElementById("divResult").innerHTML =
finalSubmit.responseText;
      }
    }
  </script>
</head>
<body onload="init();">
  <form name="masterForm" id="tab1Form" method="post" action="/test/tab1.do">
    Field1: <input name="tab1Form.field1" value="val1" type="text"><br>
  </form>
  <br>
  <form name="masterForm" id="tab2Form" method="post" action="/test/tab2.do">
    Field2: <input name="tab2Form.field2" value="val2" type="text"><br>
  </form>
  <br>
  <input value="Submit All" onclick="submitAll();" type="button">
  <br><br>
  <div id="divResult"></div>
</body>
</html>


Note that I removed all the Dojo-related stuff, and some stuff that isn't
related to this submissino, just to try and keep it small, but, the
important thing to note is that when the submission is made, the request
that goes across is a POST to http://localhost:8080/test/final.do, as
expected, and the POST body is:

tab1Form.field1=val1&tab2Form.field2=val2&

...also as expected, verified in Firebug.  Lastly, here's struts-config:


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd";>
<struts-config>
  <form-beans>
    <form-bean name="masterForm" type="app.test.formbeans.MasterForm" />
  </form-beans>
  <action-mappings>
    <action path="/final" type="app.test.actions.FinalAction"
name="masterForm" scope="request" validate="false">
      <forward name="defaultForward" path="/final.jsp" />
    </action>
  </action-mappings>
</struts-config>


Note here that I removed the stuff that generated the markup from above
too, since that seems to be working just fine, I'm pretty sure this is all
that would be relevant.

So, seeing any obvious blunders on my part?  Thanks Hubert!

Frank


-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
 Supplying the wheel, so you don't have to reinvent it!

On Fri, September 22, 2006 1:10 pm, Hubert Rabago wrote:
> So what does happen when you submit the form?  Are you getting NPEs?
>
> By the time Struts attempts to populate the form bean, the nested
> forms should already be instantiated.  You should have getters and
> setters for af1 and af2, and getAf1() and getAf2() should not return a
> null.  This could be an issue if the form bean is new, either because
> it's in request scope, or in session scope but was not previously
> instantiated.
>
> Hubert
>
> On 9/22/06, Frank W. Zammetti <[EMAIL PROTECTED]> wrote:
>> I'm facing a situation where ideally I'd like to have a single
>> ActionForm
>> like so:
>>
>> public class MyActionForm extends ActionForm {
>>   AnotherActionForm1 af1;
>>   AnotherActionForm2 af2;
>> }
>>
>> When in my JSP I do:
>>
>> <html:text property="af1.field1" />
>>
>> ...I do indeed see the value as I expect.  The way I did this is to have
>> an Action mapping that references MyActionForm, then within the Action I
>> instantiate AnotherActionForm1, set the value of field1 on it, and then
>> set af1 to that new AnotherActionForm1 instance.  That all works nicely.
>>
>> Here's where I run into a problem...
>>
>> On the page I basically have 2 separate HTML forms, one for
>> AnotherActionForm1 and another for AnotherActionForm2.  However, I have
>> a
>> SINGLE submit button at the end (just plain button that makes an AJAX
>> request).  onClick of that button, I create a query string from the
>> values
>> of both forms, and make a request to the server with the query string
>> attached.  The parameter names of the query string arguments are
>> af1.field1 and af2.field2, mimicing what's in MyActionForm.  The mapping
>> that the request goes to again references MyActionForm.
>>
>> What I've been told should happen is that Struts should be able to
>> instantiate those two nested beans and set the properties, since the
>> parameter names give it the information it needs.  This does not however
>> seem to be happening.
>>
>> So, two questions... first, is that in fact the expected behavoir?  And
>> two, if so, any ideas what I'm doing wrong?  I can post all the
>> pertinent
>> config and code, but thought it might be something more generic that
>> wouldn't require all that.  This is the first time I've ever had a need
>> to
>> nest forms like this, so it's a new question for me.
>>
>> Thanks all!
>>
>> Frank
>>
>>
>> --
>> Frank W. Zammetti
>> Founder and Chief Software Architect
>> Omnytex Technologies
>> http://www.omnytex.com
>> AIM/Yahoo: fzammetti
>> MSN: [EMAIL PROTECTED]
>> Author of "Practical Ajax Projects With Java Technology"
>>  (2006, Apress, ISBN 1-59059-695-1)
>> Java Web Parts - http://javawebparts.sourceforge.net
>>  Supplying the wheel, so you don't have to reinvent it!
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to