Create a data model

In simple cases you can build data models using java.lang and java.util classes and custom Java Beans:

For example, let's build the data model of the first example of the Designer's Guide. For convenience, here it is again:

(root)
  |
  +- user = "Big Joe"
  |
  +- latestProduct
      |
      +- url = "products/greenmouse.html"
      |
      +- name = "green mouse"  

This is the Java code fragment that builds this data model:

// Create the root hash
Map root = new HashMap();
// Put string ``user'' into the root
root.put("user", "Big Joe");
// Create the hash for ``latestProduct''
Map latest = new HashMap();
// and put it into the root
root.put("latestProduct", latest);
// put ``url'' and ``name'' into latest
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");  

For the latestProduct you migh as well use a Java Bean that has url and name properties (that is, an object that has public String getURL() and String getName() methods); it's the same from viewpoint of the template.


Page generated: 2006-03-15 13:49:01 GMT FreeMarker Manual -- For FreeMarker 2.3.6