You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Yujun Liang <yu...@acm.org> on 2005/10/28 06:31:44 UTC

java.util.Date in Action Form Bean

Hello,

I am working on a project using Struts. Struts uses BeanUtil to populate Action Form Bean. This is such a nice framework to work with, except, 

BeanUtil doesn't support java.util.Date conversion. But when I use java.sql.Timestamp, it asks for the format of 
yyyy-mm-dd hh:mm:ss.fffffffff

Here is the exception when I enter other format,

    Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff

In a web application, it is not reasonable to ask user to input yyyy-mm-dd hh:mm:ss.fffffffff.

Also, it doesn't support Locale.

Do you have experience getting java.util.Date populated in a Java Bean inside a FormBean?


Thanks in advance.

Regards
Yujun Liang
yujun.liang@acm.org
(0408) 467 448

1. Form Bean Definition,
<form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm">

<form-property name="mybean" type="com.mycompany.myproject.bean.MyBean"/>

</form-bean>

2. Java Bean,

package com.mycompany.myproject.bean;

import java.io.Serializable;

import java.sql.Timestamp;

import org.apache.commons.lang.builder.EqualsBuilder;

import org.apache.commons.lang.builder.HashCodeBuilder;

import org.apache.commons.lang.builder.ToStringBuilder;

public abstract class MyBean implements Serializable {

    private long interchangeId;

    private Timestamp creationTime;

    private String originatingChannel;

    public long getInterchangeId() {

        return this.interchangeId;

    }

    public void setInterchangeId(long interchangeId) {

        this.interchangeId = interchangeId;

    }

    public Timestamp getCreationTime() {

        return this.creationTime;

    }

    

    public void setCreationTime(Timestamp creationTime) {

        this.creationTime = creationTime;

    }


    public String getOriginatingChannel() {

        return this.originatingChannel;

    }

    public void setOriginatingChannel(String originatingChannel) {

        this.originatingChannel = originatingChannel;

    }

}

3. HTML Form

<input name="mybean.creationTime" type="text" size="30" maxlength="30" value="25/10/2005">
<input name="mybean.originatingChannel" type="text" size="40" maxlength="40" value="originating channel1">
<input name="mybean.originatingChannelUserId" type="text" size="60" maxlength="60" value="originating_channel_userid1">

4. Exception.

[28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet Error]-[BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff

at org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimestampConverter.java:117)

at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)

at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)

at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)

at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)

at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)

at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)

at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)

at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)

at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)

at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)

Re: java.util.Date in Action Form Bean

Posted by Laurie Harper <la...@holoweb.net>.
So far I'm doing this by registering a custom implementation of 
Converter which I instantiate with a suitable instance of 
SimpleDateFormat before calling BeanUtilsBean.copyProperties().

There's also the beanutils.locale package, which provides Locale-aware 
conversions and includes support for java.util.Date (I think) out of the 
box. Unfortunately, it's not obvious how to use it. I posted a thread on 
the commons-user list asking for guidance, though I haven't seen a 
response yet.

