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
> [EMAIL PROTECTED]
> (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
[EMAIL PROTECTED]
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to