sunlabs.brazil.javascript
Class JavaScriptTemplate

java.lang.Object
  extended by sunlabs.brazil.template.Template
      extended by sunlabs.brazil.javascript.JavaScriptTemplate (view source)
All Implemented Interfaces:
TemplateInterface

public class JavaScriptTemplate
extends Template

The JavaScriptTemplate looks for each <server language="javascript"> (or <javascript>) tag in an HTML page and treats the following data up to the next </server> (or </javascript>) tag as a JavaScript script to evaluate.

The reason that JavaScript scripts are included in an HTML page is usually to generate dynamic, server-side content. After running this template, everything between and including the <server> and </server> (or <javascript> and </javascript> tags is replaced by all output written to the JavaScript standard output stream (if any).

All JavaScript fragments within a given page are evaluated in the same JavaScript interpreter. The JavaScript interpreter actually lives for the entire duration of this Template object, so the user can implement persistence across requests.

The following configuration parameters are used to initialize this template.

script
The name of the JavaScript script to evaluate when the interpreter is created. This script is only evaluated when the interp is created, not on every request. The variables prefix and server are set before this file is evaluated, and are references to the parameters passed to a handler init method.
root
The document root, if the script is a relative file name. If the "root" property under the template prefix is not found, the global "root" property is used. If the global "root" property is not found, the current directory is used.
debug
If this configuration parameter is present, this class replaces the <server> and </server> tags with comments, so the user can keep track of where the dynamically generated content is coming from by examining the comments in the resultant HTML document. By default, the <server> and </server> are completely eliminated from the HTML document rather than changed into comments.

Before evaluating each HTML document, this class sets variables in the JavaScript interpreter, which can be used to interact back with Java to do things like set the response headers:

request
Exposes the Request Java object. It is set anew at each request.
prefix
Exposes the handler prefix String.
server
Exposes the handler Server object.
If the attribute eval is present as an attribute, all constructs of the form ${...} are substituted before processing the script.

Here's a simple example of a JavaScript template:


 <html>
 <head>
 <title>JavaScript Example</title>
 </head>
 <body>
 <javascript>
 var s = "request=" + request;
 s += "<br>request.serverUrl is " + request.serverUrl();
 s += "<br>server hostName is " + server.hostName;
 s += "<br>prefix is " + prefix;
 s += "<br> <table>";
 
 // This is an example of using Java in JavaScript with LiveConnect
 var e = request.props.propertyNames();
 while (e.hasMoreElements()) {
   var prop = e.nextElement();
   s += "<tr><td>" + prop + "</td>";
   s += "<td>" + request.props.getProperty(prop) + "</td></tr>";
 }
 
 s += "</table>";
 
 // The last value computed or expressed is returned
 s;
 
 </javascript>
 </body>
 </html>
 


Field Summary
 
Fields inherited from class sunlabs.brazil.template.Template
debug
 
Constructor Summary
JavaScriptTemplate()
           
 
Method Summary
 boolean init(RewriteContext hr)
          Called at the beginning of each HTML document that this JavaScriptTemplate is asked to process.
 void tag_javascript(RewriteContext hr)
          Processes the <javascript> tag.
 void tag_server(RewriteContext hr)
          Processes the <server> tag.
 
Methods inherited from class sunlabs.brazil.template.Template
done
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaScriptTemplate

public JavaScriptTemplate()
Method Detail

init

public boolean init(RewriteContext hr)
Called at the beginning of each HTML document that this JavaScriptTemplate is asked to process.

The first time this method is called, the initialization script is sourced into the interpreter, based on the configuration properties in the Request

Specified by:
init in interface TemplateInterface
Overrides:
init in class Template
Parameters:
hr - The request and associated HTML document that will be processed.
Returns:
true.

tag_server

public void tag_server(RewriteContext hr)
Processes the <server> tag. Substitutes the result of evaluating the following JavaScript script into the resultant HTML document.

Note: Currently, there is no mechanism for other language interpreters to share the same server tag.

Parameters:
hr - The request and associated HTML document that will be processed.

tag_javascript

public void tag_javascript(RewriteContext hr)
Processes the <javascript> tag. Substitutes the result of evaluating the following JavaScript script into the resultant HTML document.

Parameters:
hr - The request and associated HTML document that will be processed.

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.