For what it's worth, here's a rough outline of doing this the 'easy' 
(i.e. inelegant!) way:

   final SimpleDateFormat sdf = ...
   BeanUtilsBean utils = new BeanUtilsBean();
   ConvertUtilsBean ctuils = utils.getConvertUtils();
   cutils.register(new Converter() {
     public Object convert(Class clazz, Object value) {
       return sdf.parse((String) value);
     },
     Date.class);
   utils.copyProperties(...

That's untested code, and the implementation of convert() should incude 
checks for value being null, not a String, etc and handling for if the 
date can't be parsed... but it gives you the rough idea.

L.

Yujun Liang wrote:
> Hello,
> 
> I am working on a project using Struts. Struts uses BeanUtil to populate Action Form Bean. This is such a nice framework to work with, except, 
> 
> BeanUtil doesn't support java.util.Date conversion. But when I use java.sql.Timestamp, it asks for the format of 
> yyyy-mm-dd hh:mm:ss.fffffffff
> 
> Here is the exception when I enter other format,
> 
>     Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff
> 
> In a web application, it is not reasonable to ask user to input yyyy-mm-dd hh:mm:ss.fffffffff.
> 
> Also, it doesn't support Locale.
> 
> Do you have experience getting java.util.Date populated in a Java Bean inside a FormBean?
> 
> 
> Thanks in advance.
> 
> Regards
> Yujun Liang
> yujun.liang@acm.org
> (0408) 467 448
> 
> 1. Form Bean Definition,
> <form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm">
> 
> <form-property name="mybean" type="com.mycompany.myproject.bean.MyBean"/>
> 
> </form-bean>
> 
> 2. Java Bean,
> 
> package com.mycompany.myproject.bean;
> 
> import java.io.Serializable;
> 
> import java.sql.Timestamp;
> 
> import org.apache.commons.lang.builder.EqualsBuilder;
> 
> import org.apache.commons.lang.builder.HashCodeBuilder;
> 
> import org.apache.commons.lang.builder.ToStringBuilder;
> 
> public abstract class MyBean implements Serializable {
> 
>     private long interchangeId;
> 
>     private Timestamp creationTime;
> 
>     private String originatingChannel;
> 
>     public long getInterchangeId() {
> 
>         return this.interchangeId;
> 
>     }
> 
>     public void setInterchangeId(long interchangeId) {
> 
>         this.interchangeId = interchangeId;
> 
>     }
> 
>     public Timestamp getCreationTime() {
> 
>         return this.creationTime;
> 
>     }
> 
>     
> 
>     public void setCreationTime(Timestamp creationTime) {
> 
>         this.creationTime = creationTime;
> 
>     }
> 
> 
>     public String getOriginatingChannel() {
> 
>         return this.originatingChannel;
> 
>     }
> 
>     public void setOriginatingChannel(String originatingChannel) {
> 
>         this.originatingChannel = originatingChannel;
> 
>     }
> 
> }
> 
> 3. HTML Form
> 
> <input name="mybean.creationTime" type="text" size="30" maxlength="30" value="25/10/2005">
> <input name="mybean.originatingChannel" type="text" size="40" maxlength="40" value="originating channel1">
> <input name="mybean.originatingChannelUserId" type="text" size="60" maxlength="60" value="originating_channel_userid1">
> 
> 4. Exception.
> 
> [28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet Error]-[BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff
> 
> at org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimestampConverter.java:117)
> 
> at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
> 
> at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
> 
> at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
> 
> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
> 
> at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
> 
> at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)
> 
> at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
> 
> at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
> 
> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
> 
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> 
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> 
> at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
> 
> at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
> 
> at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
> 
> at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
> 
> at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
> 
> at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
> 
> at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
> 


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


Re: java.util.Date in Action Form Bean

Posted by Murray Collingwood <mu...@focus-computing.com.au>.
Thanks Richard, I wasn't aware of this.
mc

On 28 Oct 2005 at 6:48, Richard Yee wrote:

> You should be aware that when using a java.text.DateFormat class or its 
> subclass as a static variable in a bean that can be accessed via 
> multiple threads simultaneously, you need to synchronize access to it 
> because DateFormat is not thread-safe. So you should change the code 
> below to:
> 
>     public static synchronized String date2String(Date date) {
>         return sdf.format(date);
>     }
> 
> or
> 
>     public static String date2String(Date date) {
> 	synchronized (date) {
> 	  return sdf.format(date);
> 	}
>     }
> 
> 
> 
> This is from the DateFormat Javadoc:Synchronization
> 
> Date formats are not synchronized. It is recommended to create separate 
> format instances for each thread. If multiple threads access a format 
> concurrently, it must be synchronized externally.
> 
> Regards,
> 
> Richard
> 
> Murray Collingwood wrote:
> > Hi 
> >
> > I add additional getters and setters for handling dates and prices (and other special 
> > numeric formats).  For example, in addition to the normal getter I code an additional 
> > one to retrieve the date as a string.
> >
> >     public Date getDateStart() {
> >         return dateStart;
> >     }
> >     public String getDateStartAsString() {
> >         if (dateStart == null) return "";
> >         return Focus.date2String(dateStart);
> >     }
> >
> >
> > I then code up some static methods to format dates to strings and similar methods 
to 
> > make the opposite translation (string back to date).  For example, a method from 
my 
> > Focus class related to the above code:
> >
> >     public static String date2String(Date date) {
> >         return sdf.format(date);
> >     }
> >
> > And if you're desparate, here is the sdf definition:
> >
> >     private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
> >
> > You can do the same thing with a Timestamp, depends how many fields you want 
to 
> > declare on your form and how much data entry you want your user to do.  I used a 
> > similar method in my Focus class to get/set the date portion of the timestamp value:
> >
> >     public static String timestamp2String(Timestamp ts) {
> >         if (ts == null) return "";
> >         return date2String(new Date(ts.getTime()));
> >     }
> >
> > HTH
> > mc
> >
> >
> > On 28 Oct 2005 at 14:31, Yujun Liang wrote:
> >
> >   
> >> Hello,
> >>
> >> I am working on a project using Struts. Struts uses BeanUtil to populate Action 
Form 
> >>     
> > Bean. This is such a nice framework to work with, except, 
> >   
> >> BeanUtil doesn't support java.util.Date conversion. But when I use 
> >>     
> > java.sql.Timestamp, it asks for the format of 
> >   
> >> yyyy-mm-dd hh:mm:ss.fffffffff
> >>
> >> Here is the exception when I enter other format,
> >>
> >>     Caused by: java.lang.IllegalArgumentException: Timestamp format must be 
yyyy-
> >>     
> > mm-dd hh:mm:ss.fffffffff
> >   
> >> In a web application, it is not reasonable to ask user to input yyyy-mm-dd 
> >>     
> > hh:mm:ss.fffffffff.
> >   
> >> Also, it doesn't support Locale.
> >>
> >> Do you have experience getting java.util.Date populated in a Java Bean inside a 
> >>     
> > FormBean?
> >   
> >> Thanks in advance.
> >>
> >> Regards
> >> Yujun Liang
> >> yujun.liang@acm.org
> >> (0408) 467 448
> >>
> >> 1. Form Bean Definition,
> >> <form-bean name="myForm" 
type="org.apache.struts.validator.DynaValidatorForm">
> >>
> >> <form-property name="mybean" 
type="com.mycompany.myproject.bean.MyBean"/>
> >>
> >> </form-bean>
> >>
> >> 2. Java Bean,
> >>
> >> package com.mycompany.myproject.bean;
> >>
> >> import java.io.Serializable;
> >>
> >> import java.sql.Timestamp;
> >>
> >> import org.apache.commons.lang.builder.EqualsBuilder;
> >>
> >> import org.apache.commons.lang.builder.HashCodeBuilder;
> >>
> >> import org.apache.commons.lang.builder.ToStringBuilder;
> >>
> >> public abstract class MyBean implements Serializable {
> >>
> >>     private long interchangeId;
> >>
> >>     private Timestamp creationTime;
> >>
> >>     private String originatingChannel;
> >>
> >>     public long getInterchangeId() {
> >>
> >>         return this.interchangeId;
> >>
> >>     }
> >>
> >>     public void setInterchangeId(long interchangeId) {
> >>
> >>         this.interchangeId = interchangeId;
> >>
> >>     }
> >>
> >>     public Timestamp getCreationTime() {
> >>
> >>         return this.creationTime;
> >>
> >>     }
> >>
> >>     
> >>
> >>     public void setCreationTime(Timestamp creationTime) {
> >>
> >>         this.creationTime = creationTime;
> >>
> >>     }
> >>
> >>
> >>     public String getOriginatingChannel() {
> >>
> >>         return this.originatingChannel;
> >>
> >>     }
> >>
> >>     public void setOriginatingChannel(String originatingChannel) {
> >>
> >>         this.originatingChannel = originatingChannel;
> >>
> >>     }
> >>
> >> }
> >>
> >> 3. HTML Form
> >>
> >> <input name="mybean.creationTime" type="text" size="30" maxlength="30" 
> >>     
> > value="25/10/2005">
> >   
> >> <input name="mybean.originatingChannel" type="text" size="40" maxlength="40" 
> >>     
> > value="originating channel1">
> >   
> >> <input name="mybean.originatingChannelUserId" type="text" size="60" 
> >>     
> > maxlength="60" value="originating_channel_userid1">
> >   
> >> 4. Exception.
> >>
> >> [28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet Error]-
> >>     
> > [BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: 
Timestamp 
> > format must be yyyy-mm-dd hh:mm:ss.fffffffff
> >   
> >> at 
> >>     
> > 
org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimesta
> > mpConverter.java:117)
> >   
> >> at 
> >>     
> > 
org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
> >   
> >> at 
> >>     
> > 
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
> >   
> >> at 
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
> >>
> >> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
> >>
> >> at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
> >>
> >> at 
> >>     
> > 
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:80
> > 5)
> >   
> >> at 
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
> >>
> >> at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
> >>
> >> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
> >>
> >> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> >>
> >> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> >>
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.
> > java:110)
> >   
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.j
> > ava:174)
> >   
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:3
> > 13)
> >   
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.ja
> > va:116)
> >   
> >> at 
> >>     
> > com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
> >   
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletRef
> > erenceState.java:42)
> >   
> >> at 
> >>     
> > 
com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceR
> > eference.java:40)
> >   
> >
> >
> >
> > FOCUS Computing
> > Mob: 0415 24 26 24
> > murray@focus-computing.com.au
> > http://www.focus-computing.com.au
> >
> >
> >
> >   
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> -- 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.362 / Virus Database: 267.12.5/150 - Release Date: 27/10/2005
> 



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.5/150 - Release Date: 27/10/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 Richard Yee <ry...@cruzio.com>.
You should be aware that when using a java.text.DateFormat class or its 
subclass as a static variable in a bean that can be accessed via 
multiple threads simultaneously, you need to synchronize access to it 
because DateFormat is not thread-safe. So you should change the code 
below to:

    public static synchronized String date2String(Date date) {
        return sdf.format(date);
    }

