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 2005/11/02 16:57:54 UTC

Re: java.util.Date in Action Form Bean

[ Oh, I missed my cue! :) ]

If your form bean maps to a business object (and it sounds like it
does since you're using BeanUtils), you can try FormDef:
http://formdef.dev.java.net/

You won't need to create a separate ActionForm class or manually
define your DynaActionForm, FormDef will configure it for you.  You
can also specify how you want your fields formatted, including dates
and numbers, and use FormDef's conversion methods in place of
BeanUtils.

Hubert

On 10/31/05, Yujun Liang <yu...@acm.org> wrote:
> Laurie,
>
> Thanks for all the replies.
>
> Can you point me to a document describe how to change the default converter
> used by Struts? I was not aware of that. By the way, what's the disadvantage
> to have common beanutil to support java.util.Date as well?
>
> Thanks
>
> On 10/30/05, Laurie Harper <la...@holoweb.net> wrote:
> >
> > Right, I create a fresh instance of BeanUtilsBean and register the
> > converter at the point of use, to avoid that being a problem.
> >
> > L.

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


Re: java.util.Date in Action Form Bean

Posted by Hubert Rabago <hr...@gmail.com>.
On 11/3/05, Yujun Liang <yu...@acm.org> wrote:
> Hubert,
>
> Good morning! ( Since I am sending it from Australia.)

Good morning!

> How can I convert an input from user to the Date field base on the Locale?
> I mean the locale is a varible during the session and conversion can
> handle multiple locales.
> Sorry for asking this, but in a locale aware web application, the
> input is based on Locale
>
> For example, in US, enter 10/12/2005 it will be converted to October
> 12, 2005 and this is December 10, 2005
> if the locale is Australia. How the global converter handles this?

Instead of the format itself, you can specify a key into your
application resource files:

        <!-- define the default format for use by all Date fields -->
        <global-converter for="property-type"
            target="java.util.Date"
            key="format.date"/>

In your resource files, define the format you want to use for that locale:
in example.properties (default for Aus?):
format.date=dd/MM/yyyy

in example_en_US.properties:
format.date=MM/dd/yyyy

The binary download for FormDef includes locale.war which demonstrates
this capability.

hth,
Hubert

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


Re: java.util.Date in Action Form Bean

Posted by Murray Collingwood <mu...@focus-computing.com.au>.
Thank God I haven't ventured into Struts validation - what a waste of time if it only does 
String and boolean.

mc

On 8 Nov 2005 at 23:10, Hubert Rabago wrote:

> I feel I have to explain something about Struts form beans.
> 
> It is not recommended that you use fields other than String and
> boolean in your form beans.  



FOCUS Computing - web design
Mob: 0415 24 26 24
murray@focus-computing.com.au
http://www.focus-computing.com.au




-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.362 / Virus Database: 267.12.8/163 - Release Date: 8/11/2005


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


Re: java.util.Date in Action Form Bean

Posted by Hubert Rabago <hr...@gmail.com>.
This is exactly what FormDef does.  It reads your existing bean and
configures a form bean for it.  Reuse.

You can continue to use BeanUtils with the FormDef-configured beans as
you would any other form bean, or you can also use FormDef's
setFormValues()/getFormValues() and take advantage of its
formatting/parsing functionality.

Hubert

On 11/8/05, Yujun Liang <yu...@acm.org> wrote:
> Hubert,
>
> You made a very good point. Thanks for your patient explanation, that's
> exactly the problem I am facing, all int fields are initialized to 0.
> Probably even I want to use this feature, I need design a DTO for the form,
> and put all String or boolean values as properties.
>
> At beginning I just wanted to re-use some assets, but it turns out some idea
> is not feasible, even it is doable.
>
> Thanks.
>

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


Re: java.util.Date in Action Form Bean

Posted by Yujun Liang <yu...@acm.org>.
Hubert,

You made a very good point. Thanks for your patient explanation, that's
exactly the problem I am facing, all int fields are initialized to 0.
Probably even I want to use this feature, I need design a DTO for the form,
and put all String or boolean values as properties.

At beginning I just wanted to re-use some assets, but it turns out some idea
is not feasible, even it is doable.

Thanks.

On 11/9/05, Hubert Rabago <hr...@gmail.com> wrote:
>
> On 11/8/05, Yujun Liang <yu...@acm.org> wrote:
> > in Struts, you can define
> > <form-bean name="instructionForm"
> type="org.apache.struts.validator.DynaValidatorForm">
> > <form-property name="instruction" type="
> com.clear2pay.bph.bean.Instruction"/>
> > </form-bean>
> >
> > and you can just call,
> >
> > bean = ((DynaActionForm) form).get(pageName);
> > in this case pageName = "instruction";
> >
> > and Struts automatically populate a
> > com.clear2pay.bph.bean.Instructionobject in the form,
>
>
> I feel I have to explain something about Struts form beans.
>
> It is not recommended that you use fields other than String and
> boolean in your form beans. The form bean is where the request
> parameters are stored before your fields are even validated. If
> validation fails, you'd ideally want to preserve what the user typed
> in so s/he can correct his/her input.
>
> For instance, if the user typed in "1q3" in field that's meant to hold
> a numeric value, the field will be marked invalid and the form will be
> shown to the user again. If the form bean used a String for this
> property, the user will see the "1q3" value on the input control. If
> the form bean used an int for the property, the attempt to populate
> that field with "1q3" would fail and the user would see "0" instead.
> The same thing applies to date fields. I don't even know what will
> happen if the user typed in "35 Se[ 2005" on a field that's declared
> as java.util.Date. The best approach is to use String fields, so the
> user's input is preserved, and in the case of user errors, they will
> be able to edit the values they typed in instead of starting over
> again.
>
> Of course, each application has its own set of circumstances, so this
> may not be an issue for you. If you're still interested in checking
> out FormDef, though, take a look at
> http://www.rabago.net/struts/formdef/dev/nested.htm . This page
> describes FormDef's support for nested beans with the latest dev
> builds..
>
> Hubert
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


--
Yujun Liang
yujun.liang@acm.org

Re: java.util.Date in Action Form Bean

Posted by Hubert Rabago <hr...@gmail.com>.
On 11/8/05, Yujun Liang <yu...@acm.org> wrote:
> in Struts, you can define
> <form-bean name="instructionForm"
             type="org.apache.struts.validator.DynaValidatorForm">
>   <form-property name="instruction" type="com.clear2pay.bph.bean.Instruction"/>
> </form-bean>
>
> and you can just call,
>
> bean = ((DynaActionForm) form).get(pageName);
> in this case pageName = "instruction";
>
> and Struts automatically populate a
> com.clear2pay.bph.bean.Instructionobject in the form,


I feel I have to explain something about Struts form beans.

It is not recommended that you use fields other than String and
boolean in your form beans.  The form bean is where the request
parameters are stored before your fields are even validated.  If
validation fails, you'd ideally want to preserve what the user typed
in so s/he can correct his/her input.

For instance, if the user typed in "1q3" in field that's meant to hold
a numeric value, the field will be marked invalid and the form will be
shown to the user again.  If the form bean used a String for this
property, the user will see the "1q3" value on the input control.  If
the form bean used an int for the property, the attempt to populate
that field with "1q3" would fail and the user would see "0" instead. 
The same thing applies to date fields.  I don't even know what will
happen if the user typed in "35 Se[ 2005" on a field that's declared
as java.util.Date.  The best approach is to use String fields, so the
user's input is preserved, and in the case of user errors, they will
be able to edit the values they typed in instead of starting over
again.

Of course, each application has its own set of circumstances, so this
may not be an issue for you.  If you're still interested in checking
out FormDef, though, take a look at
http://www.rabago.net/struts/formdef/dev/nested.htm .  This page
describes FormDef's support for nested beans with the latest dev
builds..

Hubert

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


Re: java.util.Date in Action Form Bean

Posted by Yujun Liang <yu...@acm.org>.
Hubert,

I checked formDef and but I found out it is not what I want, here is the
reason, in Struts, you can define
<form-bean name="instructionForm" type="
org.apache.struts.validator.DynaValidatorForm">
<form-property name="instruction" type="com.clear2pay.bph.bean.Instruction
"/>
</form-bean>

and you can just call,

bean = ((DynaActionForm) form).get(pageName);
in this case pageName = "instruction";

and Struts automatically populate a
com.clear2pay.bph.bean.Instructionobject in the form,

In formDef, you have to call
sampler = (TypeSampler) FormUtils.getFormValues(form,this,mapping,request);
to populate the form.

The problem of Struts form is it can't handle Locale data yet. So the field
in the bean can't be java.util.Date type, that's something bothers me.
Otherwise, Struts bean population is perfect.

Anyway, formDef is a great idea and it did save some configuration effort,
but it should be better if Struts bean populator can handle the locale
conversion automatically or by configuration.


OK, let me ask this way, how can I put two instances of the employee on the
same HTML form by using formDef?

If using Struts, I can put

<form-bean name="employeeFrom" type="
org.apache.struts.validator.DynaValidatorForm">
<form-property name="employee1" type="com.clear2pay.bph.bean.Employee"/>
<form-property name="employee2" type="com.clear2pay.bph.bean.Employee"/>
</form-bean>

<form ...>
<TABLE border=1 cellpadding=1 cellspacing=0>
<TR><TD>Name
<TD><input type="text" name="employee1.name <http://employee1.name>"
value="John Doe">
<TR><TD>Birth (DD MMM YYYY)
<TD><input type="text" name="employee1.birth" value="25 Sep 1950">
<TR><TD>Salary
<TD><input type="text" name="employee1.salary" value="40.000,50">
<TR><TD>Hire (DD.MM.YYYY)
<TD><input type="text" name="employee1.hireDate" value="15.02.1999">
</TABLE>

<TABLE border=1 cellpadding=1 cellspacing=0>
<TR><TD>Name
<TD><input type="text" name="employee2.name <http://employee2.name>"
value="John Doe">
<TR><TD>Birth (DD MMM YYYY)
<TD><input type="text" name="employee2.birth" value="25 Sep 1950">
<TR><TD>Salary
<TD><input type="text" name="employee2.salary" value="40.000,50">
<TR><TD>Hire (DD.MM.YYYY)
<TD><input type="text" name="employee2.hireDate" value="15.02.1999">
</TABLE>
</form>

Thanks.


On 11/4/05, Yujun Liang <yu...@acm.org> wrote:
>
> Hubert,
>
> Good morning! ( Since I am sending it from Australia.)
>
> I am reading the document on formDef, it is such a nice plugin so why
> don't you incorporate it into Struts? It will make project much productive.
>
> Question for you,
>  Property type global converters
>
> Property type global converters are applied to all fields of a certain
> type. An example of this would be declaring the Date converter that's
> declared to handle Date fields.
>
>     <form-definition>
>         <global-converters>
>             <global-converter *for="property-type"
>                 target="java.util.Date"*
>                 param="MM/dd/yyyy"/>
>
>         </global-converters>
>     </form-definition>
>
> How can I convert an input from user to the Date field base on the Locale?
> I mean the locale is a varible during the session and conversion can handle multiple locales.
>
> Sorry for asking this, but in a locale aware web application, the input is based on Locale
>
> For example, in US, enter 10/12/2005 it will be converted to October 12, 2005 and this is December 10, 2005
> if the locale is Australia. How the global converter handles this?
>
>
> Thanks for your time.
>
> Regards
>
>
> On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
> >
> > FormDef works fine with Validator. For the most part they won't even
> > be aware of each other. There's no overlap in functionality and they
> > won't get in the way of one another.
> >
> > Hubert
> >
> > On 11/2/05, Yujun Liang < yujun.liang@acm.org> wrote:
> > > Hubert, your input is quite valuable. But I just wonder this suppose
> > to be
> > > handled by Struts itsself,
> > >
> > > You may also place a bean instance on your form, and use nested
> > property
> > > references. For example, you might have a "customer" bean on your
> > > ActionForm, and then refer to the property
> > > "customer.name <http://customer.name>< http://customer.name>"
> > > in your presentation page. This would correspond to the methods
> > > customer.getName() and customer.setName(String Name) on your customer
> > bean.
> > > See the Apache Struts Taglib Developer Guides for more about using the
> >
> > > nested syntax.
> > > http://struts.apache.org/struts-core/userGuide/building_controller.html#action_form_classes
> >
> > >
> > > Do you know whether this BeanDef works with Validator?
> > >
> > > Thanks.
> > >
> > > On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
> > > >
> > > > [ Oh, I missed my cue! :) ]
> > > >
> > > > If your form bean maps to a business object (and it sounds like it
> > > > does since you're using BeanUtils), you can try FormDef:
> > > > http://formdef.dev.java.net/
> > > >
> > > > You won't need to create a separate ActionForm class or manually
> > > > define your DynaActionForm, FormDef will configure it for you. You
> > > > can also specify how you want your fields formatted, including dates
> >
> > > > and numbers, and use FormDef's conversion methods in place of
> > > > BeanUtils.
> > > >
> > > > Hubert
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>
> --
> Yujun Liang
> yujun.liang@acm.org
>



--
Yujun Liang
yujun.liang@acm.org

Re: java.util.Date in Action Form Bean

Posted by Yujun Liang <yu...@acm.org>.
Hubert,

Good morning! ( Since I am sending it from Australia.)

I am reading the document on formDef, it is such a nice plugin so why don't
you incorporate it into Struts? It will make project much productive.

Question for you,
 Property type global converters

 Property type global converters are applied to all fields of a certain
type. An example of this would be declaring the Date converter that's
declared to handle Date fields.

    <form-definition>
        <global-converters>
            <global-converter *for="property-type"
                target="java.util.Date"*
                param="MM/dd/yyyy"/>
        </global-converters>
    </form-definition>

How can I convert an input from user to the Date field base on the Locale?
I mean the locale is a varible during the session and conversion can
handle multiple locales.
Sorry for asking this, but in a locale aware web application, the
input is based on Locale

For example, in US, enter 10/12/2005 it will be converted to October
12, 2005 and this is December 10, 2005
if the locale is Australia. How the global converter handles this?

Thanks for your time.

Regards


On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
>
> FormDef works fine with Validator. For the most part they won't even
> be aware of each other. There's no overlap in functionality and they
> won't get in the way of one another.
>
> Hubert
>
> On 11/2/05, Yujun Liang <yu...@acm.org> wrote:
> > Hubert, your input is quite valuable. But I just wonder this suppose to
> be
> > handled by Struts itsself,
> >
> > You may also place a bean instance on your form, and use nested property
> > references. For example, you might have a "customer" bean on your
> > ActionForm, and then refer to the property
> > "customer.name <http://customer.name><http://customer.name>"
> > in your presentation page. This would correspond to the methods
> > customer.getName() and customer.setName(String Name) on your customer
> bean.
> > See the Apache Struts Taglib Developer Guides for more about using the
> > nested syntax.
> >
> http://struts.apache.org/struts-core/userGuide/building_controller.html#action_form_classes
> >
> > Do you know whether this BeanDef works with Validator?
> >
> > Thanks.
> >
> > On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
> > >
> > > [ Oh, I missed my cue! :) ]
> > >
> > > If your form bean maps to a business object (and it sounds like it
> > > does since you're using BeanUtils), you can try FormDef:
> > > http://formdef.dev.java.net/
> > >
> > > You won't need to create a separate ActionForm class or manually
> > > define your DynaActionForm, FormDef will configure it for you. You
> > > can also specify how you want your fields formatted, including dates
> > > and numbers, and use FormDef's conversion methods in place of
> > > BeanUtils.
> > >
> > > Hubert
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


--
Yujun Liang
yujun.liang@acm.org

Re: java.util.Date in Action Form Bean

Posted by Hubert Rabago <hr...@gmail.com>.
FormDef works fine with Validator.  For the most part they won't even
be aware of each other.  There's no overlap in functionality and they
won't get in the way of one another.

Hubert

On 11/2/05, Yujun Liang <yu...@acm.org> wrote:
> Hubert, your input is quite valuable. But I just wonder this suppose to be
> handled by Struts itsself,
>
> You may also place a bean instance on your form, and use nested property
> references. For example, you might have a "customer" bean on your
> ActionForm, and then refer to the property
> "customer.name<http://customer.name>"
> in your presentation page. This would correspond to the methods
> customer.getName() and customer.setName(String Name) on your customer bean.
> See the Apache Struts Taglib Developer Guides for more about using the
> nested syntax.
> http://struts.apache.org/struts-core/userGuide/building_controller.html#action_form_classes
>
> Do you know whether this BeanDef works with Validator?
>
> Thanks.
>
> On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
> >
> > [ Oh, I missed my cue! :) ]
> >
> > If your form bean maps to a business object (and it sounds like it
> > does since you're using BeanUtils), you can try FormDef:
> > http://formdef.dev.java.net/
> >
> > You won't need to create a separate ActionForm class or manually
> > define your DynaActionForm, FormDef will configure it for you. You
> > can also specify how you want your fields formatted, including dates
> > and numbers, and use FormDef's conversion methods in place of
> > BeanUtils.
> >
> > Hubert

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


Re: java.util.Date in Action Form Bean

Posted by Yujun Liang <yu...@acm.org>.
Hubert, your input is quite valuable. But I just wonder this suppose to be
handled by Struts itsself,

You may also place a bean instance on your form, and use nested property
references. For example, you might have a "customer" bean on your
ActionForm, and then refer to the property
"customer.name<http://customer.name>"
in your presentation page. This would correspond to the methods
customer.getName() and customer.setName(String Name) on your customer bean.
See the Apache Struts Taglib Developer Guides for more about using the
nested syntax.
http://struts.apache.org/struts-core/userGuide/building_controller.html#action_form_classes

Do you know whether this BeanDef works with Validator?

Thanks.

On 11/3/05, Hubert Rabago <hr...@gmail.com> wrote:
>
> [ Oh, I missed my cue! :) ]
>
> If your form bean maps to a business object (and it sounds like it
> does since you're using BeanUtils), you can try FormDef:
> http://formdef.dev.java.net/
>
> You won't need to create a separate ActionForm class or manually
> define your DynaActionForm, FormDef will configure it for you. You
> can also specify how you want your fields formatted, including dates
> and numbers, and use FormDef's conversion methods in place of
> BeanUtils.
>
> Hubert
>
> On 10/31/05, Yujun Liang <yu...@acm.org> wrote:
> > Laurie,
> >
> > Thanks for all the replies.
> >
> > Can you point me to a document describe how to change the default
> converter
> > used by Struts? I was not aware of that. By the way, what's the
> disadvantage
> > to have common beanutil to support java.util.Date as well?
> >
> > Thanks
> >
> > On 10/30/05, Laurie Harper <la...@holoweb.net> wrote:
> > >
> > > Right, I create a fresh instance of BeanUtilsBean and register the
> > > converter at the point of use, to avoid that being a problem.
> > >
> > > L.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


--
Yujun Liang
yujun.liang@acm.org