It's not so bad, really ;-). For some additional background, here are a few
older messages from struts-user and taglibs-user:

http://marc.theaimsgroup.com/?l=struts-user&m=103790677413408&w=2
http://marc.theaimsgroup.com/?l=struts-user&m=105777660215673&w=2
http://marc.theaimsgroup.com/?l=taglibs-user&m=105889207116316&w=2

Here's a slightly different approach:

package com.dotech.util;

import java.io.Serializable;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ConstantBean implements Serializable {

    private Map constants;

    public ConstantBean() {
        super();
    }

    public void setClassName(String className) throws ClassNotFoundException,
                                                      IllegalAccessException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = getClass().getClassLoader();
            if (loader == null) {
                loader = ClassLoader.getSystemClassLoader();
            }
        }
        Class clazz = loader.loadClass(className);
        Field[] allFields = clazz.getDeclaredFields();
        int numFields = allFields.length;
        Map 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);
            }
        }
        this.constants =  Collections.unmodifiableMap(propMap);
    }

    public Map getConstants() {
        return this.constants;
    }
}

Which can be used in a page like this:

<%@ page contentType="text/plain" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"; %>

<jsp:useBean id="responseConstants" class="com.dotech.util.ConstantBean">
    <jsp:setProperty name="responseConstants"
                     property="className"
                     value="javax.servlet.http.HttpServletResponse"/>
</jsp:useBean>

SC_INTERNAL_SERVER_ERROR: <c:out
value="${responseConstants.constants.SC_INTERNAL_SERVER_ERROR}"/>

Quoting Bryan Hunt <[EMAIL PROTECTED]>:

> Uh oh, I just realised that is the exact same as mine just implimented 
> differently.
> 
> <c:out value="${CONSTANTS.WHATEVER)"/>
> 
> Will output the string value but will not be able to be used to resolve 
> the bean by that
> name.
> 
> I'm giving up on JSTL constants.  Nice idea ... but too expensive on my 
> hairline.
> 
> --b
> 
> Rick Reumann wrote:
> 
> > On Wed, 07 Jul 2004 20:12:02 +0200, Bryan Hunt 
> > <[EMAIL PROTECTED]>  wrote:
> >
> >> And it works fine, but really what I am trying to get here is the 
> >> value  of using Constants in
> >> both my Actions and my ( JSTL based ) jsp's.
> >
> >
> > You need to have all your Constants in a Map that is in application 
> > scope.  Kris Schneider posted this great piece of code to add to your 
> > Constants  file to return all your constants as a Map. At app start up 
> > I have a  servlet that does several things, one of which call the 
> > properties method  to put all the stuff in a Map and then you can just 
> > put that in scope:
> >
> >
> > //in some servlet at startup:
> > ServletContext context = contextEvent.getServletContext();
> > context.setAttribute("CONSTANTS", UIConstants.getConstantsMap());
> >
> >
> > (Below you don't need to do like I have. I had other reasons to do it 
> > this  way at the time. But just provide the getConstantFieldsAsMap() 
> > method )
> >
> > //example class: UIConstants
> >
> > private static Map constantsMap;
> > static {
> >     constantsMap = getConstantFieldsAsMap(UIConstants.class);
> > }
> >
> > public static Map getConstantsMap() {
> >     return constantsMap;
> > }
> >
> > //all your constants:
> > public final static String WHATEVER = "whatever";
> >
> > //this does the work.. thanks Kris
> > private static Map getConstantFieldsAsMap(Class cls) {
> >     Map propMap = null;
> >     try {
> >         Field[] allFields = cls.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);
> >                 log.debug("Putting name = " + name + " and value=" + 
> > value  + " into propMap");
> >                 propMap.put(name, value);
> >             }
> >         }
> >     } catch(IllegalAccessException ie) {
> >         log.error("Problem loading constantFieldsAsMap " + ie);
> >     }
> >     return Collections.unmodifiableMap(propMap);
> > }
> >
> > //end class
> >
> >
> > Now in JSP you can just do:
> >
> > <c:out value="${CONSTANTS.WHATEVER)"/>

-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

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

Reply via email to