I got tired of trying to get some answers and listened to a couple of
things Martin Fowler said on the users list and decided to just write my
own tags. I find I can write my own tags as easy as finding out how some
of the existing ones work. Some of that code is a little "dark"! LOL
I have some code that I think improves PropertyUtils in commons and its
ability to handle nested maps and other nested situations. This also has a
new class to go in with the bean options, which is really an improvement on
the WriteTag in conjunction with changes to PropertyUtils in commons and
RequestUtils in org.apache.struts.util. I call the tag WriteMapTag and use
it myself, as follows:
<map:write name="host" property="page(logon).key(bannerLeftColor)"
scope="request" />
The central code for this is like the following:
public static Object getNestedProperty(Object bean,
String property)
throws IllegalAccessException,
InvocationTargetException,
NoSuchMethodException,
ChainedException {
Map map = null;
if (bean == null) {
throw new IllegalArgumentException("No bean specified");
}
if (property == null) {
throw new IllegalArgumentException("No property specified");
}
int indexOfNESTED_DELIM = -1;
int indexOfMAPPED_DELIM_L = -1;
int indexOfMAPPED_DELIM_R = -1;
String next = null;
int loop = -1;
if(!(bean instanceof Map)) {
ChainedException ce = new ChainedException(new
IllegalArgumentException("This is not a BeanMap object"), "This is not a
BeanMap object. The object is a " + bean);
throw ce;
}
map = (Map)bean;
String payload = null;
while(true) {
payload = property.substring(property.indexOf(MAPPED_DELIM_L) +
1,property.indexOf(MAPPED_DELIM_R));
loop = property.indexOf(NESTED_DELIM);
if(loop > 0) {
try {
map = (Map)(map.get(payload));
} catch (ClassCastException cce) {
property = "Either you are using a class other than a map or you
are burrowing too deep into the map. The class cast exception trace is: "
+ StackTrace.trace(cce);
break;
}
property = property.substring(loop + 1);
} else {
break;
}
}
// This need not return a string but it has to
// return something whose payload is a string.
return map.get(payload);
} ///;-) Michael McGrady
With maps, the "page" and "key" are just toss ins. If you specify a type
atttribute, then these become method names with the parameters becoming
arguments. You get the idea. How do I go about showing these to someone
and seeing if they might be of some assistance?
- RE: How do I submit code? Michael McGrady
- RE: How do I submit code? Steve Raeburn
- RE: How do I submit code? Michael McGrady