• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic FormBean Population

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vijay Bala
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic