File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Dynamic FormBean Population Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Dynamic FormBean Population" Watch "Dynamic FormBean Population" New topic
Author

Dynamic FormBean Population

Vijay Bala
Greenhorn

Joined: Oct 25, 2001
Posts: 25
I have 3 classes. I am having a problem trying to pass the 2nd parameter to the validate(,) method in the
Temp class.
Here is the code :
class Parmfields {
String fldName;
boolean reqd;
}
abstract class Temp extends HttpServlet {
// Formbean stores the form input fields into a JavaBean and is not shown here.
public void checkFields(Parmfields[] parms, Formbean userFormBean) {

for (i=0;i< parms.length;i++) {

//if its a required field,validate the form field

if (parms[i].reqd) {

validate(parms[i].fldName,userFormBean.get + parms[i].fldName + () );//<======

// Here i want to use the diff. get methods of the FormBean as the 2nd parameter
// i.e getProdName(),getProdId() etc where the diff properties are populated
// dynamically.I can't figure out away to do that dynamically.
// I am planning to overload the validate() method for the diff data types for the 2nd
// parameter

}// end if

} //end for

} // end checkFields()
} // end Temp

class ConcreteTemp extends Temp {
init() }
Parmfields[] parmarray = [{ProdName,true},{ProdId,false},{ProdCust,true},...... ]
}
}

I have been thinking about using userformBean.getClass().getMethods() and then looping thru the
Method [] to get my String value but i still have the problem of trying to use it with
userFormBean.???
Also, can anyone point me to where i can see the code for how JSP implements the type conversion for
<jsp:getProperty> converting the properties into string. I could maybe use the idea to avoid
overloading my validate() method for the diff field types.
Thanx in advance,
Vijay
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by Vijay Bala:
I have 3 classes. I am having a problem trying to pass the 2nd parameter to the validate(,) method in the
Temp class.
Here is the code :
class Parmfields {
String fldName;
boolean reqd;
}
abstract class Temp extends HttpServlet {
// Formbean stores the form input fields into a JavaBean and is not shown here.
public void checkFields(Parmfields[] parms, Formbean userFormBean) {

for (i=0;i< parms.length;i++) {

//if its a required field,validate the form field

if (parms[i].reqd) {

validate(parms[i].fldName,userFormBean.get + parms[i].fldName + () );//<======

// Here i want to use the diff. get methods of the FormBean as the 2nd parameter
// i.e getProdName(),getProdId() etc where the diff properties are populated
// dynamically.I can't figure out away to do that dynamically.
// I am planning to overload the validate() method for the diff data types for the 2nd
// parameter

}// end if

} //end for

} // end checkFields()
} // end Temp

class ConcreteTemp extends Temp {
init() }
Parmfields[] parmarray = [{ProdName,true},{ProdId,false},{ProdCust,true},...... ]
}
}

I have been thinking about using userformBean.getClass().getMethods() and then looping thru the
Method [] to get my String value but i still have the problem of trying to use it with
userFormBean.???.???

I've used code like the following to populate arbitrary beans (which is what I think you're asking).
(This is from page 15 of my JUG/OOPSLA presentation on my web page below)
/**
* Iterate through the setters of this object and then take the named
* parameters that match and set the value of the setter
* to the value of the corresponding parameter
*/
public void mapBean(javax.servlet.http.HttpServletRequest req, Object bean) {
Method[] methods = bean.getClass().getMethods();
for (int i=0; i<methods.length; i++) {
Method meth = methods[i];
if (meth.getName().startsWith("set")) {
String propertyName = meth.getName().substring(3).toUpperCase();
// matching without case is faster than matching with case
String parm = req.getParameter(propertyName);
if (parm != null) {
Object[] objs = new Object[1];
objs[0] = parm;
try {
meth.invoke(bean, objs);
} catch (Exception e) {} // could not set
}
}
}
}

Also, can anyone point me to where i can see the code for how JSP implements the type conversion for
<jsp:getProperty> converting the properties into string. I could maybe use the idea to avoid
overloading my validate() method for the diff field types.
Thanx in advance,
Vijay


------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Vijay Bala
Greenhorn

Joined: Oct 25, 2001
Posts: 25
Kyle,
I picked that idea from your website only. I still haven't found an answer for my problem yet.
Thanx,
Vijay
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
OK, I think I see your problem now.
So you want to look at each getter method, find out its type and then what? Are you going to do some sort of "not null" or "not empty" check?
I can't see how you plan to write a generic "validate()" method for all fields. For instance, validating a String field that's a phone number is different than validating a String field that's a name.
Why don't you just take the Apache Struts approach and make validating the fields the job of the form bean?
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
Vijay Bala
Greenhorn

Joined: Oct 25, 2001
Posts: 25
HI Kyle,
Thanx for your input and i really liked some of your articles. Its interesting to note that many of the people here in the Portland circle like Ward Cunningham,Kent beck, martin Fowler, Rebecca etc are all Smalltalk people and it was interesting to read about what you have mentioned abut Smalltalk.
Coming back to my problem -
Does meth.invoke(bean,null) actually invoke bean.meth() ???
Also, i have realized that i can't have a single generic validate() method. I read just a little about the Struts FormBean but i am not sure as to how to use it as of now-haven't applied myself to using it as all I am trying to do right now as refactor an existing servlet which runs into about 30 pages. I guess I might have to selectively use parts of the Struts framework but that wil have to wait for another day as of now-maybe the next round of refactoring. so,here goes the code again.

public void checkFields(Parmfields[] parms, Formbean userFormBean) {

for (i=0;i< parms.length;i++) {

// get the value from each Formbean.getprodId(),Formbean.getProdName() etc
//creates each get method of bean
String mName = "get" + parms[i].fldName;
Class c = FormBean.class;
Class[] args = {}; // since my getMethods in the bean don't hv any args
try {
Method m = c.getDeclaredMethod(mName,null);
// Method m = c.getMethod(mName,args);
String result = m.invoke(userFormBean, null); <<===================
//??? Will this m.invoke(userFormBean, null) actually result in the foll. line
//for eg ?? Will either of these invoke mehods work for me ???
// userFormBean.getProdId() and return a value <<<================


}catch(Exception e {}

//if its a required field,validate the form field

if (parms[i].reqd) {

validate(parms[i].fldName,result );

// I can then use the String result here

}// end if

} //end for

} // end checkFields()

Thanx in advance,
Vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic FormBean Population