or

    public static String date2String(Date date) {
	synchronized (date) {
	  return sdf.format(date);
	}
    }



This is from the DateFormat Javadoc:Synchronization

Date formats are not synchronized. It is recommended to create separate 
format instances for each thread. If multiple threads access a format 
concurrently, it must be synchronized externally.

Regards,

Richard

Murray Collingwood wrote:
> Hi 
>
> I add additional getters and setters for handling dates and prices (and other special 
> numeric formats).  For example, in addition to the normal getter I code an additional 
> one to retrieve the date as a string.
>
>     public Date getDateStart() {
>         return dateStart;
>     }
>     public String getDateStartAsString() {
>         if (dateStart == null) return "";
>         return Focus.date2String(dateStart);
>     }
>
>
> I then code up some static methods to format dates to strings and similar methods to 
> make the opposite translation (string back to date).  For example, a method from my 
> Focus class related to the above code:
>
>     public static String date2String(Date date) {
>         return sdf.format(date);
>     }
>
> And if you're desparate, here is the sdf definition:
>
>     private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
>
> You can do the same thing with a Timestamp, depends how many fields you want to 
> declare on your form and how much data entry you want your user to do.  I used a 
> similar method in my Focus class to get/set the date portion of the timestamp value:
>
>     public static String timestamp2String(Timestamp ts) {
>         if (ts == null) return "";
>         return date2String(new Date(ts.getTime()));
>     }
>
> HTH
> mc
>
>
> On 28 Oct 2005 at 14:31, Yujun Liang wrote:
>
>   
>> Hello,
>>
>> I am working on a project using Struts. Struts uses BeanUtil to populate Action Form 
>>     
> Bean. This is such a nice framework to work with, except, 
>   
>> BeanUtil doesn't support java.util.Date conversion. But when I use 
>>     
> java.sql.Timestamp, it asks for the format of 
>   
>> yyyy-mm-dd hh:mm:ss.fffffffff
>>
>> Here is the exception when I enter other format,
>>
>>     Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-
>>     
> mm-dd hh:mm:ss.fffffffff
>   
>> In a web application, it is not reasonable to ask user to input yyyy-mm-dd 
>>     
> hh:mm:ss.fffffffff.
>   
>> Also, it doesn't support Locale.
>>
>> Do you have experience getting java.util.Date populated in a Java Bean inside a 
>>     
> FormBean?
>   
>> Thanks in advance.
>>
>> Regards
>> Yujun Liang
>> yujun.liang@acm.org
>> (0408) 467 448
>>
>> 1. Form Bean Definition,
>> <form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm">
>>
>> <form-property name="mybean" type="com.mycompany.myproject.bean.MyBean"/>
>>
>> </form-bean>
>>
>> 2. Java Bean,
>>
>> package com.mycompany.myproject.bean;
>>
>> import java.io.Serializable;
>>
>> import java.sql.Timestamp;
>>
>> import org.apache.commons.lang.builder.EqualsBuilder;
>>
>> import org.apache.commons.lang.builder.HashCodeBuilder;
>>
>> import org.apache.commons.lang.builder.ToStringBuilder;
>>
>> public abstract class MyBean implements Serializable {
>>
>>     private long interchangeId;
>>
>>     private Timestamp creationTime;
>>
>>     private String originatingChannel;
>>
>>     public long getInterchangeId() {
>>
>>         return this.interchangeId;
>>
>>     }
>>
>>     public void setInterchangeId(long interchangeId) {
>>
>>         this.interchangeId = interchangeId;
>>
>>     }
>>
>>     public Timestamp getCreationTime() {
>>
>>         return this.creationTime;
>>
>>     }
>>
>>     
>>
>>     public void setCreationTime(Timestamp creationTime) {
>>
>>         this.creationTime = creationTime;
>>
>>     }
>>
>>
>>     public String getOriginatingChannel() {
>>
>>         return this.originatingChannel;
>>
>>     }
>>
>>     public void setOriginatingChannel(String originatingChannel) {
>>
>>         this.originatingChannel = originatingChannel;
>>
>>     }
>>
>> }
>>
>> 3. HTML Form
>>
>> <input name="mybean.creationTime" type="text" size="30" maxlength="30" 
>>     
> value="25/10/2005">
>   
>> <input name="mybean.originatingChannel" type="text" size="40" maxlength="40" 
>>     
> value="originating channel1">
>   
>> <input name="mybean.originatingChannelUserId" type="text" size="60" 
>>     
> maxlength="60" value="originating_channel_userid1">
>   
>> 4. Exception.
>>
>> [28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet Error]-
>>     
> [BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: Timestamp 
> format must be yyyy-mm-dd hh:mm:ss.fffffffff
>   
>> at 
>>     
> org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimesta
> mpConverter.java:117)
>   
>> at 
>>     
> org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
>   
>> at 
>>     
> org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
>   
>> at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
>>
>> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
>>
>> at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
>>
>> at 
>>     
> org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:80
> 5)
>   
>> at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
>>
>> at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
>>
>> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
>>
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
>>
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>>
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.
> java:110)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.j
> ava:174)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:3
> 13)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.ja
> va:116)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletRef
> erenceState.java:42)
>   
>> at 
>>     
> com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceR
> eference.java:40)
>   
>
>
>
> FOCUS Computing
> Mob: 0415 24 26 24
> murray@focus-computing.com.au
> http://www.focus-computing.com.au
>
>
>
>   



