logo
Support This Project

Url Reference tutorial

This tutorial shows how to use url-references.

Url references are used to include other url-definitions in the current url, so you can encapsulate and reuse behavior (callbacks) between action-pojos.

For details on url-references, please read this.

Url-references can be placed everywhere in the url definition, but be aware that changing the order of the url-references in the url 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 UrlReferences.jsp in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlreferences 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.
<html>
<h1>${message}</h1>
</html>

The View Model

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

package org.ztemplates.tutorial.urlreferences;

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

@ZRenderer(ZJspRenderer.class)
public class UrlReferences
{
   private String message;
    
  @ZExpose
  public String getMessage()
  {
    return message;
  }
  
  public void setMessage(String message
  {
    this.message = message;
  }
}

The interface

  • Create a file IContent.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlreferences with the following content.

package org.ztemplates.tutorial.urlreferences;

public interface IContent
{
    public String getMessage();
}

The Controller

  • Create a file UrlReferencesAction.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlreferences with the following content. Note the @ZMatch annotation. It defines a url-reference named content of type interface IContent that will be instantiated with the right implementation depending on the url and assigned to the bean property.

package org.ztemplates.tutorial.urlreferences;

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

@ZMatch("/urlreferences/#{content}")
public class UrlReferencesAction
{
  private IContent content;

  public IContent getContent()
  {
    return content;
  }
  
  public void setContent(IContent content
  {
    this.content = content;
  }

  public void after() throws Exception
  {
    UrlReferences pojo = new UrlReferences();
    pojo.setMessage(content.getMessage() " from " + content.getClass().getName());
    ZTemplates.getServletService().render(pojo);
  }
}

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

  • public void before() throws Exception
  • public void initContent(IContent content) throws Exception this callback is available only for url-references
  • public void beforeContent() throws Exception
  • public void afterContent() throws Exception

The first nested controller implementation

This one matches content1/...

  • Create a file ContentAction1.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlreferences with the following content.

package org.ztemplates.tutorial.urlreferences;

import org.ztemplates.actions.ZMatch;

@ZMatch("content1/${message}")
public class ContentAction1 implements IContent
{
  private String message;

  public String getMessage()
  {
    return message;
  }
  
  public void setMessage(String message
  {
    this.message = 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
  • public void after() throws Exception

The second nested controller implementation

This one matches content2/...

  • Create a file ContentAction2.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/urlreferences with the following content.

package org.ztemplates.tutorial.urlreferences;

import org.ztemplates.actions.ZMatch;

@ZMatch("content2/${message}")
public class ContentAction2 implements IContent
{
  private String message;

  public String getMessage()
  {
    return message;
  }
  
  public void setMessage(String message
  {
    this.message = 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
  • public void after() 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/urlreferences/*.java

As you can see the framework instantiates the right implementation depending on the url!

Explanation

Url /urlreferences/content1/test_message_for_controller_1

  • UrlReferencesAction is instantiated because it matches /urlreferences/...
  • content1/test_message_for_controller_1 instantiates and assigns a ContentAction1 instance because ContentAction1 matches "content1/${message}". test_message_for_controller_1 is assigned to the message-property. The ContentAction1 instance is assigned to UrlReferencesAction.content, then the after() callback is executed.

Url /urlreferences/content2/test_message_for_controller_2 would match ContentAction2

  My Prefs Log in
This page (revision-) last changed by gerdziegler.de.
 
(c) 2007 gerdziegler.de
JSPWiki v2.4.69