And what happens if myForm.get("somekey") == null? Right, NPE.
BeanUtils.geProperty will convert the property value to a String.
PropertyUtils.getProperty will just return the property value as an Object
without any conversion.

String foo = BeanUtils.geProperty(form, "somekey");

http://jakarta.apache.org/commons/beanutils/

Quoting Ian Hunter <[EMAIL PROTECTED]>:

> 
> forgot about String Object.toString() method.  Looks a little tidier,
> doesn't it?  Anyway, it should literally be calling the same code.  I'm
> hoping BeanUtils can somehow handle translations between types like
> java.util.Date and String...I'll look into it later.
> 
> ----- Original Message -----
> From: "Mark Lowe" <[EMAIL PROTECTED]>
> To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
> Sent: Thursday, March 13, 2003 9:28 AM
> Subject: Re: Example of accessing DynaActionFrom properties from within an
> Action
> 
> 
> I had a headache over all this until i spent a couple fo days using the
> beanutils package..
> 
> 
> org.apache.common.beanutils
> 
> The dynaaction forms are basically the same..
> 
> your means of extracting the stored values from the form look like they
> should work , although i prefer using toString() rather than casting
> the world..
> 
> DynaActionForm myForm = (DynaActionForm) form;
> String foo = myForm.get("somekey").toString();
> 
> So what you are doing works for me...
> 
> hope this helps mark
> 
> Gioved�, 13 mar 2003, alle 15:03 Europe/Rome, Ian Hunter ha scritto:
> 
> > Since this isn't as clear as it could be, here's an example for anyone
> > who
> > cares:
> >
> >> (this class adds or updates a bulletin on a message board; all data
> >> access
> >> goes through a central service called the "DataStore")
> >>
> >> public final class UpdateBulletinAction extends Action {
> >>
> >>      public ActionForward execute(ActionMapping mapping, ActionForm
> >> form,
> >> HttpServletRequest request,  HttpServletResponse response)
> >>  throws Exception {
> >>
> >>  ActionErrors errors = new ActionErrors();
> >>
> >>         DynaActionForm dynaForm = (DynaActionForm) form;
> >>         UserView user =
> >> (UserView)request.getSession().getAttribute(Constants.USER_KEY);
> >>
> >>         if (((String)((DynaActionForm)form).get ("id")).equals ("0"))
> >> { //
> >> ID = 0 means we're adding a bulletin
> >>                 try {
> >>                     BulletinView bv = DataStore.getNewBulletinView();
> >>                     bv.setAccessLevel ((String)dynaForm.get
> >> ("accessLevel"));
> >>                     bv.setMessage ((String)dynaForm.get("message"));
> >>                     DataStore.addBulletinView (bv);
> >>                 } catch (ViewRecAddException e) {
> >>                     servlet.log("An error occurred while user " +
> >> user.getName() + " was adding a bulletin:\n" +
> >>                       e.toString());
> >>                     e.printStackTrace();
> >>                     errors.add(ActionErrors.GLOBAL_ERROR,
> >>                       new ActionError("error.databaseerror"));
> >>                 }
> >>         } else { // Since we have an ID, we're updating a bulletin
> >>                 try {
> >>                     BulletinView bv =
> >> DataStore.getBulletinViewById((String)dynaForm.get("id"));
> >>                     bv.setAccessLevel
> > ((String)dynaForm.get("accessLevel"));
> >>                     bv.setMessage ((String)dynaForm.get("message"));
> >>                     DataStore.updateBulletinView (bv);
> >>                 } catch (ViewRecLoadException e) {
> >>                     servlet.log("An error occurred while trying to get
> >> bulletin ID=" + (String)dynaForm.get("id") + " for update by user " +
> >> user.getName() +
> >>                       ":\n" + e.toString());
> >>                     e.printStackTrace();
> >>                     errors.add(ActionErrors.GLOBAL_ERROR,
> >>                       new ActionError("error.databaseerror"));
> >>                 } catch (ViewRecUpdateException e) {
> >>                     servlet.log("An error occurred while user " +
> >> user.getName() + " was updating bulletin with ID=" +
> >> (String)dynaForm.get("id") +
> >>                       ":\n" + e.toString());
> >>                     e.printStackTrace();
> >>                     errors.add(ActionErrors.GLOBAL_ERROR,
> >>                       new ActionError("error.databaseerror"));
> >>                 }
> >>         if (!errors.isEmpty()) {
> >>      saveErrors(request, errors);
> >>             return (new ActionForward(mapping.getInput()));
> >>         }
> >>
> >>         return (mapping.findForward("success"));
> >>     }
> >> }
> >>
> >> so to refer to a property of a DynaActionForm within an action, you do
> >> something like
> >>
> >> DynaActionFrom dynaForm = (DynaActionForm) form;
> >> // variable "form" comes from method execute's signature
> >>
> >> someVariable = (className)dynaForm.get("propertyName");
> >> // to get the property; DynaActionForm.get(String property) returns an
> > Object, so you have to cast it
> >>
> >> dynaForm.set("propertyName", someVariable);
> >> // to set the property; the class of the second parameter is, again,
> > "Object"
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to