Dynamator Pure HTML for every page generation technology.

Applying behavior to elements without markers

So far, we've seen how to apply behavior to HTML elements identified using the 'id' and 'class' attributes. This lesson shows how to apply behavior to any HTML element.

Some HTML elements aren't supposed to have 'id' or 'class' attributes. Of these elements, the ones that most frequently need server-side behavior are <meta>, <title>, and <body>.

This example extends the original Hello World file by making the title element dynamic, and adding a page refresh. It demonstrates the two ways to directly address elements in an HTML page: by element name, and by a combination of element name and attribute values.



The HTML page uses a non-standard http-equiv attribute value, "Refresh-x", instead of "Refresh", to prevent this tutorial page from refreshing.

DirectAccess.html (updated)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <title>This is the title</title>
  <meta http-equiv="Refresh-x" content="5">
<p align="center"><font size="+3" class="DynamicText">This is the text</font>

Dynamator File

<dynamator language="jsp">
    <%@ page session="false" %>
        private String getGreeting()
            return "Hello World, says Dynamator!";
        String countArgument = request.getParameter("count");
        int count = 
            ( countArgument == null || countArgument.length() == 0 )
            ? 5
            : Integer.parseInt(countArgument);
  <class name="DynamicText">
        ( count == 0 ) 
        ? getGreeting()
        : String.valueOf(count)
  <tag tag="title">
        ( count == 0 ) 
        ? getGreeting()
        : String.valueOf(count)
  <tag tag="meta" http-equiv="Refresh-x">
    <if>count > 0</if>
    <attr name="http-equiv">
    <attr name="content">
        "1; URL=" + request.getRequestURI() + "?count=" + (count-1)
  <tag tag="*" with-attrs="class">
    <attr name="class"><discard/></attr>

We've made some changes to the Dynamator file:

The file contains a new element, <tag>. The 'tag' element references all template elements having a name matching the 'tag' attribute. So in the first use of the 'tag' element, the element '<tag tag="title">' references the HTML element named '<title>'.

The second example of the 'tag' element demonstrates the ability to select a set of elements by tagname and attribute values. When Dynamator sees a 'tag' element with attributes in addition to 'tag', it looks for elements in the HTML file that have those same attribute specifications. So in the second use of the 'tag' element, the element '<tag tag="meta" http-equiv="Refresh-x">' references only the HTML element matching the name and attribute '<meta http-equiv="Refresh-x">'.

The third example of the 'tag' element demonstrates two additional capabilities. Using tag="*" causes a tag element to refer to any template element rather than elements with a specific name. The 'with-attrs' attribute allows elements to be selected that contain an attribute without regard for the attribute value. It contains a space-separated list of attribute names. This third use of 'tag' removes all 'class' attributes from the output file.

Note that in this case, more than one locator is applied to the same template element. The font element is modified by both the dynamator class entry and the last tag entry.

Finally, to keep from being bored, we've added to the prolog a declaration of a variable to hold a counter, and made the page display a countdown.

You may have noticed that the <if> element contains an XML special character, '>'. This is not valid XML, but Dynamator allows it for ease of use. (Valid XML would require <if><![CDATA[count > 0]]></if>.) Dynamator treats each Dynamator element content that may contain program code as CDATA.

JSP file

After processing with Dynamator, the resulting JSP file looks like this:

DirectAccess.jsp (generated by Dynamator)

In Action

If you are viewing this page in a servlet engine, you can see the generated page in action.