Class 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
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
bodyContentFields inherited from class jakarta.servlet.jsp.tagext.TagSupport
id, pageContextFields inherited from interface jakarta.servlet.jsp.tagext.BodyTag
EVAL_BODY_BUFFERED, EVAL_BODY_TAGFields inherited from interface jakarta.servlet.jsp.tagext.IterationTag
EVAL_BODY_AGAINFields inherited from interface jakarta.servlet.jsp.tagext.Tag
EVAL_BODY_INCLUDE, EVAL_PAGE, SKIP_BODY, SKIP_PAGE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintdoEndTag()doEnd tag handler required to fulfill the Tag interface defined in the JSP specification.intdoStart tag handler required to fulfill the Tag interface defined in the JSP specification.Backwards-compatibility for old taglibsGets the JSON format (e.g.getName()Gets the name of the javascript variable to generategetValue()Returns the object to convert into javascriptvoidsetDateFormat(String dateFormat) Backwards-compatibility for old taglibsvoidsetJsonFormat(String jsonFormat) Sets the JSON format (e.g.voidSets the name of the javascript variable to generatevoidSets the object to convert into javascriptMethods inherited from class jakarta.servlet.jsp.tagext.BodyTagSupport
doAfterBody, doInitBody, getBodyContent, getPreviousOut, release, setBodyContentMethods inherited from class jakarta.servlet.jsp.tagext.TagSupport
findAncestorWithClass, getId, getParent, getValue, getValues, removeValue, setId, setPageContext, setParent, setValueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.jsp.tagext.Tag
getParent, setPageContext, setParent
-
Constructor Details
-
SetJavascriptVarTag
public SetJavascriptVarTag()
-
-
Method Details
-
setName
Sets the name of the javascript variable to generate- Parameters:
name- the name of the javascript variable to generate
-
getName
Gets the name of the javascript variable to generate- Returns:
- the name of the javascript variable to generate
-
setValue
Sets the object to convert into javascript- Parameters:
value- the object to convert into javascript
-
getValue
Returns the object to convert into javascript- Returns:
- the object to convert into javascript
-
setJsonFormat
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
Backwards-compatibility for old taglibs- See Also:
-
getDateFormat
Backwards-compatibility for old taglibs- See Also:
-
doStartTag
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:
doStartTagin interfacejakarta.servlet.jsp.tagext.Tag- Overrides:
doStartTagin classjakarta.servlet.jsp.tagext.BodyTagSupport- Returns:
- BodyTag.SKIP_BODY
- Throws:
jakarta.servlet.jsp.JspException
-
doEndTag
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:
doEndTagin interfacejakarta.servlet.jsp.tagext.Tag- Overrides:
doEndTagin classjakarta.servlet.jsp.tagext.BodyTagSupport- Returns:
- BodyTag.EVAL_PAGE
- Throws:
jakarta.servlet.jsp.JspException
-