StringTemplate Lab

In this lab, you will modify the page infrastructure provided in the StringTemplate lecture to add another page and to develop another site skin.

Getting started

First, compile and get the lecture code to work. That is,

  1. Get ANTLR and StringTemplate into your CLASSPATH
  2. Download the source code
  3. Compile it
  4. Start the server
  5. See you can get URL localhost:8080/mail/users to respond

Add a page

Create another page called EMailListPage that displays email headers stored as fields of EMail objects.

public class EMail {
    public String from;
    public String to;
    public String subject;
    public String content;
    public EMail(String from, String to, String subject,
                 String content)
    {
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.content = content;
    }
}

Like the UserListPage, create a simulated database in your EMailListPage:

/** Our simulated database */
static EMail[] users = new EMail[] {
    new EMail("parrt", "tombu", "parking issues", "no space!");
    ...
};

Modify the DispatchServlet so that it maps /mail/email to your new page.

Use only fields from, to, and subject. Put them as columns in a 3-column table. Each row will be a new email message:

<table>
<tr>
<td>From</td><td>To</td><td>Subject</td>
</tr>
<tr>
<td>parrt</td><td>tombu</td><td>parking issues</td>
</tr>
...
</table>

You will need to create a new page template email_page.st in the templates subdirectory and have EMailListPage load an instance of it. The body() method will return a StringTemplate instance of email_page.st once it fills in the data.

To fill the body template with data, simply set an attribute perhaps called email with the simulated database of email.

New skin

  1. Create another subdirectory called templates2 that has a copy of each template. Modify the Page class so that it loads from this new directory.
  2. Recompile and restart the server
  3. Reload the /mail/users and /mail/email pages to make sure they are working
  4. Alter the page.st template in some way and then reload both pages without touching the java code, recompiling, or restarting the server. You should see your changes in the generated code.