The JSP EL (Expression Language) makes no provisions for accessing class constant values, so there is no means to easily replace the scriptlet expressions that reference the class constants representing the element names, with scriptless EL expressions.
As mentioned in the following article http://www.javaranch.com/journal/200601/Journal200601.jsp#a3, since the EL does not provide any mechanism to allow class constant references on the pages, you will have to use some "Java voodoo" to do it yourselves. You can apparently do that with Java reflection, and using custom tags.
You will need a TLD entry for our custom action, something like:
<tag>
<name>setConstant</name>
<tag-class>org.bibeault.jrjournal.ccc.SetConstantTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>constant</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
And then a tag implementation:
public class SetConstantTag extends SimpleTagSupport {
private String fieldName;
private String varName;
private String scopeName;
public void setConstant(String value) { this.fieldName = value; }
public void setVar(String value) { this.varName = value; }
public void setScope(String value) { this.scopeName = value; }
public void doTag() throws JspException {
try {
ScopedContext scopedContext = (this.scopeName == null) ? ScopedContext.PAGE : ScopedContext.getInstance( this.scopeName );
Object constantValue = ClassConstantInspector.getValue(this.fieldName);
getJspContext().setAttribute(this.varName, constantValue, scopedContext.getValue());
}
catch (Exception e) {
throw new JspException("Exception setting constant " + this.fieldName, e);
}
}
}
Then you can use it as follows in your JSP:
<%@ taglib uri="http://bibeault.org/tld/ccc" prefix="ccc" %>
<jsp:useBean id="entity" type="com.sdl.modules.model.common.GenericComponentCollectionModel" scope="request"/>
<ccc:setConstant constant="com.sdl.webapp.main.common.Constants.LARGE" var="constLarge"/>
${constLarge}
For mode details and the example code, see the above mentioned article and
the ccc.jar
file referenced in the Resources section.