At line 3 added 1 line. |
Inspired by <a href="http://www.raibledesigns.com/page/rd?anchor=how_do_you_manage_your">this post</a>, I wrote a tag library to expose the fields of my Contants.java class to my JSPs. It allows a user to specify a single variable using: |
At line 5 added 23 lines. |
{{{ |
<appfuse:constants var="USER_KEY"/> |
}}} |
|
Or all variables: |
|
{{{ |
<appfuse:constants/> |
}}} |
|
By default, it exposes the values from the Contants class imported into the tag library, but also allows a className variable to specify a different class. The main reason I wrote this was to prove it was possible. The second reason was to get around importing my Contants class (and using <%=Contants.VARNAME%>). With this tag, both of the following work: |
|
{{{Scriptlet: <%=VARNAME%>, JSTL: <c:out value="${VARNAME}"/>}}} |
|
The only thing it gives you over the import/scriptlet way is to expose the attribute to different scopes - i.e. page for JSTL. After writing it, I think it's a fairly useless tag library - I don't know if I'll ever use it, but maybe someone else will find it useful. |
|
;:''BTW, you need to use it on the JSP you plan to use it in a scriptlet and you can't wrap it with oscache's <cache> tags.'' |
|
!Files Needed for Tag Library |
* [1] ConstantsTag.java |
* [2] ConstantsTei.java - exposes variables as attributes |
---- |
!!ConstantsTag.java [#1] |
At line 180 added 79 lines. |
|
---- |
!!ConstantsTei.java [#2] |
|
[{Java2HtmlPlugin |
|
package org.appfuse.webapp.taglib; |
|
import java.lang.reflect.AccessibleObject; |
import java.lang.reflect.Field; |
|
import java.util.ArrayList; |
import java.util.List; |
|
import javax.servlet.jsp.tagext.TagData; |
import javax.servlet.jsp.tagext.TagExtraInfo; |
import javax.servlet.jsp.tagext.VariableInfo; |
|
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
|
import org.appfuse.common.Constants; |
|
|
/** |
* Implementation of <code>TagExtraInfo</code> for the <b>constants</b> |
* tag, identifying the scripting object(s) to be made visible. |
* |
* @author Matt Raible |
* @version $Revision: 1.1 $ $Date: 2003/12/31 13:59:32 $ |
*/ |
public class ConstantsTei extends TagExtraInfo { |
private Log log = LogFactory.getLog(ConstantsTei.class); |
|
/** |
* Return information about the scripting variables to be created. |
*/ |
public VariableInfo[] getVariableInfo(TagData data) { |
// loop through and expose all attributes |
List vars = new ArrayList(); |
|
try { |
String clazz = data.getAttributeString("className"); |
|
if (clazz == null) { |
clazz = Constants.class.getName(); |
} |
|
Class c = Class.forName(clazz); |
|
// if no var specified, get all |
if (data.getAttributeString("var") == null) { |
Field[] fields = c.getDeclaredFields(); |
|
AccessibleObject.setAccessible(fields, true); |
|
for (int i = 0; i < fields.length; i++) { |
vars.add(new VariableInfo(fields[i].getName(), |
"java.lang.String", true, |
VariableInfo.AT_END)); |
} |
} else { |
String var = data.getAttributeString("var"); |
vars.add(new VariableInfo(c.getField(var).getName(), |
"java.lang.String", true, |
VariableInfo.AT_END)); |
} |
} catch (Exception cnf) { |
log.error(cnf.getMessage()); |
cnf.printStackTrace(); |
} |
|
return (VariableInfo[]) vars.toArray(new VariableInfo[] { }); |
} |
} |
|
}] |
|
Also see [my blog post|http://www.raibledesigns.com/page/rd?anchor=useless_tag_library]. |