2

File 1 : Constants.java

package com.sdl.webapp.main.common;


public class Constants {
    public static final String LARGE = "Large";
}

File 2 : demo.jsp

<jsp:useBean id="entity" type="com.sdl.modules.model.common.GenericComponentCollectionModel" scope="request"/>
<jsp:useBean id="cons" type="com.sdl.webapp.main.common.Constants" scope="request"/>

${cons.LARGE}

[3/21/17 11:53:24:125 EDT] 00000076 SystemOut O 11:53:24.122 [WebContainer : 2] ERROR c.s.w.c.impl.taglib.dxa.EntitiesTag - Error while processing entities tag com.ibm.websphere.servlet.error.ServletErrorReport: java.lang.InstantiationException: bean cons not found within scope

How do I handle String constants in JSP in the DXA framework ?

We are using the Java MVC framework, DXA 1.5 and SDL Tridion 2013

3

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.