FirstStep

The first page

The very first page will be a simple view. A view item is supposed to return the HTML content as string. This can be generated by velocity or freemarker or whatever. But as a start we will just generate the HTML in our java class.

First open the pages.xml located in the etc directory. This file contains all page definitions. Put in the following:

<?xml version="1.0" ?>
<application>  
       
    <page name="index">
      <view>org.jzonic.demo.views.HelloWorld</view>            
    </page>
    
</application>


Now create a class called HelloWorld.java in the src/org/jzonic/demo/views directory. If the directory does not exist so far then create it.
Every view must implement the org.jzonic.core.View interface. The class looks like this:

package org.jzonic.demo.views;

import org.jzonic.core.*;

public class HelloWorld implements View {
        
    public HelloWorld() {
    }
    
    public String generate(WebContext webContext) throws ViewException {
        return "Hello world";
    }
    
}


There is only one method that you need to implement. Now save it and run “ant”. This will generate a demo.war file in the dist directory. Deploy this file and point your browser to:
http://localhost:8080/demo/index.jz
You will see:


The second page

Since generating HTML in Java is a bad idea we will now write a view that will use velocity as template engine. There is a helper class in jZonic that reduces the overhead of using velocity.
First we will create the new page in the pages.xml:

<?xml version="1.0" ?>
<application>  
       
    <page name="index">
      <view>org.jzonic.demo.views.HelloWorld</view>            
    </page>
    
    <page name="velocity">
      <view>org.jzonic.demo.views.VelocityView</view>            
    </page>
                  
</application>


Now we create the VelocityView.java file in the same directory as the one class above.
Here is the code:

package org.jzonic.demo.views;

import org.jzonic.core.*;
import org.apache.velocity.VelocityContext;
import org.jzonic.util.VelocityUtil;

public class VelocityView implements View {
        
    public VelocityView() {
    }
    
    public String generate(WebContext webContext) throws ViewException {
        try {
            VelocityContext ctx = new VelocityContext();  
            String[] colors = {"red","white","yellow","black","green"};
            ctx.put("colors",colors);
            return VelocityUtil.transform("colors.vm", ctx);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new ViewException("Error while processing this view:"+e.getMessage());
        }
    }    
}


The necessary jar file for velocity is already in this distribution. The basic steps are to create a new VelocityContext. Then put all objects inside the context. The last step is to use the org.jzonic.util.VelocityUtil which has a helper method to generate the HTML.
In order to see something we need to create a file called “colors.vm” in the web directory.
The file looks like this:

< HTML>
< BODY>
< H1>All colors</ H1>
< UL>
#foreach ($color in $colors )
< LI>$color</ LI>
#end
</ UL>
</ BODY>
</ HTML>

Note: The space inside every HTML tag is needed otherwise this wiki system will erase them. They are not needed in the "real" file.

Now you can run “ant” again and deploy the war file. If you point your browser to
http://localhost:8080/demo/velocity.jz
you will see:



revision: 13 / last update: 25.08.2004 10:48:08