---------------------------------------------------------------------
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>.
Murray,

Thanks for your reply. I know how to use other methods to handle it, such as 
putting String property on form bean and convert it to date inside mybean. I 
am asking whether it is possible for Struts to handle date conversion with 
Locale automatically, then I can only configure a single entry form bean for 
the action form, it can save a great deal of effort. Please compare the 
following configuration style,

Do you configure your action form like this,
<form-bean name="myForm" 
type="org.apache.struts.validator.DynaValidatorForm">

    <form-property name="iPod" type="com.mycompany.myproject.bean.MyBean"/>

</form-bean>

or like this,
<form-bean name="myForm" 
type="org.apache.struts.validator.DynaValidatorForm">

    <form-property name="aaId" type="java.lang.String"/>

    <form-property name="aaOperator" type="java.lang.String"/>

    <form-property name="product" type="java.lang.String"/>

    <form-property name="productCategory" type="java.lang.String"/>

    <form-property name="bbStatus" type="java.lang.String"/>

    <form-property name="valueDate" type="java.lang.String"/>

    <form-property name="valueDateOperator" type="java.lang.String"/>

    <form-property name="grossTotal" type="java.lang.String"/>

    <form-property name="grossTotalOperator" type="java.lang.String"/>

    <form-property name="itemCount" type="java.lang.String"/>

    <form-property name="itemCountOperator" type="java.lang.String"/>

    <form-property name="tab" type="java.lang.String"/>

    <form-property name="ccId" type="java.lang.String"/>

    <form-property name="ddId" type="java.lang.String"/>

    <form-property name="id" type="java.lang.String"/>

    <form-property name="idOperator" type="java.lang.String"/>

    <form-property name="bankId" type="java.lang.String"/>

    <form-property name="bankIdOperator" type="java.lang.String"/>

    <form-property name="accountNumber" type="java.lang.String"/>

    <form-property name="accountNumberOperator" type="java.lang.String"/>

    <form-property name="payeeName" type="java.lang.String"/>

    <form-property name="payeeNameOperator" type="java.lang.String"/>

    <form-property name="amount" type="java.lang.String"/>

    <form-property name="amountOperator" type="java.lang.String"/>

    <form-property name="amountType" type="java.lang.String"/>

    <form-property name="status" type="java.lang.String"/>

    <form-property name="eeDate" type="java.lang.String"/>

    <form-property name="rrDateOperator" type="java.lang.String"/>

    <form-property name="grossTotalOperator" type="java.lang.String"/>

    <form-property name="product" type="java.lang.String"/>

