<< 浅谈Linux内存管理机制 - 51CTO.COM | 首页 | 中国最美的地方排行榜 我一个都没去过!! >>

How to access Constant from a constant class in JSTL(JSP page) (Struts forum at JavaRanch)

Scriptlet is pedestrian. Make sure that you are using jsp 2.x and add a taglib with a function like the one below. It will get any object type, not just strings, and could be modified to include dot notation for any constants class. You can make a custom functions TLD once and reuse it for every project you ever do in the future. As a final note, if this constants class is for actual user viewable content, not keys and the like, use a localizable resource file and jstls fmt:message instead.

/**
* usage: ${yourprefix:constant('constantName')}
*/
public static Object getConstant(String constantName)
{
try
{
Field field = Constants.class.getDeclaredField(constantName);
return field.get(null);
}
catch (Exception e)
{
return null;
}
}

in your tld:
<function>
<name>constant</name>
<function-class>gov.fema.web.nimcast.tools.taglib.Functions</function-class>
<function-signature>Object getConstant(java.lang.String)</function-signature>
</function>

阅读全文……

标签 :



发表评论 发送引用通报