updated for 2.4.0

Callbacks on Url Variables#

The callbacks are the active part of a ztemplates application. The view-logic goes there.

This tutorial shows how to create callbacks on urls. It creates and displays a trace of the callback-method calls and the action-handler state at callback-time.

Prepare#

The View#

  • Create a file called UrlCallbacks.vm in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlcallbacks with the following content.

<html>

<ol>
#foreach($item in $list)
    <li>$item</li>
#end
</ol>

</html>

The View Model#

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

package org.ztemplates.tutorial.urlcallbacks;

import java.util.List;
import java.util.ArrayList;

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


@ZRenderer(ZVelocityRenderer.class)
public class UrlCallbacks
{
  @ZExpose
  final List<String> list;
    
  public UrlCallbacks(List list)
  {
    this.list = list;
  }
}

The Controller#

  • Create a file UrlCallbacksAction.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlcallbacks with the following content. Note the @ZMatch annotation. It defines 2 url-variables named var1 and var2 that will be assigned to the properties var1 and var2 in the annotated bean. Before and after doing that, callbacks will be called.

package org.ztemplates.tutorial.urlcallbacks;

import java.util.List;
import java.util.ArrayList;

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

@ZMatch("/urlcallbacks/${var1}/${var2}")
public class UrlCallbacksAction
{   
  private List<String> list = new ArrayList<String>();
  private String var1;
  private String var2;
  
  public void before() throws Exception
  {
    list.add("before var1=" + var1 + " var2=" + var2);
  }


  public void beforeVar1() throws Exception
  {
    list.add("beforeVar1 var1=" + var1 + " var2=" + var2);
  }
  
  public void afterVar1() throws Exception
  {
    list.add("afterVar1 var1=" + var1 + " var2=" + var2);
  }
  
  public void beforeVar2() throws Exception
  {
    list.add("beforeVar2 var1=" + var1 + " var2=" + var2);
  }
  
  public void afterVar2() throws Exception
  {
    list.add("afterVar2 var1=" + var1 + " var2=" + var2);
  }

  public void after() throws Exception
  {
    list.add("after var1=" + var1 + " var2=" + var2);
    UrlCallbacks pojo = new UrlCallbacks(list);
    ZTemplates.getServletService().render(pojo);
  }
}

Build#

  • open a command window and change to the tutorial/WEB-INF/classes directory
  • compile
javac -cp ../lib/ztemplates.jar tutorial/org/ztemplates/urlcallbacks/*.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.