</form-bean>



Regards
Yujun Liang
yujun.liang@acm.org
(0408) 467 448
----- Original Message ----- 
From: "Murray Collingwood" <mu...@focus-computing.com.au>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Friday, October 28, 2005 2:44 PM
Subject: Re: java.util.Date in Action Form Bean


> Hi
>
> I add additional getters and setters for handling dates and prices (and 
> other special
> numeric formats).  For example, in addition to the normal getter I code an 
> additional
> one to retrieve the date as a string.
>
>    public Date getDateStart() {
>        return dateStart;
>    }
>    public String getDateStartAsString() {
>        if (dateStart == null) return "";
>        return Focus.date2String(dateStart);
>    }
>
>
> I then code up some static methods to format dates to strings and similar 
> methods to
> make the opposite translation (string back to date).  For example, a 
> method from my
> Focus class related to the above code:
>
>    public static String date2String(Date date) {
>        return sdf.format(date);
>    }
>
> And if you're desparate, here is the sdf definition:
>
>    private static SimpleDateFormat sdf = new 
> SimpleDateFormat("dd/MM/yyyy");
>
> You can do the same thing with a Timestamp, depends how many fields you 
> want to
> declare on your form and how much data entry you want your user to do.  I 
> used a
> similar method in my Focus class to get/set the date portion of the 
> timestamp value:
>
>    public static String timestamp2String(Timestamp ts) {
>        if (ts == null) return "";
>        return date2String(new Date(ts.getTime()));
>    }
>
> HTH
> mc
>
>
> On 28 Oct 2005 at 14:31, Yujun Liang wrote:
>
>> Hello,
>>
>> I am working on a project using Struts. Struts uses BeanUtil to populate 
>> Action Form
> Bean. This is such a nice framework to work with, except,
>>
>> BeanUtil doesn't support java.util.Date conversion. But when I use
> java.sql.Timestamp, it asks for the format of
>> yyyy-mm-dd hh:mm:ss.fffffffff
>>
>> Here is the exception when I enter other format,
>>
>>     Caused by: java.lang.IllegalArgumentException: Timestamp format must 
>> be yyyy-
> mm-dd hh:mm:ss.fffffffff
>>
>> In a web application, it is not reasonable to ask user to input 
>> yyyy-mm-dd
> hh:mm:ss.fffffffff.
>>
>> Also, it doesn't support Locale.
>>
>> Do you have experience getting java.util.Date populated in a Java Bean 
>> inside a
> FormBean?
>>
>>
>> Thanks in advance.
>>
>> Regards
>> Yujun Liang
>> yujun.liang@acm.org
>> (0408) 467 448
>>
>> 1. Form Bean Definition,
>> <form-bean name="myForm" 
>> type="org.apache.struts.validator.DynaValidatorForm">
>>
>> <form-property name="mybean" type="com.mycompany.myproject.bean.MyBean"/>
>>
>> </form-bean>
>>
>> 2. Java Bean,
>>
>> package com.mycompany.myproject.bean;
>>
>> import java.io.Serializable;
>>
>> import java.sql.Timestamp;
>>
>> import org.apache.commons.lang.builder.EqualsBuilder;
>>
>> import org.apache.commons.lang.builder.HashCodeBuilder;
>>
>> import org.apache.commons.lang.builder.ToStringBuilder;
>>
>> public abstract class MyBean implements Serializable {
>>
>>     private long interchangeId;
>>
>>     private Timestamp creationTime;
>>
>>     private String originatingChannel;
>>
>>     public long getInterchangeId() {
>>
>>         return this.interchangeId;
>>
>>     }
>>
>>     public void setInterchangeId(long interchangeId) {
>>
>>         this.interchangeId = interchangeId;
>>
>>     }
>>
>>     public Timestamp getCreationTime() {
>>
>>         return this.creationTime;
>>
>>     }
>>
>>
>>
>>     public void setCreationTime(Timestamp creationTime) {
>>
>>         this.creationTime = creationTime;
>>
>>     }
>>
>>
>>     public String getOriginatingChannel() {
>>
>>         return this.originatingChannel;
>>
>>     }
>>
>>     public void setOriginatingChannel(String originatingChannel) {
>>
>>         this.originatingChannel = originatingChannel;
>>
>>     }
>>
>> }
>>
>> 3. HTML Form
>>
>> <input name="mybean.creationTime" type="text" size="30" maxlength="30"
> value="25/10/2005">
>> <input name="mybean.originatingChannel" type="text" size="40" 
>> maxlength="40"
> value="originating channel1">
>> <input name="mybean.originatingChannelUserId" type="text" size="60"
> maxlength="60" value="originating_channel_userid1">
>>
>> 4. Exception.
>>
>> [28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet 
>> Error]-
> [BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: 
> Timestamp
> format must be yyyy-mm-dd hh:mm:ss.fffffffff
>>
>> at
> org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimesta
> mpConverter.java:117)
>>
>> at
> org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
>>
>> at
> org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
>>
>> at 
>> org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
>>
>> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
>>
>> at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
>>
>> at
> org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:80
> 5)
>>
>> at 
>> org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
>>
>> at 
>> org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
>>
>> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
>>
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
>>
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>>
>> at
> com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.
> java:110)
>>
>> at
> com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.j
> ava:174)
>>
>> at
> com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:3
> 13)
>>
>> at
> com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.ja
> va:116)
>>
>> at
> com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
>>
>> at
> com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletRef
> erenceState.java:42)
>>
>> at
> com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceR
> eference.java:40)
>>
>
>
>
> FOCUS Computing
> 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.5/150 - Release Date: 27/10/2005
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
> 

