Java Tutorial - Java Script :
Creating the Tea Template:
Tea provides an excellent Integrated Development Environment (IDE) with which to manage the development of Tea templates, called the Kettle IDE. Kettle requires Java 1.3 or greater (even though Kettle runs on Linux, Windows is the preferred operating environment). Figure 6.4 shows the Kettle IDE. We start Kettle by executing the following command:
java -cp Kettle.jar com.go.kettle.Kettle
You will need to have Tomcat running with the TeaServlet installed in order to make effective use of the Kettle IDE.
Create a new project by selecting File ➪ New Project. Enter the name HelloMyCity for the project, and make sure it is in the following directory:
TOMCAT_ROOT/webapps/tea/WEB-INF/templates
After the project has been created, use Configure ➪ Project Properties, and make sure to select Link Project to Tea Servlet and enter the URL for the TeaServlet Administration Console. This URL should be the same one you used to test the servlet earlier and is repeated below for convenience:
Next, create a new template by selecting File ➪ New, and enter HelloMy City for the template name. Enter the code shown below:
<% template HelloMyCity(String myCity) %>
<html>
<head>
<title>Hello My City Template</title>
</head>
<body>
<% myCity = getMyCity() %>
<h1>Hello <% myCity %>!</h1>
</body>
</html>
You will notice code regions within the template that start with “<%” and terminate with “%>”. Within these code regions are where functions are invoked and logical operations are performed. All Tea templates must have a
template declaration (within the code region), and the declaration must be the same name as the template file. The template declaration in our example is:
<% template HelloMyCity(String myCity) %>
The template file is saved as HelloMyCity.tea. This particular template accepts a single parameter, myCity, which can be defined in the URL when invoking the template. Two other code regions appear in our example, <% myCity=getMyCity() %> and <% myCity %>. The <%myCity =getMy City () %> code region invokes the user-defined function that’s defined in the Context class created earlier. The value returned is assigned to the variable myCity. In our case, the code for the Context class examines the request to see if a city was provided as a parameter, and if not, supplies the string “Fort Myers ”. The <% myCity %> code region tells the template to insert the value assigned to this variable into the template’s output. Anything outside of these code regions is considered the text region, and is output by the template in the servlet response without modification.
The template can be compiled using Compile ➪ Template. If everything was entered correctly, Kettle should compile the template and output the compiled template class file into the templateClasses directory. This directory was created when we installed the servlet and configured in the TeaServlet .parameters file.
