updated for 2.4.0

Hello World tutorial Velocity#

This tutorial shows how to send the standard "Hello World" from the web-server to the web-browser with ztemplates and Apache Velocity.

Prepare#

  • First create empty application
  • Enable velocity in WEB-INF/web.xml by uncommenting the Velocity context listener. (Should be already uncommented, but just in case...)

The View#

  • create a file called HelloWorldVelocity.vm in tutorial/WEB-INF/classes/org/ztemplates/tutorial/helloworld/velocity with the following content. Note that although Velocity supports $message it is better to be as compatible with JSP-EL as possible, so we use ${message}. In fact the template could be used without changes by the JSP renderer.
<html>
<h1>${message}</h1>
</html>

If you want to put the template somewhere else, you can use the @ZTemplate annotation.

The View Model#

  • create a file called HelloWorldVelocity.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/helloworld/velocity with the following content. Note the @ZRenderer annotation.

package org.ztemplates.tutorial.helloworld.velocity;

import org.ztemplates.render.ZExpose;
import org.ztemplates.render.ZRenderer;
import org.ztemplates.render.velocity.ZVelocityRenderer;

//this tells ztemplates to use velocity as a renderer
//by default the velocity template has the same name as
//the class but with extension '.vm'
@ZRenderer(ZVelocityRenderer.class)
public class HelloWorldVelocity
{
  //make a property called 'message' available to 
  //the rendering engine, in this case velocity
  @ZExpose
  final String message;

  public HelloWorldVelocity()  
  {
    message = "Hello World!";
  }
}

The Controller#

  • create a file HelloWorldVelocityAction.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/helloworld/velocity with the following content. Note the @ZMatch annotation.

package org.ztemplates.tutorial.helloworld.velocity;

import org.ztemplates.actions.ZMatch;
import org.ztemplates.web.ZTemplates;

@ZMatch("/helloworld/velocity")
public class HelloWorldVelocityAction implements ZIAction
{
  public void after() throws Exception
  {
    ZTemplates.getServletService().render(new HelloWorldVelocity());
  }
}

In the controller we could also have defined the following callbacks:

  • public void before() throws Exception

Build#

  • open a command window and change to the tutorial/WEB-INF/classes directory
  • compile
javac -cp ../lib/ztemplates.jar org/ztemplates/tutorial/helloworld/velocity/*.java

Run#

Neuen Anhang hinzuf�gen

Du bist nicht autorisiert, Anh�nge zu dieser Seite hochzuladen.
« Diese Seite (Version-) wurde zuletzt am 22-Dez-2013 13:18 von gerdziegler.de ge�ndert.