And If you can't extend from that class then you can write adapter for that 
class. Something like this

Using in a JSP page:
[code]

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"; %>
<jsp:useBean id="Constants" class="com.utils.JSTLMyConstants"/>
<c:out value="${Constants.name <http://Constants.name>}"/><br>
<c:out value="${Constants.age}"/>
<c:forEach var="day" items="${Constants.daysOfTheWeek}">
<c:out value="${day}"/>
</c:forEach>
[/code]

Where adapter class is

[code]

package com.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
* Class to reveal java constants to JSTL Expression Language
* Uses reflection to scan the declared fields of a Constants class
* Adds these fields to the Map.
* Map is unmodifiable after initialization.
*/
public class JSTLMyConstants extends HashMap {

private boolean initialised = false;

private static final Class adaptee = MyConstant.class;

public JSTLMyConstants() {
Class c = adaptee;
Field[] fields = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {

Field field = fields[i];
int modifier = field.getModifiers();
if (Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier))
try {
this.put(field.getName(), field.get(this));
}
catch (IllegalAccessException e) {}
}
initialised = true;
}

public void clear() {
if (!initialised)
super.clear();
else
throw new UnsupportedOperationException("Cannot modify this map");
}

public Object put(Object key, Object value) {
if (!initialised)
return super.put(key, value);
else
throw new UnsupportedOperationException("Cannot modify this map");
}

public void putAll(Map m) {
if (!initialised)
super.putAll(m);
else
throw new UnsupportedOperationException("Cannot modify this map");
}

public Object remove(Object key) {
if (!initialised)
return super.remove(key);
else
throw new UnsupportedOperationException("Cannot modify this map");
}
}

[/code]

// Main constant class

[code]

package com.utils;

public interface MyConstant {

public static final int age = 30;
public static final String name = "Sudhaker Raj";
public static final String programmingSkill = "Awesome";

public static final String[] daysOfTheWeek = { "Sun", "Mon", "Tue", "Wed", 
"Thu", "Fri", "Sat", "Sun" };

}

[/code]

Hope this helps.


On 8/30/05, Luca Passani <[EMAIL PROTECTED]> wrote:
> 
> Sudhaker Raj wrote:
> 
> >You may want to see this -
> >
> >http://forums.java.sun.com/thread.jspa?threadID=508847&messageID=2543490
> >
> >
> very smart. A bit overkill for my problem, but very smart...
> 
> Thanks
> 
> Luca
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 
Cheers,
Sudhaker Raj
http://thej2ee.com

Reply via email to