You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Jen <ji...@yahoo.com> on 2004/01/07 19:42:11 UTC

All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Hi, my program compiled OKay.  When I ran the
application, somehow, the properties of my
DynaValidatorForm are not passed from the action class
to the business tier and then to the data access tier.
 Please help me taking a look at what went wrong.   

It looks that the validation worked fine: 

<form-bean
    name="postForm"
          
type="org.apache.struts.validator.DynaValidatorForm">
       <form-property
            name="receiver"
            type="java.lang.String"/>
       <form-property
            name="sender"
            type="java.lang.String"/>
    .....
    .....
</form-bean>
because when I intentionally left several of the text
fields blank and submitted my JSP form, an applet got
prompted showing something in those fields are
required.

In my action class (see below),  I used BeanUtils to
copy all the properties of the DynaValidatorForm to a
bean (ThreadBean): 

DynaActionForm postForm = ( DynaActionForm )form;
ThreadBean threadBean = new ThreadBean();
BeanUtils.copyProperties( threadBean, postForm );

I then added a number of properties with pre-defined
values to the ThreadBean in the Action class:

Timestamp now = DateUtil.getCurrentGMTTimestamp();
threadBean.setThreadCreationDate( now );
threadBean.setThreadViewCount( 0 );

I then called a method in a service class
ThreadHandler and passing threadBean as the parameter
of that method:

ThreadHandler thandler = new ThreadHandler();
threadID = thandler.insertThread( threadBean );

In the ThreadHandler class (see below), I first tried
to retrieve the values of those properties from the
ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE NULL!

class ThreadHandler extends ThreadBean 
{
   int threadID = getThreadID();
   String receiver = getReceiver();
   String sender = getSender();
   Timestamp threadCreationDate =
getThreadCreationDate();
   int threadViewCount = getThreadViewCount();

   public ThreadHandler() {}

   MessageDAO md = new MySQLMessageDAO();

   public int insertThread( ThreadBean threadBean )
                            throws
MessageDAOSysException, ObjectNotFoundException
   {
      md.createThread( receiver, sender,   
                   threadCreationDate, threadViewCount
);

      .....
      .....

      return threadID;
   }
}





__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Robert Taylor <rt...@mulework.com>.
Sorry, I mean copyProperties().

You can find the differences defined below:

http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils
/BeanUtils.html#copyProperties(java.lang.Object, java.lang.Object)

http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils
/BeanUtils.html#populate(java.lang.Object, java.util.Map)


robert

> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Wednesday, January 07, 2004 2:13 PM
> To: Struts Users Mailing List
> Subject: RE: All The Bean Properties Are Null in the Business Tier!!!
> (Used BeanUtils to Convert DynaValidatorForm)
>
>
> What does BeanUtils.populate(); do?  I used
> BeanUtils.copyProperties();
> Does it make any difference?
>
> --- Robert Taylor <rt...@mulework.com> wrote:
> > Have you checked to make sure that the fields you
> > expect
> > actually exist in the form before calling
> > BeanUtils.populate()?
> >
> > If so, have you checked to see if the data was
> > copied properly
> > just after BeanUtils.populate()?
> >
> > BeanUtils.populate() uses reflection to copy
> > properties.
> > Are you usingproper JavaBeans naming convention?
> >
> > Try and isolate exactly where the data is getting
> > "lost".
> >
> > I use BeanUtils.populate() all the time with
> > DynaXXXXForms and
> > have no problems. Usually when I do, I find that my
> > data member
> > names don't correspond.
> >
> > robert
> >
> > > -----Original Message-----
> > > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > > Sent: Wednesday, January 07, 2004 1:42 PM
> > > To: struts-user@jakarta.apache.org
> > > Subject: All The Bean Properties Are Null in the
> > Business Tier!!! (Used
> > > BeanUtils to Convert DynaValidatorForm)
> > >
> > >
> > > Hi, my program compiled OKay.  When I ran the
> > > application, somehow, the properties of my
> > > DynaValidatorForm are not passed from the action
> > class
> > > to the business tier and then to the data access
> > tier.
> > >  Please help me taking a look at what went wrong.
> >
> > >
> > > It looks that the validation worked fine:
> > >
> > > <form-bean
> > >     name="postForm"
> > >
> > >
> >
> type="org.apache.struts.validator.DynaValidatorForm">
> > >        <form-property
> > >             name="receiver"
> > >             type="java.lang.String"/>
> > >        <form-property
> > >             name="sender"
> > >             type="java.lang.String"/>
> > >     .....
> > >     .....
> > > </form-bean>
> > > because when I intentionally left several of the
> > text
> > > fields blank and submitted my JSP form, an applet
> > got
> > > prompted showing something in those fields are
> > > required.
> > >
> > > In my action class (see below),  I used BeanUtils
> > to
> > > copy all the properties of the DynaValidatorForm
> > to a
> > > bean (ThreadBean):
> > >
> > > DynaActionForm postForm = ( DynaActionForm )form;
> > > ThreadBean threadBean = new ThreadBean();
> > > BeanUtils.copyProperties( threadBean, postForm );
> > >
> > > I then added a number of properties with
> > pre-defined
> > > values to the ThreadBean in the Action class:
> > >
> > > Timestamp now = DateUtil.getCurrentGMTTimestamp();
> > > threadBean.setThreadCreationDate( now );
> > > threadBean.setThreadViewCount( 0 );
> > >
> > > I then called a method in a service class
> > > ThreadHandler and passing threadBean as the
> > parameter
> > > of that method:
> > >
> > > ThreadHandler thandler = new ThreadHandler();
> > > threadID = thandler.insertThread( threadBean );
> > >
> > > In the ThreadHandler class (see below), I first
> > tried
> > > to retrieve the values of those properties from
> > the
> > > ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE
> > NULL!
> > >
> > > class ThreadHandler extends ThreadBean
> > > {
> > >    int threadID = getThreadID();
> > >    String receiver = getReceiver();
> > >    String sender = getSender();
> > >    Timestamp threadCreationDate =
> > > getThreadCreationDate();
> > >    int threadViewCount = getThreadViewCount();
> > >
> > >    public ThreadHandler() {}
> > >
> > >    MessageDAO md = new MySQLMessageDAO();
> > >
> > >    public int insertThread( ThreadBean threadBean
> > )
> > >                             throws
> > > MessageDAOSysException, ObjectNotFoundException
> > >    {
> > >       md.createThread( receiver, sender,
> > >                    threadCreationDate,
> > threadViewCount
> > > );
> > >
> > >       .....
> > >       .....
> > >
> > >       return threadID;
> > >    }
> > > }
> > >
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Do you Yahoo!?
> > > Yahoo! Hotjobs: Enter the "Signing Bonus"
> > Sweepstakes
> > > http://hotjobs.sweepstakes.yahoo.com/signingbonus
> > >
> > >
> >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> > >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> >
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
> http://hotjobs.sweepstakes.yahoo.com/signingbonus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Caroline Jen <ji...@yahoo.com>.
What does BeanUtils.populate(); do?  I used
BeanUtils.copyProperties();
Does it make any difference?

