A sensible way to handle constants in a webapp is to define a bean that holds all of the constants as attributes.
However, Java programmers have a propensity for creating constants as classes that have the values defined static
final members. This tag exposes these attribute handles to save the developer from having to read the constants class
source code to determine the returned values. This way the JSP source and the backend bean (and/or bean handlers)
have a consistent interface to the constants class.
Under the hood, the tag uses reflection to build a map of names and values. The map is cached to save the expense
of repeated runtime reflection.
Usage: Suppose you have a class com.example.Constants:
public class Constants { public static final int WARMRESTART = 0; public static final int COLDRESTART = 1; public
static final int HARDRESTART = 2; public static final String RO = "rock"; public static final String SHAM = "paper";
public static final String BO = "scissors"; }
and you want to uniformly access the symbold names in the jsp as you would in your Java classes. Use this tag.
<%@ taglib uri="/WEB-INF/constants.tld" prefix="constants" %> ... someplace where a constant needs to be accessed:
Important: This usage assumes the class to access is specified in a
web-xml/context-param
The alternate form
can be used to
accomadate having multiple constants classes.
Another usage is set attributes, within an optionally specified scope
or just
to put it
in the page scope. Either way, at sometime later, you can do this