
494
|
Chapter 12, Miscellany
#98 Add Velocity for Dynamic HTML
HACK
When you run Velocity with this context and this template, it will print out:
Jonathan is a total Rockstar
Create the HTML
You’ll probably want to create your pages in an HTML editor like Dream-
weaver. Better yet, have your graphics designers handle design, and then you
can add VTL tags to the HTML they create.
For each of the three days in the display, you need temperature, humidity,
pressure, and the name of the day. For each measurement, you need a vari-
able in VTL—call these
TEMP
,
HUMIDITY
, and
PRESSURE
—and preface them
with
DAY and the day number (like DAY1). Figure 12-14 is a screenshot from
Dreamweaver, where I built the page with all of the VTL. The dynamic data
for the day, temperature, humidity, and pressure are provided by VTL tags:
the first day’s name is
${DAY1}, its temperature is ${DAY1_TEMP}, etc.
Create a Data Object
Now, it’s time to leave template land and get into some Java code. You need
to write a data object to represent the weather for a particular day. You’ll
find this useful when you build up your context. To keep things simple,
Example 12-20 uses an immutable object.
Figure 12-14. Weather web page in Dreamweaver
Example 12-20. A simple weather data object
public class Weather {
private BigDecimal temperature;
private BigDecimal humidity;
private BigDecimal pressure;
private String day;