You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ne...@mcbennett.net on 2001/12/14 22:04:28 UTC

Using BeanUtils.populate()

Hi, I looked at Ted Husted's example (Ted's catalogue) for using
BeanUtils.populate() to populate beans from a database query, and put them
into an ArrayList.
I am working on some code which queries an LDAP Directory and requires the
same kind of functionality. I reworked Ted's example to fit my needs, but
the beans never seem to get populated, so I end up with a ArrayList of empty
beans! I am using the package org.apache.commons.beanutils to do this and I
have tried version 1.0 and 1.1.
The relevant part of the code is shown below. I may have missed something
obvious, but I cannot see it! I have already verified that I am getting data
from the LDAP query, and I am believe the problem lies in popuating the
bean.

Thanks in advance

Neil

*****************************************************************


     // start looping over the LDAP entries...
      while ( searchResults.hasMoreElements())
      {
        LDAPEntry nextEntry = null;
        try
        {
          nextEntry = searchResults.next();
        }
        catch(LDAPException e)
        {
          System.out.println("Error: " + e.toString());

          // Exception is thrown, go for next entry
          continue;
        }

        // create a HashMap to hold att/val pairs...
        HashMap properties = new HashMap();

        LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
        Enumeration allAttributes = attributeSet.getAttributes();

        // loop over each attribute..
        while(allAttributes.hasMoreElements())
        {
          LDAPAttribute attrib = (LDAPAttribute)allAttributes.nextElement();

          // get the current attribute name...
          String attributeName = attrib.getName();

          // get the value(s) for this attribute...
          String[] Values = attrib.getStringValueArray();


          // if we have at least 1 value for this attribute...
          if( allValues.length != 0)
          {
            properties.put(attributeName, Values);
          }
        }

        // now populate bean for this entry...
        try
        {
          //instantiate new bean to hold results...
          UserResult userresult = new UserResult();

          BeanUtils.populate(userresult,properties);
          list.add(userresult);

        }
        catch (Exception e)
        {
          throw new IllegalAccessException("RequestUtils.getCollection: " +
e.toString());
        }
      }

*******rest of code follows.......



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Using BeanUtils.populate()

Posted by Tadashi Ishikawa <is...@wink.tis.co.jp>.
I think you need commons-collections.jar.

neil@mcbennett.net wrote:

>Hi, I looked at Ted Husted's example (Ted's catalogue) for using
>BeanUtils.populate() to populate beans from a database query, and put them
>into an ArrayList.
>I am working on some code which queries an LDAP Directory and requires the
>same kind of functionality. I reworked Ted's example to fit my needs, but
>the beans never seem to get populated, so I end up with a ArrayList of empty
>beans! I am using the package org.apache.commons.beanutils to do this and I
>have tried version 1.0 and 1.1.
>The relevant part of the code is shown below. I may have missed something
>obvious, but I cannot see it! I have already verified that I am getting data
>from the LDAP query, and I am believe the problem lies in popuating the
>bean.
>
>Thanks in advance
>
>Neil
>
>*****************************************************************
>
>
>     // start looping over the LDAP entries...
>      while ( searchResults.hasMoreElements())
>      {
>        LDAPEntry nextEntry = null;
>        try
>        {
>          nextEntry = searchResults.next();
>        }
>        catch(LDAPException e)
>        {
>          System.out.println("Error: " + e.toString());
>
>          // Exception is thrown, go for next entry
>          continue;
>        }
>
>        // create a HashMap to hold att/val pairs...
>        HashMap properties = new HashMap();
>
>        LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
>        Enumeration allAttributes = attributeSet.getAttributes();
>
>        // loop over each attribute..
>        while(allAttributes.hasMoreElements())
>        {
>          LDAPAttribute attrib = (LDAPAttribute)allAttributes.nextElement();
>
>          // get the current attribute name...
>          String attributeName = attrib.getName();
>
>          // get the value(s) for this attribute...
>          String[] Values = attrib.getStringValueArray();
>
>
>          // if we have at least 1 value for this attribute...
>          if( allValues.length != 0)
>          {
>            properties.put(attributeName, Values);
>          }
>        }
>
>        // now populate bean for this entry...
>        try
>        {
>          //instantiate new bean to hold results...
>          UserResult userresult = new UserResult();
>
>          BeanUtils.populate(userresult,properties);
>          list.add(userresult);
>
>        }
>        catch (Exception e)
>        {
>          throw new IllegalAccessException("RequestUtils.getCollection: " +
>e.toString());
>        }
>      }
>
>*******rest of code follows.......
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>