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

Reply via email to