Of course supplying getters for your constant values would work like a charm for JSTL ;-)

Rick Reumann wrote:

Woodchuck wrote:

how can i do the following the non-scriptlet way on my jsp page?

<%@ page import="MyPackage.Constants"%>

<%= Constants.BUTTON__KEY %>


is there an elegant Struts or JSTL equivalent for the above?


Thankfully Kris Schneider demonstrated a good way to handle this...

In your Constants class add a method similar to this:

public static Map getConstantsMap() {
Map propMap = null;
try {
Field[] allFields = Constants.class.getDeclaredFields();
int numFields = allFields.length;
propMap = new HashMap(numFields);
for(int i = 0; i < numFields; i++) {
Field f = allFields[i];
int mods = f.getModifiers();
if(Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
String name = f.getName();
Object value = f.get(null);
propMap.put(name, value);
}
}
} catch(IllegalAccessException ie) {
log.error("Problem loading getConstantsMap " + ie);
}


    return Collections.unmodifiableMap(propMap);
}


Then, I have a Servlet that runs on applications startup (such as a ServletContextListener) that will load the constants map into application scope:


ServletContext context = contextEvent.getServletContext();
try {...
   context.setAttribute("CONSTANTS", Constants.getConstantsMap());

Now anywhere in your JSPs you can just use the Map

${CONSTANTS.SOME_CONSTANT}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to