--- Robert Taylor <rt...@mulework.com> wrote:
> Have you checked to make sure that the fields you
> expect
> actually exist in the form before calling
> BeanUtils.populate()?
> 
> If so, have you checked to see if the data was
> copied properly 
> just after BeanUtils.populate()?
> 
> BeanUtils.populate() uses reflection to copy
> properties.
> Are you usingproper JavaBeans naming convention?
> 
> Try and isolate exactly where the data is getting
> "lost".
> 
> I use BeanUtils.populate() all the time with
> DynaXXXXForms and
> have no problems. Usually when I do, I find that my
> data member
> names don't correspond.
> 
> robert
> 
> > -----Original Message-----
> > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > Sent: Wednesday, January 07, 2004 1:42 PM
> > To: struts-user@jakarta.apache.org
> > Subject: All The Bean Properties Are Null in the
> Business Tier!!! (Used
> > BeanUtils to Convert DynaValidatorForm)
> > 
> > 
> > Hi, my program compiled OKay.  When I ran the
> > application, somehow, the properties of my
> > DynaValidatorForm are not passed from the action
> class
> > to the business tier and then to the data access
> tier.
> >  Please help me taking a look at what went wrong. 
>  
> > 
> > It looks that the validation worked fine: 
> > 
> > <form-bean
> >     name="postForm"
> >           
> >
>
type="org.apache.struts.validator.DynaValidatorForm">
> >        <form-property
> >             name="receiver"
> >             type="java.lang.String"/>
> >        <form-property
> >             name="sender"
> >             type="java.lang.String"/>
> >     .....
> >     .....
> > </form-bean>
> > because when I intentionally left several of the
> text
> > fields blank and submitted my JSP form, an applet
> got
> > prompted showing something in those fields are
> > required.
> > 
> > In my action class (see below),  I used BeanUtils
> to
> > copy all the properties of the DynaValidatorForm
> to a
> > bean (ThreadBean): 
> > 
> > DynaActionForm postForm = ( DynaActionForm )form;
> > ThreadBean threadBean = new ThreadBean();
> > BeanUtils.copyProperties( threadBean, postForm );
> > 
> > I then added a number of properties with
> pre-defined
> > values to the ThreadBean in the Action class:
> > 
> > Timestamp now = DateUtil.getCurrentGMTTimestamp();
> > threadBean.setThreadCreationDate( now );
> > threadBean.setThreadViewCount( 0 );
> > 
> > I then called a method in a service class
> > ThreadHandler and passing threadBean as the
> parameter
> > of that method:
> > 
> > ThreadHandler thandler = new ThreadHandler();
> > threadID = thandler.insertThread( threadBean );
> > 
> > In the ThreadHandler class (see below), I first
> tried
> > to retrieve the values of those properties from
> the
> > ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE
> NULL!
> > 
> > class ThreadHandler extends ThreadBean 
> > {
> >    int threadID = getThreadID();
> >    String receiver = getReceiver();
> >    String sender = getSender();
> >    Timestamp threadCreationDate =
> > getThreadCreationDate();
> >    int threadViewCount = getThreadViewCount();
> > 
> >    public ThreadHandler() {}
> > 
> >    MessageDAO md = new MySQLMessageDAO();
> > 
> >    public int insertThread( ThreadBean threadBean
> )
> >                             throws
> > MessageDAOSysException, ObjectNotFoundException
> >    {
> >       md.createThread( receiver, sender,   
> >                    threadCreationDate,
> threadViewCount
> > );
> > 
> >       .....
> >       .....
> > 
> >       return threadID;
> >    }
> > }
> > 
> > 
> > 
> > 
> > 
> > __________________________________
> > Do you Yahoo!?
> > Yahoo! Hotjobs: Enter the "Signing Bonus"
> Sweepstakes
> > http://hotjobs.sweepstakes.yahoo.com/signingbonus
> > 
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> > 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Robert Taylor <rt...@mulework.com>.
Glad it worked for you.

robert

> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Friday, January 09, 2004 5:12 PM
> To: Struts Users Mailing List
> Subject: RE: All The Bean Properties Are Null in the Business Tier!!!
> (Used BeanUtils to Convert DynaValidatorForm)
> 
> 
> Robert,  I followed your advices.  Your code works
> very well.  I have successfully inserted all the
> values of my bean properties into the database.  Thank
> you very much.
> --- Robert Taylor <rt...@mulework.com> wrote:
> > The same problem exists, you are calling getters on
> > a ThreadBean instance
> > which has not yet been populated.
> > 
> > 
> > Below three ways to accomplish your goal:
> > 
> > METHOD 1:
> > Try this (proxy):
> > 
> > public final class ThreadHandler {
> > 
> >     MessageDAO md = new MySQLMessageDAO();
> > 
> >     public int insertThread(ThreadBean bean)
> >         throws throws MessageDAOSysException,
> > ObjectNotFoundException {
> > 
> >       int identity = 0;
> > 
> >       md.createThread(bean.getReceiver(),
> >                       bean.getSender(),
> >                       bean.getThreadTopic(),
> >                       bean.getThreadBody(),
> >                       bean.getThreadCreationDate(),
> >                       bean.getThreadViewCount(),
> >                       bean.getThreadReplyCount());
> > 
> >       // get identity
> > 
> >       return identity;
> > 
> >     }
> > 
> > 
> > }
> > 
> > ThreadBean bean = new ThreadBean();
> > BeanUtils.copyProperties(bean, postForm);
> > ThreadHandler th = new ThreadHandler()
> > th.insertThread(bean);
> > 
> > If you do it this way, then it would be better if
> > ThreadHandler was a
> > singleton,
> > but that's a different discussion :)
> > 
> > 
> > 
> > 
> > METHOD 2:
> > Another way to do it (design by composition).
> > 
> > public final class ThreadHandler {
> > 
> >     MessageDAO md = new MySQLMessageDAO();
> >     ThreadBean bean;
> > 
> >     public ThreadHandler(ThreadBean bean) {
> > 
> >        this.bean = bean;
> > 
> >     }
> >     public int insertThread()
> >         throws throws MessageDAOSysException,
> > ObjectNotFoundException {
> > 
> >       int identity = 0;
> > 
> >       md.createThread(bean.getReceiver(),
> >                       bean.getSender(),
> >                       bean.getThreadTopic(),
> >                       bean.getThreadBody(),
> >                       bean.getThreadCreationDate(),
> >                       bean.getThreadViewCount(),
> >                       bean.getThreadReplyCount());
> > 
> >       // get identity
> > 
> >       return identity;
> > 
> >     }
> > 
> > 
> > }
> > 
> > ThreadBean bean = new ThreadBean();
> > BeanUtils.copyProperties(bean, postForm);
> > ThreadHandler th = new ThreadHandler(bean)
> > th.insertThread();
> > 
> > 
> > METHOD 3:
> > Yet another way via classic inheritance:
> > 
> > public final class ThreadHandler
> > 	extends ThreadBean {
> > 
> >     MessageDAO md = new MySQLMessageDAO();
> > 
> > 
> >     public ThreadHandler() {
> > 
> >       super();
> > 
> >     }
> >     public int insertThread()
> >         throws throws MessageDAOSysException,
> > ObjectNotFoundException {
> > 
> >       int identity = 0;
> > 
> >       md.createThread(this.getReceiver(),
> >                       this.getSender(),
> >                       this.getThreadTopic(),
> >                       this.getThreadBody(),
> >                       this.getThreadCreationDate(),
> >                       this.getThreadViewCount(),
> >                       this.getThreadReplyCount());
> > 
> >       // get identity
> > 
> >       return identity;
> > 
> >     }
> > 
> > 
> > }
> > 
> > ThreadHandler th = new ThreadHandler();
> > BeanUtils.copyProperties(th, postForm);
> > th.insertThread();
> > 
> > 
> > 
> > hth,
> > 
> > robert
> > 
> > 
> > > -----Original Message-----
> > > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > > Sent: Thursday, January 08, 2004 11:48 AM
> > > To: Struts Users Mailing List
> > > Subject: RE: All The Bean Properties Are Null in
> > the Business Tier!!!
> > > (Used BeanUtils to Convert DynaValidatorForm)
> > >
> > >
> > > Thank you for your comment, which is very helpful.
> > >
> > > Instead of extends ThreadBean, I now import the
> > > ThreadBean into my ThreadHandler class (see the
> > code
> > > below). I am still getting all null or zero values
> > > from the bean.
> > >
> > > What is the proper way to do it?  I simply want to
> > > insert the value of all the properties into the
> > > database.  Most of the fields in my database
> > require
> > > NOT NULL.
> > >
> > > code:
> > > ==================================================
> > > import org.apache.artimus.message.ThreadBean;
> > >
> > > class ThreadHandler
> > > {
> > >    ThreadBean threadBean = new ThreadBean();
> > >
> > >    String receiver = threadBean.getReceiver();
> > >    String sender = threadBean.getSender();
> > >    String threadTopic = threadBean.getPostTopic();
> > >    String threadBody = threadBean.getPostBody();
> > >    Timestamp threadCreationDate =
> > > threadBean.getThreadCreationDate();
> > >    int threadViewCount =
> > > threadBean.getThreadViewCount();
> > >    int threadReplyCount =
> > > threadBean.getThreadReplyCount();
> > >
> > >    MessageDAO md = new MySQLMessageDAO();
> > >    public int insertThread( ThreadBean threadBean
> > )
> > >                         throws
> > MessageDAOSysException,
> > >
> > >                               
> > ObjectNotFoundException
> > >
> > >    {
> > >       System.out.println( "The sender is " +
> > sender +
> > > "." );
> > >       System.out.println( "The subject is " +
> > > threadTopic + "." );
> > >       System.out.println( "The creation date is "
> > +
> > > threadCreationDate );
> > >       System.out.println( "The number of replies
> > are 
> === message truncated ===
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
> http://hotjobs.sweepstakes.yahoo.com/signingbonus
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Caroline Jen <ji...@yahoo.com>.
Robert,  I followed your advices.  Your code works
very well.  I have successfully inserted all the
values of my bean properties into the database.  Thank
you very much.
--- Robert Taylor <rt...@mulework.com> wrote:
> The same problem exists, you are calling getters on
> a ThreadBean instance
> which has not yet been populated.
> 
> 
> Below three ways to accomplish your goal:
> 
> METHOD 1:
> Try this (proxy):
> 
> public final class ThreadHandler {
> 
>     MessageDAO md = new MySQLMessageDAO();
> 
>     public int insertThread(ThreadBean bean)
>         throws throws MessageDAOSysException,
> ObjectNotFoundException {
> 
>       int identity = 0;
> 
>       md.createThread(bean.getReceiver(),
>                       bean.getSender(),
>                       bean.getThreadTopic(),
>                       bean.getThreadBody(),
>                       bean.getThreadCreationDate(),
>                       bean.getThreadViewCount(),
>                       bean.getThreadReplyCount());
> 
>       // get identity
> 
>       return identity;
> 
>     }
> 
> 
> }
> 
> ThreadBean bean = new ThreadBean();
> BeanUtils.copyProperties(bean, postForm);
> ThreadHandler th = new ThreadHandler()
> th.insertThread(bean);
> 
> If you do it this way, then it would be better if
> ThreadHandler was a
> singleton,
> but that's a different discussion :)
> 
> 
> 
> 
> METHOD 2:
> Another way to do it (design by composition).
> 
> public final class ThreadHandler {
> 
>     MessageDAO md = new MySQLMessageDAO();
>     ThreadBean bean;
> 
>     public ThreadHandler(ThreadBean bean) {
> 
>        this.bean = bean;
> 
>     }
>     public int insertThread()
>         throws throws MessageDAOSysException,
> ObjectNotFoundException {
> 
>       int identity = 0;
> 
>       md.createThread(bean.getReceiver(),
>                       bean.getSender(),
>                       bean.getThreadTopic(),
>                       bean.getThreadBody(),
>                       bean.getThreadCreationDate(),
>                       bean.getThreadViewCount(),
>                       bean.getThreadReplyCount());
> 
>       // get identity
> 
>       return identity;
> 
>     }
> 
> 
> }
> 
> ThreadBean bean = new ThreadBean();
> BeanUtils.copyProperties(bean, postForm);
> ThreadHandler th = new ThreadHandler(bean)
> th.insertThread();
> 
> 
> METHOD 3:
> Yet another way via classic inheritance:
> 
> public final class ThreadHandler
> 	extends ThreadBean {
> 
>     MessageDAO md = new MySQLMessageDAO();
> 
> 
>     public ThreadHandler() {
> 
>       super();
> 
>     }
>     public int insertThread()
>         throws throws MessageDAOSysException,
> ObjectNotFoundException {
> 
>       int identity = 0;
> 
>       md.createThread(this.getReceiver(),
>                       this.getSender(),
>                       this.getThreadTopic(),
>                       this.getThreadBody(),
>                       this.getThreadCreationDate(),
>                       this.getThreadViewCount(),
>                       this.getThreadReplyCount());
> 
>       // get identity
> 
>       return identity;
> 
>     }
> 
> 
> }
> 
> ThreadHandler th = new ThreadHandler();
> BeanUtils.copyProperties(th, postForm);
> th.insertThread();
> 
> 
> 
> hth,
> 
> robert
> 
> 
> > -----Original Message-----
> > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > Sent: Thursday, January 08, 2004 11:48 AM
> > To: Struts Users Mailing List
> > Subject: RE: All The Bean Properties Are Null in
> the Business Tier!!!
> > (Used BeanUtils to Convert DynaValidatorForm)
> >
> >
> > Thank you for your comment, which is very helpful.
> >
> > Instead of extends ThreadBean, I now import the
> > ThreadBean into my ThreadHandler class (see the
> code
> > below). I am still getting all null or zero values
> > from the bean.
> >
> > What is the proper way to do it?  I simply want to
> > insert the value of all the properties into the
> > database.  Most of the fields in my database
> require
> > NOT NULL.
> >
> > code:
> > ==================================================
> > import org.apache.artimus.message.ThreadBean;
> >
> > class ThreadHandler
> > {
> >    ThreadBean threadBean = new ThreadBean();
> >
> >    String receiver = threadBean.getReceiver();
> >    String sender = threadBean.getSender();
> >    String threadTopic = threadBean.getPostTopic();
> >    String threadBody = threadBean.getPostBody();
> >    Timestamp threadCreationDate =
> > threadBean.getThreadCreationDate();
> >    int threadViewCount =
> > threadBean.getThreadViewCount();
> >    int threadReplyCount =
> > threadBean.getThreadReplyCount();
> >
> >    MessageDAO md = new MySQLMessageDAO();
> >    public int insertThread( ThreadBean threadBean
> )
> >                         throws
> MessageDAOSysException,
> >
> >                               
> ObjectNotFoundException
> >
> >    {
> >       System.out.println( "The sender is " +
> sender +
> > "." );
> >       System.out.println( "The subject is " +
> > threadTopic + "." );
> >       System.out.println( "The creation date is "
> +
> > threadCreationDate );
> >       System.out.println( "The number of replies
> are 
=== message truncated ===


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Robert Taylor <rt...@mulework.com>.
The same problem exists, you are calling getters on a ThreadBean instance
which has not yet been populated.


Below three ways to accomplish your goal:

METHOD 1:
Try this (proxy):

public final class ThreadHandler {

    MessageDAO md = new MySQLMessageDAO();

    public int insertThread(ThreadBean bean)
        throws throws MessageDAOSysException, ObjectNotFoundException {

      int identity = 0;

      md.createThread(bean.getReceiver(),
                      bean.getSender(),
                      bean.getThreadTopic(),
                      bean.getThreadBody(),
                      bean.getThreadCreationDate(),
                      bean.getThreadViewCount(),
                      bean.getThreadReplyCount());

      // get identity

      return identity;

    }


}

ThreadBean bean = new ThreadBean();
BeanUtils.copyProperties(bean, postForm);
ThreadHandler th = new ThreadHandler()
th.insertThread(bean);

If you do it this way, then it would be better if ThreadHandler was a
singleton,
but that's a different discussion :)




METHOD 2:
Another way to do it (design by composition).

public final class ThreadHandler {

    MessageDAO md = new MySQLMessageDAO();
    ThreadBean bean;

    public ThreadHandler(ThreadBean bean) {

       this.bean = bean;

    }
    public int insertThread()
        throws throws MessageDAOSysException, ObjectNotFoundException {

      int identity = 0;

      md.createThread(bean.getReceiver(),
                      bean.getSender(),
                      bean.getThreadTopic(),
                      bean.getThreadBody(),
                      bean.getThreadCreationDate(),
                      bean.getThreadViewCount(),
                      bean.getThreadReplyCount());

      // get identity

      return identity;

    }


}

ThreadBean bean = new ThreadBean();
BeanUtils.copyProperties(bean, postForm);
ThreadHandler th = new ThreadHandler(bean)
th.insertThread();


METHOD 3:
Yet another way via classic inheritance:

public final class ThreadHandler
	extends ThreadBean {

    MessageDAO md = new MySQLMessageDAO();


    public ThreadHandler() {

      super();

    }
    public int insertThread()
        throws throws MessageDAOSysException, ObjectNotFoundException {

      int identity = 0;

      md.createThread(this.getReceiver(),
                      this.getSender(),
                      this.getThreadTopic(),
                      this.getThreadBody(),
                      this.getThreadCreationDate(),
                      this.getThreadViewCount(),
                      this.getThreadReplyCount());

      // get identity

      return identity;

    }


}

ThreadHandler th = new ThreadHandler();
BeanUtils.copyProperties(th, postForm);
th.insertThread();



hth,

robert


> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Thursday, January 08, 2004 11:48 AM
> To: Struts Users Mailing List
> Subject: RE: All The Bean Properties Are Null in the Business Tier!!!
> (Used BeanUtils to Convert DynaValidatorForm)
>
>
> Thank you for your comment, which is very helpful.
>
> Instead of extends ThreadBean, I now import the
> ThreadBean into my ThreadHandler class (see the code
> below). I am still getting all null or zero values
> from the bean.
>
> What is the proper way to do it?  I simply want to
> insert the value of all the properties into the
> database.  Most of the fields in my database require
> NOT NULL.
>
> code:
> ==================================================
> import org.apache.artimus.message.ThreadBean;
>
> class ThreadHandler
> {
>    ThreadBean threadBean = new ThreadBean();
>
>    String receiver = threadBean.getReceiver();
>    String sender = threadBean.getSender();
>    String threadTopic = threadBean.getPostTopic();
>    String threadBody = threadBean.getPostBody();
>    Timestamp threadCreationDate =
> threadBean.getThreadCreationDate();
>    int threadViewCount =
> threadBean.getThreadViewCount();
>    int threadReplyCount =
> threadBean.getThreadReplyCount();
>
>    MessageDAO md = new MySQLMessageDAO();
>    public int insertThread( ThreadBean threadBean )
>                         throws MessageDAOSysException,
>
>                                ObjectNotFoundException
>
>    {
>       System.out.println( "The sender is " + sender +
> "." );
>       System.out.println( "The subject is " +
> threadTopic + "." );
>       System.out.println( "The creation date is " +
> threadCreationDate );
>       System.out.println( "The number of replies are "
> + threadReplyCount );
>
>       md.createThread( receiver, sender, threadTopic,
>                        threadBody, threadCreationDate,
>
>                        threadViewCount,
>                        threadReplyCount );
>
>       int threadID = 0;
>       .....
>       .....
>       return threadID;
>    }
> }
>
>
> --- Robert Taylor <rt...@mulework.com> wrote:
> > Your code seems a bit confusing based upon what you
> > want to
> > achieve.
> >
> > If indeed you want ThreadHandler to inherit from
> > ThreadBean,
> > you should be able to do something like this:
> >
> > DynaActionForm postForm = ( DynaActionForm )form;
> > ThreadHander = new ThreadHandler();
> > BeanUtils.copyProperties( threadHandler, postForm );
> >
> > threadHandler.insertThread();
> >
> >
> > The reason you are getting null and zero values in
> > ThreadHandler
> > is that you are populating a new instance of
> > ThreadBean  here:
> >
> > > > > DynaActionForm postForm = ( DynaActionForm
> > )form;
> > > > > ThreadBean threadBean = new ThreadBean();
> > > > > BeanUtils.copyProperties( threadBean, postForm
> > );
> >
> > ... and then you get a new instance of ThreadHandler
> > here:
> >
> > > > > ThreadHandler thandler = new ThreadHandler();
> >
> > At this point, ThreadHandler knows nothing of your
> > populated
> > instance of ThreadBean. Instead it is getting values
> > from
> > its own "empty" ThreadBean parent here:
> >
> > > class ThreadHandler extends ThreadBean
> > > {
> > >    String receiver = getReceiver();
> > >    String sender = getSender();
> > >    String threadTopic = getPostTopic();
> > >    String threadBody = getPostBody();
> > >    Timestamp threadCreationDate =
> > > getThreadCreationDate();
> > >    int threadViewCount = getThreadViewCount();
> > >    int threadReplyCount = getThreadReplyCount();
> > >
> > >    .....
> > >    .....
> > > }
> >
> >
> > ...unless you do something like the following in
> > ThreadHandler.insertThread():
> >
> > public int insertThread(ThreadBean bean) throws
> > SomeException {
> >
> >       this.receiver = bean.getReceiver();
> >       this.sender = bean.getSender();
> >       ....
> >
> >       // insert code here
> >
> >
> >
> > }
> >
> > which from a design perspective, I'm not sure why
> > you would want to do it
> > this way.
> >
> >
> > hth,
> >
> > robert
> >
> >
> > > -----Original Message-----
> > > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > > Sent: Wednesday, January 07, 2004 10:27 PM
> > > To: Struts Users Mailing List
> > > Subject: RE: All The Bean Properties Are Null in
> > the Business Tier!!!
> > > (Used BeanUtils to Convert DynaValidatorForm)
> > >
> > >
> > > I have narrowed down the problem.  Something went
> > > wrong in my business tier class
> > (ThreadHandler.java).
> > > All the properties in the JavaBean are populated
> > > properly (I have checked).  The way I coded in the
> > > ThreadHandler class failed to retrieve the value
> > of
> > > all the properties in the JavaBean
> > (ThreadBean.java).
> > > All the values turned out to be null or 0.  Now,
> > what
> > > is wrong with the code?
> > >
> > > class ThreadHandler extends ThreadBean
> > > {
> > >    String receiver = getReceiver();
> > >    String sender = getSender();
> > >    String threadTopic = getPostTopic();
> > >    String threadBody = getPostBody();
> > >    Timestamp threadCreationDate =
> > > getThreadCreationDate();
> > >    int threadViewCount = getThreadViewCount();
> > >    int threadReplyCount = getThreadReplyCount();
> > >
> > >    .....
> > >    .....
> > > }
> > >
> > > To answer your question:
> > >
> > > 1. Have you checked to make sure that the fields
> > you
> > > expect actually exist in the form before calling
> > > BeanUtils.copyProperties()?
> > >
> > > Yes, I have printed out the contents of the JSP
> > text
> > > fields in my action class, which is a servlet.
> > For
> > > example: System.out.println( "the sender is " +
> > > postForm.get( "sender" ) );
> > >
> > > 2. If so, have you checked to see if the data was
> > > copied properly just after
> > BeanUtils.copyProperties()?
> > >
> > > Yes, I have also printed out the value of the bean
> > > properties.  For example: System.out.println( "the
> > > sender is " + threadBean.getSender() );
> > >
> > > 3. BeanUtils.copyProperties() uses reflection to
> > copy
> > > properties.  Are you using proper JavaBeans naming
> > > convention?
> > >
> > > Yes, I used a pair of get and set methods for each
> > > properties in the JavaBean (ThreadBean.java).  And
> > in
> > > my action class, I could print out the value of
> > those
> > > properties.
> > >
> > > --- Robert Taylor <rt...@mulework.com> wrote:
> > > > Have you checked to make sure that the fields
> > you
> > > > expect
> > > > actually exist in the form before calling
> > > > BeanUtils.populate()?
> > > >
> > > > If so, have you checked to see if the data was
> > > > copied properly
> > > > just after BeanUtils.populate()?
> > > >
> > > > BeanUtils.populate() uses reflection to copy
> > > > properties.
> > > > Are you usingproper JavaBeans naming convention?
> > > >
> > > > Try and isolate exactly where the data is
> > getting
> > > > "lost".
> > > >
> > > > I use BeanUtils.populate() all the time with
> > > > DynaXXXXForms and
> > > > have no problems. Usually when I do, I find that
> > my
> > > > data member
> > > > names don't correspond.
> > > >
> > > > robert
> > > >
> > > > > -----Original Message-----
> > > > > From: Caroline Jen
> > [mailto:jiapei_jen@yahoo.com]
> > > > > Sent: Wednesday, January 07, 2004 1:42 PM
> > > > > To: struts-user@jakarta.apache.org
> > > > > Subject: All The Bean Properties Are Null in
> > the
> > > > Business Tier!!! (Used
> > > > > BeanUtils to Convert DynaValidatorForm)
> > > > >
> > > > >
> > > > > Hi, my program compiled OKay.  When I ran the
> > > > > application, somehow, the properties of my
> > > > > DynaValidatorForm are not passed from the
> > action
> > > > class
> > > > > to the business tier and then to the data
> > access
> > > > tier.
> > > > >  Please help me taking a look at what went
> > wrong.
> > > >
> > > > >
> >
> === message truncated ===
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
> http://hotjobs.sweepstakes.yahoo.com/signingbonus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Caroline Jen <ji...@yahoo.com>.
Thank you for your comment, which is very helpful.

Instead of extends ThreadBean, I now import the
ThreadBean into my ThreadHandler class (see the code
below). I am still getting all null or zero values
from the bean.

What is the proper way to do it?  I simply want to
insert the value of all the properties into the
database.  Most of the fields in my database require
NOT NULL.  

code:
==================================================
import org.apache.artimus.message.ThreadBean;

class ThreadHandler 
{
   ThreadBean threadBean = new ThreadBean();  

   String receiver = threadBean.getReceiver();
   String sender = threadBean.getSender();
   String threadTopic = threadBean.getPostTopic();
   String threadBody = threadBean.getPostBody();
   Timestamp threadCreationDate =
threadBean.getThreadCreationDate();
   int threadViewCount =
threadBean.getThreadViewCount();
   int threadReplyCount =
threadBean.getThreadReplyCount();

   MessageDAO md = new MySQLMessageDAO();
   public int insertThread( ThreadBean threadBean )
                        throws MessageDAOSysException,

                               ObjectNotFoundException

   {
      System.out.println( "The sender is " + sender +
"." );  
      System.out.println( "The subject is " +
threadTopic + "." );
      System.out.println( "The creation date is " +
threadCreationDate );
      System.out.println( "The number of replies are "
+ threadReplyCount );

      md.createThread( receiver, sender, threadTopic, 
                       threadBody, threadCreationDate,

                       threadViewCount,   
                       threadReplyCount ); 

      int threadID = 0;
      ..... 
      .....
      return threadID;
   }
} 


--- Robert Taylor <rt...@mulework.com> wrote:
> Your code seems a bit confusing based upon what you
> want to
> achieve.
> 
> If indeed you want ThreadHandler to inherit from
> ThreadBean,
> you should be able to do something like this:
> 
> DynaActionForm postForm = ( DynaActionForm )form;
> ThreadHander = new ThreadHandler();
> BeanUtils.copyProperties( threadHandler, postForm );
> 
> threadHandler.insertThread();
> 
> 
> The reason you are getting null and zero values in
> ThreadHandler
> is that you are populating a new instance of
> ThreadBean  here:
> 
> > > > DynaActionForm postForm = ( DynaActionForm
> )form;
> > > > ThreadBean threadBean = new ThreadBean();
> > > > BeanUtils.copyProperties( threadBean, postForm
> );
> 
> ... and then you get a new instance of ThreadHandler
> here:
> 
> > > > ThreadHandler thandler = new ThreadHandler();
> 
> At this point, ThreadHandler knows nothing of your
> populated
> instance of ThreadBean. Instead it is getting values
> from
> its own "empty" ThreadBean parent here:
> 
> > class ThreadHandler extends ThreadBean
> > {
> >    String receiver = getReceiver();
> >    String sender = getSender();
> >    String threadTopic = getPostTopic();
> >    String threadBody = getPostBody();
> >    Timestamp threadCreationDate =
> > getThreadCreationDate();
> >    int threadViewCount = getThreadViewCount();
> >    int threadReplyCount = getThreadReplyCount();
> >
> >    .....
> >    .....
> > }
> 
> 
> ...unless you do something like the following in
> ThreadHandler.insertThread():
> 
> public int insertThread(ThreadBean bean) throws
> SomeException {
> 
>       this.receiver = bean.getReceiver();
>       this.sender = bean.getSender();
>       ....
> 
>       // insert code here
> 
> 
> 
> }
> 
> which from a design perspective, I'm not sure why
> you would want to do it
> this way.
> 
> 
> hth,
> 
> robert
> 
> 
> > -----Original Message-----
> > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > Sent: Wednesday, January 07, 2004 10:27 PM
> > To: Struts Users Mailing List
> > Subject: RE: All The Bean Properties Are Null in
> the Business Tier!!!
> > (Used BeanUtils to Convert DynaValidatorForm)
> >
> >
> > I have narrowed down the problem.  Something went
> > wrong in my business tier class
> (ThreadHandler.java).
> > All the properties in the JavaBean are populated
> > properly (I have checked).  The way I coded in the
> > ThreadHandler class failed to retrieve the value
> of
> > all the properties in the JavaBean
> (ThreadBean.java).
> > All the values turned out to be null or 0.  Now,
> what
> > is wrong with the code?
> >
> > class ThreadHandler extends ThreadBean
> > {
> >    String receiver = getReceiver();
> >    String sender = getSender();
> >    String threadTopic = getPostTopic();
> >    String threadBody = getPostBody();
> >    Timestamp threadCreationDate =
> > getThreadCreationDate();
> >    int threadViewCount = getThreadViewCount();
> >    int threadReplyCount = getThreadReplyCount();
> >
> >    .....
> >    .....
> > }
> >
> > To answer your question:
> >
> > 1. Have you checked to make sure that the fields
> you
> > expect actually exist in the form before calling
> > BeanUtils.copyProperties()?
> >
> > Yes, I have printed out the contents of the JSP
> text
> > fields in my action class, which is a servlet. 
> For
> > example: System.out.println( "the sender is " +
> > postForm.get( "sender" ) );
> >
> > 2. If so, have you checked to see if the data was
> > copied properly just after
> BeanUtils.copyProperties()?
> >
> > Yes, I have also printed out the value of the bean
> > properties.  For example: System.out.println( "the
> > sender is " + threadBean.getSender() );
> >
> > 3. BeanUtils.copyProperties() uses reflection to
> copy
> > properties.  Are you using proper JavaBeans naming
> > convention?
> >
> > Yes, I used a pair of get and set methods for each
> > properties in the JavaBean (ThreadBean.java).  And
> in
> > my action class, I could print out the value of
> those
> > properties.
> >
> > --- Robert Taylor <rt...@mulework.com> wrote:
> > > Have you checked to make sure that the fields
> you
> > > expect
> > > actually exist in the form before calling
> > > BeanUtils.populate()?
> > >
> > > If so, have you checked to see if the data was
> > > copied properly
> > > just after BeanUtils.populate()?
> > >
> > > BeanUtils.populate() uses reflection to copy
> > > properties.
> > > Are you usingproper JavaBeans naming convention?
> > >
> > > Try and isolate exactly where the data is
> getting
> > > "lost".
> > >
> > > I use BeanUtils.populate() all the time with
> > > DynaXXXXForms and
> > > have no problems. Usually when I do, I find that
> my
> > > data member
> > > names don't correspond.
> > >
> > > robert
> > >
> > > > -----Original Message-----
> > > > From: Caroline Jen
> [mailto:jiapei_jen@yahoo.com]
> > > > Sent: Wednesday, January 07, 2004 1:42 PM
> > > > To: struts-user@jakarta.apache.org
> > > > Subject: All The Bean Properties Are Null in
> the
> > > Business Tier!!! (Used
> > > > BeanUtils to Convert DynaValidatorForm)
> > > >
> > > >
> > > > Hi, my program compiled OKay.  When I ran the
> > > > application, somehow, the properties of my
> > > > DynaValidatorForm are not passed from the
> action
> > > class
> > > > to the business tier and then to the data
> access
> > > tier.
> > > >  Please help me taking a look at what went
> wrong.
> > >
> > > >
> 
=== message truncated ===


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Robert Taylor <rt...@mulework.com>.
Your code seems a bit confusing based upon what you want to
achieve.

If indeed you want ThreadHandler to inherit from ThreadBean,
you should be able to do something like this:

DynaActionForm postForm = ( DynaActionForm )form;
ThreadHander = new ThreadHandler();
BeanUtils.copyProperties( threadHandler, postForm );

threadHandler.insertThread();


The reason you are getting null and zero values in ThreadHandler
is that you are populating a new instance of ThreadBean  here:

> > > DynaActionForm postForm = ( DynaActionForm )form;
> > > ThreadBean threadBean = new ThreadBean();
> > > BeanUtils.copyProperties( threadBean, postForm );

... and then you get a new instance of ThreadHandler here:

> > > ThreadHandler thandler = new ThreadHandler();

At this point, ThreadHandler knows nothing of your populated
instance of ThreadBean. Instead it is getting values from
its own "empty" ThreadBean parent here:

> class ThreadHandler extends ThreadBean
> {
>    String receiver = getReceiver();
>    String sender = getSender();
>    String threadTopic = getPostTopic();
>    String threadBody = getPostBody();
>    Timestamp threadCreationDate =
> getThreadCreationDate();
>    int threadViewCount = getThreadViewCount();
>    int threadReplyCount = getThreadReplyCount();
>
>    .....
>    .....
> }


...unless you do something like the following in
ThreadHandler.insertThread():

public int insertThread(ThreadBean bean) throws SomeException {

      this.receiver = bean.getReceiver();
      this.sender = bean.getSender();
      ....

      // insert code here



}

which from a design perspective, I'm not sure why you would want to do it
this way.


hth,

robert


> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Wednesday, January 07, 2004 10:27 PM
> To: Struts Users Mailing List
> Subject: RE: All The Bean Properties Are Null in the Business Tier!!!
> (Used BeanUtils to Convert DynaValidatorForm)
>
>
> I have narrowed down the problem.  Something went
> wrong in my business tier class (ThreadHandler.java).
> All the properties in the JavaBean are populated
> properly (I have checked).  The way I coded in the
> ThreadHandler class failed to retrieve the value of
> all the properties in the JavaBean (ThreadBean.java).
> All the values turned out to be null or 0.  Now, what
> is wrong with the code?
>
> class ThreadHandler extends ThreadBean
> {
>    String receiver = getReceiver();
>    String sender = getSender();
>    String threadTopic = getPostTopic();
>    String threadBody = getPostBody();
>    Timestamp threadCreationDate =
> getThreadCreationDate();
>    int threadViewCount = getThreadViewCount();
>    int threadReplyCount = getThreadReplyCount();
>
>    .....
>    .....
> }
>
> To answer your question:
>
> 1. Have you checked to make sure that the fields you
> expect actually exist in the form before calling
> BeanUtils.copyProperties()?
>
> Yes, I have printed out the contents of the JSP text
> fields in my action class, which is a servlet.  For
> example: System.out.println( "the sender is " +
> postForm.get( "sender" ) );
>
> 2. If so, have you checked to see if the data was
> copied properly just after BeanUtils.copyProperties()?
>
> Yes, I have also printed out the value of the bean
> properties.  For example: System.out.println( "the
> sender is " + threadBean.getSender() );
>
> 3. BeanUtils.copyProperties() uses reflection to copy
> properties.  Are you using proper JavaBeans naming
> convention?
>
> Yes, I used a pair of get and set methods for each
> properties in the JavaBean (ThreadBean.java).  And in
> my action class, I could print out the value of those
> properties.
>
> --- Robert Taylor <rt...@mulework.com> wrote:
> > Have you checked to make sure that the fields you
> > expect
> > actually exist in the form before calling
> > BeanUtils.populate()?
> >
> > If so, have you checked to see if the data was
> > copied properly
> > just after BeanUtils.populate()?
> >
> > BeanUtils.populate() uses reflection to copy
> > properties.
> > Are you usingproper JavaBeans naming convention?
> >
> > Try and isolate exactly where the data is getting
> > "lost".
> >
> > I use BeanUtils.populate() all the time with
> > DynaXXXXForms and
> > have no problems. Usually when I do, I find that my
> > data member
> > names don't correspond.
> >
> > robert
> >
> > > -----Original Message-----
> > > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > > Sent: Wednesday, January 07, 2004 1:42 PM
> > > To: struts-user@jakarta.apache.org
> > > Subject: All The Bean Properties Are Null in the
> > Business Tier!!! (Used
> > > BeanUtils to Convert DynaValidatorForm)
> > >
> > >
> > > Hi, my program compiled OKay.  When I ran the
> > > application, somehow, the properties of my
> > > DynaValidatorForm are not passed from the action
> > class
> > > to the business tier and then to the data access
> > tier.
> > >  Please help me taking a look at what went wrong.
> >
> > >
> > > It looks that the validation worked fine:
> > >
> > > <form-bean
> > >     name="postForm"
> > >
> > >
> >
> type="org.apache.struts.validator.DynaValidatorForm">
> > >        <form-property
> > >             name="receiver"
> > >             type="java.lang.String"/>
> > >        <form-property
> > >             name="sender"
> > >             type="java.lang.String"/>
> > >     .....
> > >     .....
> > > </form-bean>
> > > because when I intentionally left several of the
> > text
> > > fields blank and submitted my JSP form, an applet
> > got
> > > prompted showing something in those fields are
> > > required.
> > >
> > > In my action class (see below),  I used BeanUtils
> > to
> > > copy all the properties of the DynaValidatorForm
> > to a
> > > bean (ThreadBean):
> > >
> > > DynaActionForm postForm = ( DynaActionForm )form;
> > > ThreadBean threadBean = new ThreadBean();
> > > BeanUtils.copyProperties( threadBean, postForm );
> > >
> > > I then added a number of properties with
> > pre-defined
> > > values to the ThreadBean in the Action class:
> > >
> > > Timestamp now = DateUtil.getCurrentGMTTimestamp();
> > > threadBean.setThreadCreationDate( now );
> > > threadBean.setThreadViewCount( 0 );
> > >
> > > I then called a method in a service class
> > > ThreadHandler and passing threadBean as the
> > parameter
> > > of that method:
> > >
> > > ThreadHandler thandler = new ThreadHandler();
> > > threadID = thandler.insertThread( threadBean );
> > >
> > > In the ThreadHandler class (see below), I first
> > tried
> > > to retrieve the values of those properties from
> > the
> > > ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE
> > NULL!
> > >
> > > class ThreadHandler extends ThreadBean
> > > {
> > >    int threadID = getThreadID();
> > >    String receiver = getReceiver();
> > >    String sender = getSender();
> > >    Timestamp threadCreationDate =
> > > getThreadCreationDate();
> > >    int threadViewCount = getThreadViewCount();
> > >
> > >    public ThreadHandler() {}
> > >
> > >    MessageDAO md = new MySQLMessageDAO();
> > >
> > >    public int insertThread( ThreadBean threadBean
> > )
> > >                             throws
> > > MessageDAOSysException, ObjectNotFoundException
> > >    {
> > >       md.createThread( receiver, sender,
> > >                    threadCreationDate,
> > threadViewCount
> > > );
> > >
> > >       .....
> > >       .....
> > >
> > >       return threadID;
> > >    }
> > > }
> > >
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Do you Yahoo!?
> > > Yahoo! Hotjobs: Enter the "Signing Bonus"
> > Sweepstakes
> > > http://hotjobs.sweepstakes.yahoo.com/signingbonus
> > >
> > >
> >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> > >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> >
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
> http://hotjobs.sweepstakes.yahoo.com/signingbonus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>


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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Caroline Jen <ji...@yahoo.com>.
I have narrowed down the problem.  Something went
wrong in my business tier class (ThreadHandler.java). 
All the properties in the JavaBean are populated
properly (I have checked).  The way I coded in the
ThreadHandler class failed to retrieve the value of
all the properties in the JavaBean (ThreadBean.java). 
All the values turned out to be null or 0.  Now, what
is wrong with the code?

class ThreadHandler extends ThreadBean 
{
   String receiver = getReceiver();
   String sender = getSender();
   String threadTopic = getPostTopic();
   String threadBody = getPostBody();
   Timestamp threadCreationDate =
getThreadCreationDate();
   int threadViewCount = getThreadViewCount();
   int threadReplyCount = getThreadReplyCount();
 
   .....
   .....
}

To answer your question:

1. Have you checked to make sure that the fields you
expect actually exist in the form before calling
BeanUtils.copyProperties()?

Yes, I have printed out the contents of the JSP text
fields in my action class, which is a servlet.  For
example: System.out.println( "the sender is " +
postForm.get( "sender" ) );

2. If so, have you checked to see if the data was
copied properly just after BeanUtils.copyProperties()?

Yes, I have also printed out the value of the bean
properties.  For example: System.out.println( "the
sender is " + threadBean.getSender() );

3. BeanUtils.copyProperties() uses reflection to copy
properties.  Are you using proper JavaBeans naming
convention?

Yes, I used a pair of get and set methods for each
properties in the JavaBean (ThreadBean.java).  And in
my action class, I could print out the value of those
properties.

--- Robert Taylor <rt...@mulework.com> wrote:
> Have you checked to make sure that the fields you
> expect
> actually exist in the form before calling
> BeanUtils.populate()?
> 
> If so, have you checked to see if the data was
> copied properly 
> just after BeanUtils.populate()?
> 
> BeanUtils.populate() uses reflection to copy
> properties.
> Are you usingproper JavaBeans naming convention?
> 
> Try and isolate exactly where the data is getting
> "lost".
> 
> I use BeanUtils.populate() all the time with
> DynaXXXXForms and
> have no problems. Usually when I do, I find that my
> data member
> names don't correspond.
> 
> robert
> 
> > -----Original Message-----
> > From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> > Sent: Wednesday, January 07, 2004 1:42 PM
> > To: struts-user@jakarta.apache.org
> > Subject: All The Bean Properties Are Null in the
> Business Tier!!! (Used
> > BeanUtils to Convert DynaValidatorForm)
> > 
> > 
> > Hi, my program compiled OKay.  When I ran the
> > application, somehow, the properties of my
> > DynaValidatorForm are not passed from the action
> class
> > to the business tier and then to the data access
> tier.
> >  Please help me taking a look at what went wrong. 
>  
> > 
> > It looks that the validation worked fine: 
> > 
> > <form-bean
> >     name="postForm"
> >           
> >
>
type="org.apache.struts.validator.DynaValidatorForm">
> >        <form-property
> >             name="receiver"
> >             type="java.lang.String"/>
> >        <form-property
> >             name="sender"
> >             type="java.lang.String"/>
> >     .....
> >     .....
> > </form-bean>
> > because when I intentionally left several of the
> text
> > fields blank and submitted my JSP form, an applet
> got
> > prompted showing something in those fields are
> > required.
> > 
> > In my action class (see below),  I used BeanUtils
> to
> > copy all the properties of the DynaValidatorForm
> to a
> > bean (ThreadBean): 
> > 
> > DynaActionForm postForm = ( DynaActionForm )form;
> > ThreadBean threadBean = new ThreadBean();
> > BeanUtils.copyProperties( threadBean, postForm );
> > 
> > I then added a number of properties with
> pre-defined
> > values to the ThreadBean in the Action class:
> > 
> > Timestamp now = DateUtil.getCurrentGMTTimestamp();
> > threadBean.setThreadCreationDate( now );
> > threadBean.setThreadViewCount( 0 );
> > 
> > I then called a method in a service class
> > ThreadHandler and passing threadBean as the
> parameter
> > of that method:
> > 
> > ThreadHandler thandler = new ThreadHandler();
> > threadID = thandler.insertThread( threadBean );
> > 
> > In the ThreadHandler class (see below), I first
> tried
> > to retrieve the values of those properties from
> the
> > ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE
> NULL!
> > 
> > class ThreadHandler extends ThreadBean 
> > {
> >    int threadID = getThreadID();
> >    String receiver = getReceiver();
> >    String sender = getSender();
> >    Timestamp threadCreationDate =
> > getThreadCreationDate();
> >    int threadViewCount = getThreadViewCount();
> > 
> >    public ThreadHandler() {}
> > 
> >    MessageDAO md = new MySQLMessageDAO();
> > 
> >    public int insertThread( ThreadBean threadBean
> )
> >                             throws
> > MessageDAOSysException, ObjectNotFoundException
> >    {
> >       md.createThread( receiver, sender,   
> >                    threadCreationDate,
> threadViewCount
> > );
> > 
> >       .....
> >       .....
> > 
> >       return threadID;
> >    }
> > }
> > 
> > 
> > 
> > 
> > 
> > __________________________________
> > Do you Yahoo!?
> > Yahoo! Hotjobs: Enter the "Signing Bonus"
> Sweepstakes
> > http://hotjobs.sweepstakes.yahoo.com/signingbonus
> > 
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> > 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

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


RE: All The Bean Properties Are Null in the Business Tier!!! (Used BeanUtils to Convert DynaValidatorForm)

Posted by Robert Taylor <rt...@mulework.com>.
Have you checked to make sure that the fields you expect
actually exist in the form before calling BeanUtils.populate()?

If so, have you checked to see if the data was copied properly 
just after BeanUtils.populate()?

BeanUtils.populate() uses reflection to copy properties.
Are you usingproper JavaBeans naming convention?

Try and isolate exactly where the data is getting "lost".

I use BeanUtils.populate() all the time with DynaXXXXForms and
have no problems. Usually when I do, I find that my data member
names don't correspond.

robert

> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Wednesday, January 07, 2004 1:42 PM
> To: struts-user@jakarta.apache.org
> Subject: All The Bean Properties Are Null in the Business Tier!!! (Used
> BeanUtils to Convert DynaValidatorForm)
> 
> 
> Hi, my program compiled OKay.  When I ran the
> application, somehow, the properties of my
> DynaValidatorForm are not passed from the action class
> to the business tier and then to the data access tier.
>  Please help me taking a look at what went wrong.   
> 
> It looks that the validation worked fine: 
> 
> <form-bean
>     name="postForm"
>           
> type="org.apache.struts.validator.DynaValidatorForm">
>        <form-property
>             name="receiver"
>             type="java.lang.String"/>
>        <form-property
>             name="sender"
>             type="java.lang.String"/>
>     .....
>     .....
> </form-bean>
> because when I intentionally left several of the text
> fields blank and submitted my JSP form, an applet got
> prompted showing something in those fields are
> required.
> 
> In my action class (see below),  I used BeanUtils to
> copy all the properties of the DynaValidatorForm to a
> bean (ThreadBean): 
> 
> DynaActionForm postForm = ( DynaActionForm )form;
> ThreadBean threadBean = new ThreadBean();
> BeanUtils.copyProperties( threadBean, postForm );
> 
> I then added a number of properties with pre-defined
> values to the ThreadBean in the Action class:
> 
> Timestamp now = DateUtil.getCurrentGMTTimestamp();
> threadBean.setThreadCreationDate( now );
> threadBean.setThreadViewCount( 0 );
> 
> I then called a method in a service class
> ThreadHandler and passing threadBean as the parameter
> of that method:
> 
> ThreadHandler thandler = new ThreadHandler();
> threadID = thandler.insertThread( threadBean );
> 
> In the ThreadHandler class (see below), I first tried
> to retrieve the values of those properties from the
> ThreadBean.  I FOUND OUT ALL THOSE VALUES ARE NULL!
> 
> class ThreadHandler extends ThreadBean 
> {
>    int threadID = getThreadID();
>    String receiver = getReceiver();
>    String sender = getSender();
>    Timestamp threadCreationDate =
> getThreadCreationDate();
>    int threadViewCount = getThreadViewCount();
> 
>    public ThreadHandler() {}
> 
>    MessageDAO md = new MySQLMessageDAO();
> 
>    public int insertThread( ThreadBean threadBean )
>                             throws
> MessageDAOSysException, ObjectNotFoundException
>    {
>       md.createThread( receiver, sender,   
>                    threadCreationDate, threadViewCount
> );
> 
>       .....
>       .....
> 
>       return threadID;
>    }
> }
> 
> 
> 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
> http://hotjobs.sweepstakes.yahoo.com/signingbonus
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 

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