---------------------------------------------------------------------
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>.
Hi 

I add additional getters and setters for handling dates and prices (and other special 
numeric formats).  For example, in addition to the normal getter I code an additional 
one to retrieve the date as a string.

    public Date getDateStart() {
        return dateStart;
    }
    public String getDateStartAsString() {
        if (dateStart == null) return "";
        return Focus.date2String(dateStart);
    }


I then code up some static methods to format dates to strings and similar methods to 
make the opposite translation (string back to date).  For example, a method from my 
Focus class related to the above code:

    public static String date2String(Date date) {
        return sdf.format(date);
    }

And if you're desparate, here is the sdf definition:

    private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

You can do the same thing with a Timestamp, depends how many fields you want to 
declare on your form and how much data entry you want your user to do.  I used a 
similar method in my Focus class to get/set the date portion of the timestamp value:

    public static String timestamp2String(Timestamp ts) {
        if (ts == null) return "";
        return date2String(new Date(ts.getTime()));
    }

HTH
mc


On 28 Oct 2005 at 14:31, Yujun Liang wrote:

> Hello,
> 
> I am working on a project using Struts. Struts uses BeanUtil to populate Action Form 
Bean. This is such a nice framework to work with, except, 
> 
> BeanUtil doesn't support java.util.Date conversion. But when I use 
java.sql.Timestamp, it asks for the format of 
> yyyy-mm-dd hh:mm:ss.fffffffff
> 
> Here is the exception when I enter other format,
> 
>     Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-
mm-dd hh:mm:ss.fffffffff
> 
> In a web application, it is not reasonable to ask user to input yyyy-mm-dd 
hh:mm:ss.fffffffff.
> 
> Also, it doesn't support Locale.
> 
> Do you have experience getting java.util.Date populated in a Java Bean inside a 
FormBean?
> 
> 
> Thanks in advance.
> 
> Regards
> Yujun Liang
> yujun.liang@acm.org
> (0408) 467 448
> 
> 1. Form Bean Definition,
> <form-bean name="myForm" type="org.apache.struts.validator.DynaValidatorForm">
> 
> <form-property name="mybean" type="com.mycompany.myproject.bean.MyBean"/>
> 
> </form-bean>
> 
> 2. Java Bean,
> 
> package com.mycompany.myproject.bean;
> 
> import java.io.Serializable;
> 
> import java.sql.Timestamp;
> 
> import org.apache.commons.lang.builder.EqualsBuilder;
> 
> import org.apache.commons.lang.builder.HashCodeBuilder;
> 
> import org.apache.commons.lang.builder.ToStringBuilder;
> 
> public abstract class MyBean implements Serializable {
> 
>     private long interchangeId;
> 
>     private Timestamp creationTime;
> 
>     private String originatingChannel;
> 
>     public long getInterchangeId() {
> 
>         return this.interchangeId;
> 
>     }
> 
>     public void setInterchangeId(long interchangeId) {
> 
>         this.interchangeId = interchangeId;
> 
>     }
> 
>     public Timestamp getCreationTime() {
> 
>         return this.creationTime;
> 
>     }
> 
>     
> 
>     public void setCreationTime(Timestamp creationTime) {
> 
>         this.creationTime = creationTime;
> 
>     }
> 
> 
>     public String getOriginatingChannel() {
> 
>         return this.originatingChannel;
> 
>     }
> 
>     public void setOriginatingChannel(String originatingChannel) {
> 
>         this.originatingChannel = originatingChannel;
> 
>     }
> 
> }
> 
> 3. HTML Form
> 
> <input name="mybean.creationTime" type="text" size="30" maxlength="30" 
value="25/10/2005">
> <input name="mybean.originatingChannel" type="text" size="40" maxlength="40" 
value="originating channel1">
> <input name="mybean.originatingChannelUserId" type="text" size="60" 
maxlength="60" value="originating_channel_userid1">
> 
> 4. Exception.
> 
> [28/10/05 14:05:42:623 EST] 3f64a3f WebGroup E SRVE0026E: [Servlet Error]-
[BeanUtils.populate]: org.apache.commons.beanutils.ConversionException: Timestamp 
format must be yyyy-mm-dd hh:mm:ss.fffffffff
> 
> at 
org.apache.commons.beanutils.converters.SqlTimestampConverter.convert(SqlTimesta
mpConverter.java:117)
> 
> at 
org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
> 
> at 
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1004)
> 
> at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
> 
> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
> 
> at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
> 
> at 
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:80
5)
> 
> at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
> 
> at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
> 
> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
> 
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> 
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> 
> at 
com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.
java:110)
> 
> at 
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.j
ava:174)
> 
> at 
com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:3
13)
> 
> at 
com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.ja
va:116)
> 
> at 
com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
> 
> at 
com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletRef
erenceState.java:42)
> 
> at 
com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceR
eference.java:40)
> 



FOCUS Computing
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.5/150 - Release Date: 27/10/2005


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