Hi,
Some questions that have been in my mind after using Struts for a while. I am still
very confused as
the use of setter/getter in beans+struts environment. The setter and getter are
supposed to do
several things I think (from an OOP point of view):
[1]setter - validate the input data, either reject it, or do some reformating to get
it to the format we
want
[2]getter - calculate values out of the private attributes if they are needed.
And almost 95% of the setters/getters that I code using the Struts Framework as such
as (as good
as a public attribute) :
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
be it in the Bean (Business Level) or the Form Bean. Since I understand that the
PropertyUtils is
using the setters/getters to do the transfer between Bean<->Form Bean, we have to
provide all the
setter/getter functions for all the attributes. Okay, here's why I think setter/getter
should have no
use in Struts
[1] setter - since this is the web front end, everything in the From is done in one
go, so that's why
we have a validate function instead of every setter having their own validation
function, so we
don't actually need any validation at all. If we are doing some internal
transformation to the values,
let say 400 degrees -> 40 degrees, it won't be something that the user wants to see
from the front
end, since he did enters 400 and it came out 40 the next time when he loads it, the
validate function
should took care of that and explains what's wrong with the value keyed in.
[2] getter - it seemed that from the front end, every form input item wil be an
attribute, so there is not
much that we can do inside getter.
Okay, let say I have a value that i want it to be an int (simple type) in the Business
Level Bean, how
would I transform this into String in the JSP?? The setter/getter of the FormBean will
have to be in
taking in and returning String. So not much choice actually in the FormBean. Let's
see, so if we
really want the value to be in int, the internal value wil be in int, and we will want
to use a
getter/setter as int values. But if we set the setter/getter to int values, will the
PropertyUtils do its job
in transforming the int value -> String values in the Form Bean?
(what I did the work this around is to make another setter/getter pair, eg int
getAge(); String
getAgeStr(); void setAge(int age); void setAgeStr(String age); )
or worse? Use string all the way into the Bean itself? where we hold the Buisness
Level Logic??
Cheers,
Yee Keat