Why don't we use PropertyEditor?

http://java.sun.com/j2se/1.3/docs/api/java/beans/PropertyEditor.html

-----Original Message-----
From: James Mitchell [mailto:[EMAIL PROTECTED]] 
Sent: Saturday, February 16, 2002 2:27 PM
To: 'Struts Users Mailing List'
Subject: RE: Form -> Bean conversion

I have also seen this done as an "embedded bean" object for generic
action
form.

What you would do is create a generic ActionForm with a setBean() method
and
a getBean() method.

Then in your ActionClass, call setBean( bean ) in the form and pass it
to
the action forward.

Then in your jsp, always refer to the ActionForm with "bean.<method>"

This concept forces the struts ActionForm to act as a wrapper to your
bean
classes and you never have to write a single "duplicate" line of code.

Example:
Say you have a bean called Customer:
---------------------------------------
public class Customer {
  private String firstName;
  private String lastName;

 public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getLastName() {
    return lastName;
  }
}
----------------------------------------

Now you want to use it in struts without using some mapping utility or
writing a bunch of this:
...
...
form.setFirstName( bean.getFirstName() );
form.setLastName( bean.getLastName() );
...
...

right?

Then don't.

Create one ActionForm (for each bean) like so:
-------------------------------------------------------
public class GenericDetailForm extends ActionForm {

  private Customer bean = null;

  public void setBean( Customer bean ){
    this.bean = bean;
  }
  public Customer getBean(){
    return this.bean;
  }
...
...
//rest of required code here
...
...

}
-------------------------------------------------------


Then in your jsp page.....do something like this:

...
...
<tr>
 <td><html:text property="bean.firstName" size="10"/></td>
...
...

Struts will auto-populate your existing bean for you and hand it to you
(stuffed) in your ActionForm (in the perform() of your Action class)



*Note - you could even take it a step further and use the same wrapper
for
**ALL** your beans.  Of course that would mean changing your bean field
in
the wrapper class to java.lang.Object and then casting it out of the
form at
the top of each jsp page, which isn't much work considering the time
saved
in code reuse. (Let's face it, some people just can't stand
scriplets....like me)


James Mitchell
Software Engineer
Open-Tools.org
Home Phone (770) 822-3359
Cell Phone: (678) 910-8017


-----Original Message-----
From: CyberZombie [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 15, 2002 11:36 PM
To: Struts Users Mailing List
Subject: Re: Form -> Bean conversion


Here's a rudimentary wrapper tool.  The commented out section is used
when wrapping for Struts 1.0.x -- 1.1 does a much better job there.  I
still have hardcoded the file masks (I assume all VO's end in
Value.java).  And I assume that all wrapped getters have corresponding
setters.  What can I say...I got lazy when I got it working and went
back to solving the business problems on this app... :)

John M. Corro wrote:

>We have a bunch of existing beans that we'd like to use w/ ActionForms.
For each bean we'll need to obviously expose the getters/setters in the
corresponding ActionForm.  The ideal scenario we'd like to see happen is
prevent the ActionForms from having all the corresponding
getters/setters
hardcoded into it.  We're ok w/ coding the validate() method for each
class
by hand, but we'd like to avoid having to code each ActionForm w/ a
getter/setter (even if it is templated out for us by a Struts plug in or
even a custom written batch file).
>
>Has anyone implemented something like this?
>




_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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



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

Reply via email to