Hey Java Guy,
Here is a method that I wrote to do what you are looking for. Maybe this will help you out. I think this is sort of what Craig was talking about.
The beans that I use either have fields that are all of type String, or it is a lister bean that contains all fields of type String[]. So to populate a bean I fill a hashtable with name-value pairs where the names all match the names of my fields in the bean. I use this method when I want to populate the bean. It is called like this:
MyBean myBean = (MyBean) BeanHelper.populateBean(myHastable,MyBean.class);
public static Object populateBean(Hashtable ht, Class aClass) throws Exception
{
// Create an instance of the class
Object myBean = aClass.newInstance();
// Iterate through Hashtable and get the names
for (Enumeration keys = ht.keys(); keys.hasMoreElements();)
{
String key = (String) keys.nextElement();
String propertyName = key;
// Change the property name to the setXXX() naming convention
propertyName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
// Debugging
//System.out.println(propertyName);
//
// Now get the setter method
java.lang.reflect.Method method = aClass.getMethod("set" + propertyName, new Class[] {java.lang.String.class});
// Call the setter method for this property for this object
method.invoke(myBean, new Object[] {ht.get(key)});
}
// Return the object
return myBean;
}
David Eaves
-----Original Message-----
From: Java Guy [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 28, 1999 10:05 AM
To: [EMAIL PROTECTED]
Subject: Re: Something like jsp:setProperty in servlets?
Thanks for the information. Since there doesn't seem to be a method in the
servlet API to do this, I've come up with a solution: Instead of submitting
the data directly to the servlet, the submit button sends the page to a jsp
which does nothing but populate the bean with the data from the form. The
jsp then forwards to the servlet. Something like:
<jsp:useBean id="abean" scope="session" class="APackage.ABean" />
<jsp:setProperty name="abean" property="*" />
<jsp:forward page="/servlet/APackage.ATest" />
This approach requires one additional .jsp per form/bean pair. A more
clever jsp can probably be written which will work for all pages -- probably
by passing the bean name, class name and the name of the servlet that the
page will be forwarded to...
Yet another, probably better, way would be to just encapsulate the logic
from Tomcat in a helper class that will be used by the servelt to initialize
the bean.
>From: "Craig R. McClanahan" <[EMAIL PROTECTED]>
>Reply-To: "Craig R. McClanahan" <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Something like jsp:setProperty in servlets?
>Date: Mon, 27 Dec 1999 17:23:17 -0800
>
>Java Guy wrote:
>
> > Is there something similar to jsp:setProperty is the servlet API? I'm
> > trying to implement a "Model 2" application. The "Submit" button on a
>(jsp
> > generated) HTML page sends the contents of the form to a servlet, which,
> > after processing the contents, forwards the request to a JSP page which
> > displays the results.) In order to facilitate the processing of the
>data by
> > the servlet, I'd like a way to automatically get all the data that was
> > submitted by the form to be available in a bean, instead of getting each
> > attribute seperately in the servlet.
> >
> > If it were a jsp only solution,
> > <jsp:setProperty name="mybean" property="*" />
> >
> > at the top of the .jsp would do the trick.
> >
>
>There is nothing like this in the standard servlet API. However, you could
>build a
>utility method that did this for you. It could accept a JavaBean instance
>and your
>current HttpServletRequest as arguments, use the Java Reflection API to
>introspect
>the property names supported by that bean, and do the appropriate
>assignments based
>on the parameters included in the request.
>
>Because this process is so similar to what a JSP engine does for the
>servlet
>generated by your JSP pages, it might be possible to look at the source
>code for an
>existing JSP engine implementation (such as Jakarta's Tomcat at
><http://jakarta.apache.org>), extract the code that does this for that
>implementation, and use it as a starting point for your utility class.
>
>Craig McClanahan
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
