Hello World tutorial JSP#

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

Prepare#

The View#

  • create a file called HelloWorldJsp.jsp in tutorial/WEB-INF/classes/org/ztemplates/tutorial/helloworld/jsp 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.

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

<html>
<h1>${message}</h1>
</html>

The View Model#

  • create a file called HelloWorldJsp.java in tutorial/WEB-INF/classes/org/ztemplates/tutorial/helloworld/jsp with the following content

package org.ztemplates.tutorial.helloworld.jsp;

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'

@ZRenderer(ZJspRenderer.class)
public class HelloWorldJsp
{

  //make a property called 'message' available to 
  //the rendering engine, in this case JSP
  @ZExpose
  public String getMessage()
  {
    return "Hello World!";
  }
}

The Controller#

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

package org.ztemplates.tutorial.helloworld.jsp;

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

@ZMatch("/helloworld/jsp")
public class HelloWorldJspAction
{
  public void after() throws Exception
  {
    ZTemplates.getServletService().render(new HelloWorldJsp());
  }
}

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/jsp/*.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.