This following assumes that in your JSPs, you include the OpenCms tag library as follows:
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms"%>
When the URL of an incoming request contains the request parameter "__json=true
", JSON conversion is activated for this request. This means that instead of rendering the page by default, OpenCms produces JSON output that contains selected sections of the page's HTML as JSON values.
The <cms;jsonpart>
tag can be used to control which parts of the JSP output should be used as JSON values, and the element
attribute of the tag is used to control the JSON key for the value produced from the tag's body.
For example, the output of the JSP code:
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms"%>
lorem ipsum<
<cms:jsonpart element="part1">this is the first part</cms:jsonpart>
lorem ipsum
<cms:jsonpart element="part2">this is the second part</cms:jsonpart>
will, when in JSON conversion mode, be converted to the (approximate) JSON output:
{
"part1": "this is the first part",
"part2": "this is the second part",
"parts": ["part1", "part2"]
}
(Note that spacing/newlines have been adjusted in this example to increase readability. The actual output will look more messy.)
When not in JSON conversion mode, the <cms:jsonpart>
tag will leave its content's output unchanged, i.e. the output will be the same as if the tags weren't there.
In JSON conversion mode, everything except the parts of the page enclosed by these tags is thrown away, and the response text will consist of a single JSON object with the key-value pairs described above.
An additional key-value pair with the key "parts" is added to this object, which contains an array of the other key names, in the order they were processed while rendering the original page.<cms:jsonpart>
tags may not be nested either directly in the same JSP file, or indirectly, i.e. by including a JSP file using <cms:jsonpart>
inside the body of another <cms:jsonpart>
tag.
Note that the tag does not require that the output of its content is valid HTML; it is your responsibility as a developer to ensure this if the application consuming the JSON output requires valid HTML as JSON values.