Class SetJavascriptVarTag

java.lang.Object
jakarta.servlet.jsp.tagext.TagSupport
jakarta.servlet.jsp.tagext.BodyTagSupport
com.randomnoun.common.webapp.taglib.SetJavascriptVarTag
All Implemented Interfaces:
jakarta.servlet.jsp.tagext.BodyTag, jakarta.servlet.jsp.tagext.IterationTag, jakarta.servlet.jsp.tagext.JspTag, jakarta.servlet.jsp.tagext.Tag, Serializable

public class SetJavascriptVarTag extends jakarta.servlet.jsp.tagext.BodyTagSupport
Sets a javascript variable from a server-side resource or request attribute. The variable may contain any amount of maps or lists, which will be converted into the javascript equivalent.

Attributes defined for this tag (in common.tld) are:

  • name - the name of the javascript variable to define
  • value - the value of the javascript variable.

Both name and value may contain EL-style expressions.

e.g. the following JSP snippet (taken from an early version of messageList.jsp) retrieves the 'columns' List from a request attribute, and sets it to a javascript variable of the same name:

             var columns = new Array();
             <c:forEach var="i" varStatus="rowStatus" items="${columns}" >
               columns[<c:out value='${rowStatus.index}'/>] = {
                 visible: <c:out value='${i.visible}'/>,
                 name: "<c:out value='${i.name}'/>",
                 width: <c:out value='${i.width}'/> };
             </c:forEach>
             

... which produces output of the form:

             columns[0] = {
               visible: true,
               name: "externalMessageType",
               width: 91 };
             columns[1] = {
               visible: true,
               name: "queue",
               width: 89 };
             ...
             

This JSP snippet can be reproduced with the following tag:

             <mm:setJavascriptVar name="columns" value="${columns}" />
             

... which produces the slightly more terse, but functionally equivalent:

             var columns = [{name: "externalMessageType",visible: true,width: 91}
               ,{name: "queue",visible: true,width: 89}
               ...
             ]
             ;
             
This tag also translates arbitrary levels of maps and lists within objects passed through to Javascript,

e.g. this code sets a javascript variable 'x' to the value of the 'y' request attribute

            <mm:setJavascriptVar name="x" value="${y}" />
            

For a reasonably complex 'y', this would generate the following HTML-embedded Javascript:

            var x = ['list-string-element-1', 'list-string-element2',
                     (key1: value1), (key2:value2),
                      1234, 4321 ]
            

The example above shows how string, map and numeric elements are represented within a list object.

Author:
knoxg
See Also:
  • Field Summary

    Fields inherited from class jakarta.servlet.jsp.tagext.BodyTagSupport

    bodyContent

    Fields inherited from class jakarta.servlet.jsp.tagext.TagSupport

    id, pageContext

    Fields inherited from interface jakarta.servlet.jsp.tagext.BodyTag

    EVAL_BODY_BUFFERED, EVAL_BODY_TAG

    Fields inherited from interface jakarta.servlet.jsp.tagext.IterationTag

    EVAL_BODY_AGAIN

    Fields inherited from interface jakarta.servlet.jsp.tagext.Tag

    EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    doEnd tag handler required to fulfill the Tag interface defined in the JSP specification.
    int
    doStart tag handler required to fulfill the Tag interface defined in the JSP specification.
    Backwards-compatibility for old taglibs
    Gets the JSON format (e.g.
    Gets the name of the javascript variable to generate
    Returns the object to convert into javascript
    void
    setDateFormat(String dateFormat)
    Backwards-compatibility for old taglibs
    void
    setJsonFormat(String jsonFormat)
    Sets the JSON format (e.g.
    void
    Sets the name of the javascript variable to generate
    void
    Sets the object to convert into javascript

    Methods inherited from class jakarta.servlet.jsp.tagext.BodyTagSupport

    doAfterBody, doInitBody, getBodyContent, getPreviousOut, release, setBodyContent

    Methods inherited from class jakarta.servlet.jsp.tagext.TagSupport

    findAncestorWithClass, getId, getParent, getValue, getValues, removeValue, setId, setPageContext, setParent, setValue

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface jakarta.servlet.jsp.tagext.Tag

    getParent, setPageContext, setParent
  • Constructor Details

  • Method Details

    • setName

      public void setName(String name)
      Sets the name of the javascript variable to generate
      Parameters:
      name - the name of the javascript variable to generate
    • getName

      public String getName()
      Gets the name of the javascript variable to generate
      Returns:
      the name of the javascript variable to generate
    • setValue

      public void setValue(Object value)
      Sets the object to convert into javascript
      Parameters:
      value - the object to convert into javascript
    • getValue

      public Object getValue()
      Returns the object to convert into javascript
      Returns:
      the object to convert into javascript
    • setJsonFormat

      public void setJsonFormat(String jsonFormat)
      Sets the JSON format (e.g. method in which dates are serialised to JSON)
      Parameters:
      name - the JSON format
    • getJsonFormat

      Gets the JSON format (e.g. method in which dates are serialised to JSON)
      Returns:
      the JSON format
    • setDateFormat

      public void setDateFormat(String dateFormat)
      Backwards-compatibility for old taglibs
      See Also:
    • getDateFormat

      Backwards-compatibility for old taglibs
      See Also:
    • doStartTag

      public int doStartTag() throws jakarta.servlet.jsp.JspException
      doStart tag handler required to fulfill the Tag interface defined in the JSP specification. This tag is always empty, and therefore must always return BodyTag.SKIP_BODY
      Specified by:
      doStartTag in interface jakarta.servlet.jsp.tagext.Tag
      Overrides:
      doStartTag in class jakarta.servlet.jsp.tagext.BodyTagSupport
      Returns:
      BodyTag.SKIP_BODY
      Throws:
      jakarta.servlet.jsp.JspException
    • doEndTag

      public int doEndTag() throws jakarta.servlet.jsp.JspException
      doEnd tag handler required to fulfill the Tag interface defined in the JSP specification.

      This method does nothing, and always returns BodyTag.EVAL_PAGE

      Specified by:
      doEndTag in interface jakarta.servlet.jsp.tagext.Tag
      Overrides:
      doEndTag in class jakarta.servlet.jsp.tagext.BodyTagSupport
      Returns:
      BodyTag.EVAL_PAGE
      Throws:
      jakarta.servlet.jsp.JspException