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 Template Author'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 might 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. Similarly, the root could be a Java Bean with user and lastestProduct properties.

FreeMarker Manual -- For FreeMarker 2.3.20
HTML generated: 2013-06-27 20:54:33 GMT
Edited with XMLMind XML Editor
Here!