Hi,
I myself really need this implementation so i am hoping that it can be inserted in
Struts 1.0.
If not then i must alway's use a slightly altered struts version :-(
What i want:
I want to control my own implementation of getting and setting property's for a
specifiek bean of mine
when using struts. The bean i want to control the setting and getting of properties is
a Form bean of mine
so i need to alter struts it self because struts uses PropertyUtils for this. I can't
have a get and set for
every property.
What i altered:
I inserted a Interface in the struts.util package:
public interface Property
{
public Object getProperty(String);
public void setProperty(String,object);
}
So if a bean wants to control it's own property's it must implement that interface.
PropertyUtils.getSimpleProperty(Object bean, String name){
if(bean instanceof Property) {
return ((Property)bean).getProperty(name);
}
else {
// CURRENT IMPLEMENTATION.
}
}
and
PropertyUtils.setSimpleProperty(Object bean, String name, Object value){
if(bean instanceof Property) {
((Property)bean).setProperty(name, value);
}
else {
// CURRENT IMPLEMENTATION.
}
}
I only implemented the SimpleProperty at this time, maybe the same should be done with
indexed.
But if a bean implements the Property Interface the PropertyUtils class is relaying
this to
the bean it self.
Also the BeanUtils.populate(Object bean, Map properties) had to change a bit because
that method is trying to get the PropertyDescriptor for a given name and it none is
found
it won't call the PropertyUtils.setProperty(bean, name, parameters[0]);
the method:
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
if(bean instanceof Property) {
while (names.hasNext()) {
String name = (String) names.next();
if (name == null) continue;
Object value = properties.get(name); // String or String[]
Object parameters[] = new Object[1];
if (value instanceof String[]) {
parameters[0] = ((String[])value)[0];
}
else {
parameters[0] = value;
}
try{
PropertyUtils.setProperty(bean, name, parameters[0]);
}
catch (NoSuchMethodException e) {
if (debug >= 1) {
System.out.println(" CANNOT HAPPEN: " + e);
e.printStackTrace(System.out);
}
}
}
}
else{
while (names.hasNext()) {
// CURRENT IMPLEMENTATION
}
}
I attached all the files.
Johan Compagner
PropertyUtils.java
Property.java
BeanUtils.java