You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Hubert Rabago <hr...@gmail.com> on 2006/07/20 22:52:02 UTC

Re: FormDef

(was: Is there any direct link between model and view in struts..)

On 7/20/06, Michael Jouravlev <jm...@gmail.com> wrote:
Another library,
> FormDef, can build a dynaform based on properties of nested business
> object

Hey Michael,

Just thought I'd set the record straight.
FormDef does not require nor enforce nested beans.

public class Employee {
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public Date getHireDate() { return hireDate; }
    public void setHireDate(Date hireDate) { this.hireDate = hireDate; }

    public int getZipCode() { return zipCode; }
    public void setZipCode(int zipCode) { this.zipCode = zipCode; }
}

  ... plus ...

<form name="employeeForm" beanType="Employee"/>


  ... equals ...


<form-bean name="employeeForm" type="org.apache.struts.action.DynaActionForm">
    <form-property name="name" type="java.lang.String"/>
    <form-property name="hireDate" type="java.lang.String"/>
    <form-property name="zipCode" type="java.lang.String"/>
</form-bean>

You're still using traditional ActionForms, and you still need to copy
to and from the form bean and business object (using BeanUtils or
FormDef's own methods).

So, your form still looks the same way:

<html:text property="name"/>
<html:text property="hireDate"/>
<html:text property="zipCode"/>

FormDef support nested beans, and nested collections of beans.  If you
had a nested Address object, your form would look like:

<html:text property="name"/>
<html:text property="hireDate"/>
<html:text property="address.zipCode"/>


Hubert


[The first part of http://www.rabago.net/struts/formdef/nested.htm
explains the same thing, and goes on to explain how nested beans are
supported.]

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: FormDef

Posted by Michael Jouravlev <jm...@gmail.com>.
Thanks, now I got it. How could I not see the difference before? ;-)

On 7/21/06, Hubert Rabago <hr...@gmail.com> wrote:
> (inline)
>
> > -----Original Message-----
> > From: Michael Jouravlev [mailto:jmikus@gmail.com]
> > Sent: Thursday, July 20, 2006 6:16 PM
> > To: Struts Users Mailing List
> > Subject: Re: FormDef
> >
> > Um, by nested beans I mean a bean which is a property of ActionForm
> > like Employee, not a bean which is a property of a bean which is a
> > property of an ActionForm, like Address. I guess there is a
> > discrepancy in terminology here ;-)
> >
> > What I mean is this (taken from
> > http://www.rabago.net/struts/formdef/manual.htm):
> >
> > "To declare a form bean using FormDef, simple add a <form> entry in
> > the configuration file, provide the name for your form bean, and the
> > name of the class from which the form bean will be patterned after.
> > ... When the plugin gets executed, it will create a DynaActionForm
> > containing String fields for each property of MyBean.
> >
> > To me MyBean is nested within a DynaActionForm. If this is not a
> > proper term, what do you call it? Calling it just a property will do a
> > disservice (I think so) because it is not a primitive type.
>
> Actually, with FormDef, it will be a primitive type (err, String, actually).
> What FormDef generates is a String field for each field of MyBean.
>
> Okay, let's make sure we're talking about the same thing.
> If I were to just use struts-config.xml to define my forms, here's two
> ways I can do it.
>
> Here's a non-nested approach (Approach 1):
>
> <form-bean name="myForm" type="o.a.s.DynaActionForm>
>   <form-property name="field1" type="java.lang.String"/>
>   <form-property name="field2" type="java.lang.String"/>
> </form-bean>
>
> <html:form action="..">
>   <html:text property="field1"/>
>   <html:text property="field2"/>
> </html:form>
>
> BeanUtils.copyProperties(myForm, myBean);
>
>
> Here's my interpretation of a nested approach (Approach 2):
>
> <form-bean name="myForm" type="o.a.s.DynaActionForm>
>   <form-property name="myBean" type="MyBean"/>
> </form-bean>
>
> <html:form action="..">
>   <html:text property="myBean.field1"/>
>   <html:text property="myBean.field2"/>
> </html:form>
>
> myBean = ((DynaActionForm) myForm).get("myBean");
>
>
> FormDef use Approach 1.
>
> > So when converters and validators are executed? Is it correct that
> > when I call  FormUtils.setFormValues() then output converter is
> > executed (uh, do you have output converters?), and when I call
> > FormUtils.getFormValues() then input converters and validators are
> > executed? Validation or conversion is not executed automatically or at
> > least can be turned off?
>
>
> Yes, there are output converters.  Validation would typically be
> through Commons Validator (like most dyna forms).  Conversion happens
> based on convention but can be configured by the user.
>
>
> Hubert
>
>
> > On 7/20/06, Hubert Rabago <hr...@gmail.com> wrote:
> > > (was: Is there any direct link between model and view in struts..)
> > >
> > > On 7/20/06, Michael Jouravlev <jm...@gmail.com> wrote:
> > > Another library,
> > > > FormDef, can build a dynaform based on properties of
> > nested business
> > > > object
> > >
> > > Hey Michael,
> > >
> > > Just thought I'd set the record straight.
> > > FormDef does not require nor enforce nested beans.
> > >
> > > public class Employee {
> > >     public String getName() { return name; }
> > >     public void setName(String name) { this.name = name; }
> > >
> > >     public Date getHireDate() { return hireDate; }
> > >     public void setHireDate(Date hireDate) { this.hireDate
> > = hireDate; }
> > >
> > >     public int getZipCode() { return zipCode; }
> > >     public void setZipCode(int zipCode) { this.zipCode = zipCode; }
> > > }
> > >
> > >   ... plus ...
> > >
> > > <form name="employeeForm" beanType="Employee"/>
> > >
> > >
> > >   ... equals ...
> > >
> > >
> > > <form-bean name="employeeForm"
> > type="org.apache.struts.action.DynaActionForm">
> > >     <form-property name="name" type="java.lang.String"/>
> > >     <form-property name="hireDate" type="java.lang.String"/>
> > >     <form-property name="zipCode" type="java.lang.String"/>
> > > </form-bean>
> > >
> > > You're still using traditional ActionForms, and you still
> > need to copy
> > > to and from the form bean and business object (using BeanUtils or
> > > FormDef's own methods).
> > >
> > > So, your form still looks the same way:
> > >
> > > <html:text property="name"/>
> > > <html:text property="hireDate"/>
> > > <html:text property="zipCode"/>
> > >
> > > FormDef support nested beans, and nested collections of
> > beans.  If you
> > > had a nested Address object, your form would look like:
> > >
> > > <html:text property="name"/>
> > > <html:text property="hireDate"/>
> > > <html:text property="address.zipCode"/>
> > >
> > >
> > > Hubert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: FormDef

Posted by Hubert Rabago <hr...@gmail.com>.
(inline)

> -----Original Message-----
> From: Michael Jouravlev [mailto:jmikus@gmail.com]
> Sent: Thursday, July 20, 2006 6:16 PM
> To: Struts Users Mailing List
> Subject: Re: FormDef
>
> Um, by nested beans I mean a bean which is a property of ActionForm
> like Employee, not a bean which is a property of a bean which is a
> property of an ActionForm, like Address. I guess there is a
> discrepancy in terminology here ;-)
>
> What I mean is this (taken from
> http://www.rabago.net/struts/formdef/manual.htm):
>
> "To declare a form bean using FormDef, simple add a <form> entry in
> the configuration file, provide the name for your form bean, and the
> name of the class from which the form bean will be patterned after.
> ... When the plugin gets executed, it will create a DynaActionForm
> containing String fields for each property of MyBean.
>
> To me MyBean is nested within a DynaActionForm. If this is not a
> proper term, what do you call it? Calling it just a property will do a
> disservice (I think so) because it is not a primitive type.

Actually, with FormDef, it will be a primitive type (err, String, actually).
What FormDef generates is a String field for each field of MyBean.

Okay, let's make sure we're talking about the same thing.
If I were to just use struts-config.xml to define my forms, here's two
ways I can do it.

Here's a non-nested approach (Approach 1):

<form-bean name="myForm" type="o.a.s.DynaActionForm>
  <form-property name="field1" type="java.lang.String"/>
  <form-property name="field2" type="java.lang.String"/>
</form-bean>

<html:form action="..">
  <html:text property="field1"/>
  <html:text property="field2"/>
</html:form>

BeanUtils.copyProperties(myForm, myBean);


Here's my interpretation of a nested approach (Approach 2):

<form-bean name="myForm" type="o.a.s.DynaActionForm>
  <form-property name="myBean" type="MyBean"/>
</form-bean>

<html:form action="..">
  <html:text property="myBean.field1"/>
  <html:text property="myBean.field2"/>
</html:form>

myBean = ((DynaActionForm) myForm).get("myBean");


FormDef use Approach 1.

> So when converters and validators are executed? Is it correct that
> when I call  FormUtils.setFormValues() then output converter is
> executed (uh, do you have output converters?), and when I call
> FormUtils.getFormValues() then input converters and validators are
> executed? Validation or conversion is not executed automatically or at
> least can be turned off?


Yes, there are output converters.  Validation would typically be
through Commons Validator (like most dyna forms).  Conversion happens
based on convention but can be configured by the user.


Hubert


> On 7/20/06, Hubert Rabago <hr...@gmail.com> wrote:
> > (was: Is there any direct link between model and view in struts..)
> >
> > On 7/20/06, Michael Jouravlev <jm...@gmail.com> wrote:
> > Another library,
> > > FormDef, can build a dynaform based on properties of
> nested business
> > > object
> >
> > Hey Michael,
> >
> > Just thought I'd set the record straight.
> > FormDef does not require nor enforce nested beans.
> >
> > public class Employee {
> >     public String getName() { return name; }
> >     public void setName(String name) { this.name = name; }
> >
> >     public Date getHireDate() { return hireDate; }
> >     public void setHireDate(Date hireDate) { this.hireDate
> = hireDate; }
> >
> >     public int getZipCode() { return zipCode; }
> >     public void setZipCode(int zipCode) { this.zipCode = zipCode; }
> > }
> >
> >   ... plus ...
> >
> > <form name="employeeForm" beanType="Employee"/>
> >
> >
> >   ... equals ...
> >
> >
> > <form-bean name="employeeForm"
> type="org.apache.struts.action.DynaActionForm">
> >     <form-property name="name" type="java.lang.String"/>
> >     <form-property name="hireDate" type="java.lang.String"/>
> >     <form-property name="zipCode" type="java.lang.String"/>
> > </form-bean>
> >
> > You're still using traditional ActionForms, and you still
> need to copy
> > to and from the form bean and business object (using BeanUtils or
> > FormDef's own methods).
> >
> > So, your form still looks the same way:
> >
> > <html:text property="name"/>
> > <html:text property="hireDate"/>
> > <html:text property="zipCode"/>
> >
> > FormDef support nested beans, and nested collections of
> beans.  If you
> > had a nested Address object, your form would look like:
> >
> > <html:text property="name"/>
> > <html:text property="hireDate"/>
> > <html:text property="address.zipCode"/>
> >
> >
> > Hubert
> >

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: FormDef

Posted by Michael Jouravlev <jm...@gmail.com>.
Um, by nested beans I mean a bean which is a property of ActionForm
like Employee, not a bean which is a property of a bean which is a
property of an ActionForm, like Address. I guess there is a
discrepancy in terminology here ;-)

What I mean is this (taken from
http://www.rabago.net/struts/formdef/manual.htm):

"To declare a form bean using FormDef, simple add a <form> entry in
the configuration file, provide the name for your form bean, and the
name of the class from which the form bean will be patterned after.
... When the plugin gets executed, it will create a DynaActionForm
containing String fields for each property of MyBean.

To me MyBean is nested within a DynaActionForm. If this is not a
proper term, what do you call it? Calling it just a property will do a
disservice (I think so) because it is not a primitive type.

So when converters and validators are executed? Is it correct that
when I call  FormUtils.setFormValues() then output converter is
executed (uh, do you have output converters?), and when I call
FormUtils.getFormValues() then input converters and validators are
executed? Validation or conversion is not executed automatically or at
least can be turned off?

On 7/20/06, Hubert Rabago <hr...@gmail.com> wrote:
> (was: Is there any direct link between model and view in struts..)
>
> On 7/20/06, Michael Jouravlev <jm...@gmail.com> wrote:
> Another library,
> > FormDef, can build a dynaform based on properties of nested business
> > object
>
> Hey Michael,
>
> Just thought I'd set the record straight.
> FormDef does not require nor enforce nested beans.
>
> public class Employee {
>     public String getName() { return name; }
>     public void setName(String name) { this.name = name; }
>
>     public Date getHireDate() { return hireDate; }
>     public void setHireDate(Date hireDate) { this.hireDate = hireDate; }
>
>     public int getZipCode() { return zipCode; }
>     public void setZipCode(int zipCode) { this.zipCode = zipCode; }
> }
>
>   ... plus ...
>
> <form name="employeeForm" beanType="Employee"/>
>
>
>   ... equals ...
>
>
> <form-bean name="employeeForm" type="org.apache.struts.action.DynaActionForm">
>     <form-property name="name" type="java.lang.String"/>
>     <form-property name="hireDate" type="java.lang.String"/>
>     <form-property name="zipCode" type="java.lang.String"/>
> </form-bean>
>
> You're still using traditional ActionForms, and you still need to copy
> to and from the form bean and business object (using BeanUtils or
> FormDef's own methods).
>
> So, your form still looks the same way:
>
> <html:text property="name"/>
> <html:text property="hireDate"/>
> <html:text property="zipCode"/>
>
> FormDef support nested beans, and nested collections of beans.  If you
> had a nested Address object, your form would look like:
>
> <html:text property="name"/>
> <html:text property="hireDate"/>
> <html:text property="address.zipCode"/>
>
>
> Hubert
>
>
> [The first part of http://www.rabago.net/struts/formdef/nested.htm
> explains the same thing, and goes on to explain how nested beans are
> supported.]

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: FormDef

Posted by Hubert Rabago <hr...@gmail.com>.
On 8/2/06, Michael Jouravlev <jm...@gmail.com> wrote:
> On 7/20/06, Hubert Rabago <hr...@gmail.com> wrote:
> > FormDef support nested beans, and nested collections of beans.  If you
> > had a nested Address object, your form would look like:
> >
> > <html:text property="name"/>
> > <html:text property="hireDate"/>
> > <html:text property="address.zipCode"/>
>
> In your example Address is nested within ActionForm, right?

No.  What happens is an ActionForm is nested within an ActionForm.

((DynaBean) form).get("address") will return another DynaBean.

What if I
> have an object like Person having Address as its member. All
> properties are strongly-typed so I do not want to expose them for I/O,
> instead I want to have autogenerated ActionForm fields so that I could
> redisplay them on error. Say I do not nest Persion inside ActionForm.
> Can I still somehow have a string property inside my ActionForm that
> corresponds to Person.Address? Can this property be autogenerated?

This is how FormDef works.  Of course, you'll have to tell FormDef
about the address form, but that's very easy:

    <form name="addressForm"
        beanType="my.package.Address"/>

    <form name="employeeForm"
        beanType="my.package.Person">

        <!-- specify that our address field should use addressForm -->
        <field property="address" formName="addressForm"/>
    </form>

FormUtils.getFormValues() and setFormValues() takes care of handling
the nested objects, instantiating them as needed.

As you can imagine, they can go multiple levels, all nesting
Stringified versions of your business objects.  In one of my sample
apps, I have a "companyForm" with an "addressForm" and a collection of
"employeeForm" objects, with each employee having its own
"addressForm" nested form.

Hubert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: FormDef

Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/20/06, Hubert Rabago <hr...@gmail.com> wrote:
> FormDef support nested beans, and nested collections of beans.  If you
> had a nested Address object, your form would look like:
>
> <html:text property="name"/>
> <html:text property="hireDate"/>
> <html:text property="address.zipCode"/>

In your example Address is nested within ActionForm, right? What if I
have an object like Person having Address as its member. All
properties are strongly-typed so I do not want to expose them for I/O,
instead I want to have autogenerated ActionForm fields so that I could
redisplay them on error. Say I do not nest Persion inside ActionForm.
Can I still somehow have a string property inside my ActionForm that
corresponds to Person.Address? Can this property be autogenerated?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org