I have some code that works great in my Bean class that looks like the
below where I have around 15 fields. For example I condensed it to 2
fields:
//bean model part
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
if (firstname.equals(""))
{
errors.put("firstname","missing firstname".);
}
if (lastname.equals(""))
{
errors.put("lastname","missing lastname");
}
//Each field is then called like this in a View page:
errors.get("firstname"));
errors.get("lastname"));Anyway I can create a loop to go through all
15 fields or how is the best way to do this??
//use Array or Iterator or List or what to get the 15 fields then
loop?
for(int i= 0;i < 15;i++)
{
/*
how would I make this part work?
if (firstname.equals(""))
{
errors.put("firstname","missing firstname".);
}
if (lastname.equals(""))
{
errors.put("lastname","missing lastname");
}
/*
}
Please advise.
Joshua Cranmer - 17 Aug 2007 02:10 GMT
> Anyway I can create a loop to go through all
> 15 fields or how is the best way to do this??
NOTE: I do not have experience with beans, so there might be an
exception to my rules here. Soliciting advice from anyone with more
knowledge here to supersede me.
The best way is to internally store the fields as a HashMap and iterate
over that.
The second best way is to internally store the fields as a TreeMap and
iterate over that.
The third best way is to internally store the fields as anything else
that implements the Map interface and iterate over that.
The fourth best way is to manually go through each field.
There is a fifth way that I really don't recommend. Try using the first
method instead.
Still want to hear it?
This uses reflection:
Field[] fields = {< classname >}.class.getDeclaredFields();
for (Field f : fields) {
if (f.getType().getName().equals("java.lang.String")) {
errors.put(f.getName(),(String)f.get(this));
}
}
Like all reflection, that would have to be wrapped with a myriad of
exceptions; it is also prone to breaking if one is not careful (although
annotations would considerably help if this were being done). Finally,
it is also considerably slower than using a HashMap.

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
GArlington - 17 Aug 2007 11:20 GMT
> tes...@hotmail.com wrote:
> > Anyway I can create a loop to go through all
[quoted text clipped - 38 lines]
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth
How about using reflection once (on init) to populate the fields
hashmap and then use the hashmap on each validation run?
Lew - 17 Aug 2007 14:07 GMT
Joshua Cranmer wrote:
>> There is a fifth way that I really don't recommend. Try using the first
>> method instead.
[quoted text clipped - 15 lines]
>> annotations would considerably help if this were being done). Finally,
>> it is also considerably slower than using a HashMap.
> How about using reflection once
One should avoid using reflection almost always. When one does use
reflection, it should be limited to Class.forName() almost always.

Signature
Lew
Oliver Wong - 17 Aug 2007 23:51 GMT
>I have some code that works great in my Bean class that looks like the
> below where I have around 15 fields. For example I condensed it to 2
[quoted text clipped - 18 lines]
> errors.get("lastname"));Anyway I can create a loop to go through all
> 15 fields or how is the best way to do this??
[...]
This is the third time I've seen you post this question. Why haven't
you accepted the answers you were already given?
http://groups.google.com/group/comp.lang.java.programmer/msg/eb3f492ca7ae8c5b
- Oliver