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.

Page

HTML

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">
<html>
<head>
  <title>This is the title</title>
  <meta http-equiv="Refresh-x" content="5">
</head>
<body>
<p align="center"><font size="+3" class="DynamicText">This is the text</font>
</body>
</html>

Dynamator File

DirectAccess.dyn
<dynamator language="jsp">
  <prolog>
    <%@ 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);
    %>
  </prolog>
  <class name="DynamicText">
    <content>
        ( count == 0 ) 
        ? getGreeting()
        : String.valueOf(count)
    </content>
  </class>
  <tag tag="title">
    <content>
        ( count == 0 ) 
        ? getGreeting()
        : String.valueOf(count)
    </content>
  </tag>
  <tag tag="meta" http-equiv="Refresh-x">
    <if>count > 0</if>
    <attr name="http-equiv">
      <raw-content>
        Refresh
      </raw-content>
    </attr>
    <attr name="content">
      <content>
        "1; URL=" + request.getRequestURI() + "?count=" + (count-1)
      </content>
    </attr>
  </tag>
  <tag tag="*" with-attrs="class">
    <attr name="class"><discard/></attr>
  </tag>
</dynamator>

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.