Java Web Application Development With Click Framework/Velocity

From Wikibooks, open books for an open world
< Java Web Application Development With Click Framework
Jump to: navigation, search

Velocity is a Java-based template framework which provides the HTML rending engine for Click. Velocity is very easy to learn, simple to use, and is also very capable.

Velocity is an open source Apache project licensed using the [LICENSE.txt Apache] Software License.

Velocity's comprehensive online documentation is included in this distribution.

  • Velocity user's guide
  • Velocity developer's configuration guide
  • Velocity Template Language (VTL) reference guide

Object References[edit]

With Click you add objects to your page template using the Page's model. You can then access these objects using Velocity's $ reference notation. By default the ClickServlet adds the following objects to page templates:

  • any public Page fields using the field's name
  • context - the Servlet context path, e.g. /mycorp
  • cssImports - the CSS imports and style blocks to include in the page's header.
  • format - the Format object for formatting the display of objects.
  • imports - the CSS and JavaScript imports to include in the page's header.
  • jsImports - the JavaScript imports and script blocks to include in the page's footer.
  • messages - the MessagesMap adapter for the Page getMessage() method.
  • path - the path of the page template to render.
  • request - the page's HttpServletRequest object.
  • response - the page's HttpServletResponse object.
  • session - the SessionMap adapter for the user's HttpSession.

In your page templates you can access object references using a dot path notation. For example:

Welcome: $[../click-api/net/sf/click/util/SessionMap.html session].user.fullName

Or by using a more explicit Java notation:

Welcome: $[../click-api/net/sf/click/util/SessionMap.html session].get("user").getFullName()

Directives[edit]

In Velocity directives are prefixed by a # symbol. While references access stuff, directives do stuff. The full set of Velocity directives are:

  • #if #elseif #else - output conditional on truth of statements.
  • #foreach - loops through a list of objects.
  • #macro - allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required.
  • #set - establishes the value of a reference.
  • #include - renders local file(s) that are not parsed by Velocity.
  • #parse - renders a local template that is parsed by Velocity.
  • #stop - stops the template engine.