Generating Elements Dynamically

Example 17-10 contains a JSP standard action that you have not seen in previous examples, combined with a couple of actions that are more familiar:

    <jsp:element name="${param.element}">
      <jsp:attribute name="style">${param.style}</jsp:attribute>
      <jsp:body>${param.body}</jsp:body>
    </jsp:element>

The <jsp:element> action generates a new XML element in the response with the name specified by the name attribute. The <jsp:attribute> and <jsp:body> elements perform the same function as in previous chapters—adding an attribute and a body to the element, respectively. Together, these standard actions allow you to create any XML element, with any number of attributes and a body based on the evaluation result of their bodies, while still keeping the JSP Document as a well-formed XML document. For instance, if the page in Example 17-10 receives the parameters element with the value p, style with the value font-weight:bold, and body with the value Hello+World, this element is generated:

<p style="font-weight:bold">Hello World</p>

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.