updated for 2.4.0

Url Variable tutorial#

This tutorial shows how to use url-variables. Url variables are used to extract information from the url itself, not only from the url-parameters. They can be placed everywhere in the url definition, but be aware that changing the order of the url-variables in the definition also changes the order in which callbacks are called, as the url-processing order is left to right.

The View#

  • Create a file called UrlVariables.jsp in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlvariables with the following content. Note that the notation must be EL ${}. This has been enabled in the web.xml of the application in the <jsp-config> setting.

The View Model#

  • Create a file called UrlVariables.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlvariables with the following content.
As you can see this class is a passive data-holder.

package org.ztemplates.tutorial.urlvariables;

import org.ztemplates.render.ZExpose;
import org.ztemplates.render.ZRenderer;
import org.ztemplates.web.jsp.ZJspRenderer;

//this tells ztemplates to use JSP as a renderer
//by default the JSP template has the same name as
//the class but with extension '.jsp'

public class UrlVariables
  //make a property called 'message' available to the renderer
  final String message;

  public UrlVariables(String message)
    this.message = message;

The Controller#

  • Create a file UrlVariablesAction.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlvariables with the following content. Note the @ZMatch annotation. It defines a url-variable named message that will be assigned to the property message in the annotated bean.

package org.ztemplates.tutorial.urlvariables;

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

public class UrlVariablesAction
  //same name as in url, no getter/setter needed
  private String message;

  public void after() throws Exception
    UrlVariables pojo = new UrlVariables(message);

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

  • public void before() throws Exception
  • public void beforeMessage() throws Exception
  • public void afterMessage() throws Exception


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

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.