One step beyond HelloWorld

I assume you have both installed Tomcat, Turbine 2.x and Maven, and tried the maven helloworld example.

In the following piece, we'll create a new application, called tutorial1, in which we add both a header, a footer and a left navigation menu.

The navigation menu leads to two other pages, one with a Java class associated which fills in a parameter, and one pure text (no java class).

There is no authentication.

This about the simplest potentially usefull application one can think of, resulting in a public website with both static and dynamic content.

Note on turbine, or HelloWorld

If you try to install turbine by installing helloworld with maven, you'll probably encounter some errors concerning the java activation framework, java mail and more. This is because Sun chose to license those projects such that they can not be incorporated in a tool like Maven.

In order to make it work, you can download the appropriate versions by hand (Maven tells you which version you need), unzip the packages and move the jars to $HOME/.maven/repository/jaf/jars/activation-<version>.jar (this example is for activation). You'll have to append the version number yourself.

When all jars are copied and appropriately named, you can run maven again and it will deploy helloworld, installing Turbine in the process.

Initializing your project

My directory structure when starting a project is like this:

/home/leon/intranet
	intranet/tomcat (the tomcat distribution)
	intranet/src

My Maven /home/leon/build.properties looks like this:

maven.appserver.home=/home/leon/intranet/tomcat

This is used for the deploy feature of maven.

I'm now going to:

cd /home/leon/intranet/src
and type
maven -Dturbine.app.name=tutorial1 turbine:setup

(this is shown in http://jakarta.apache.org/turbine/meta/using_meta.html )

I now have a fully deployable directory structure, which I'll test by:

cd tutorial1
maven turbine:deploy

and surfing to http://localhost:8080/tutorial1

You'll also notice that tomcat/webapps now holds a directory called tutorial1.

Adding headers and footers

Adding headers, footers, or other parts of a web page requires two small steps. Firstly, you must design the part of the page, and secondly you must let your layout know you wish to include them, and where it should put them.

First, the header. This is a small HTML segment like:

<center>
<b>Header</b>
</center>

and I store it in tutorial1/src/templates/navigations/header.vm

The footer is something like:

&copy; 2005 Leon

and stored as tutorial1/src/templates/navigations/footer.vm

To include it in the default layout, I change the file tutorial1/src/templates/layouts/Default.vm to

<html>
#TurbineHtmlHead()
<body #TurbineHtmlBodyAttributes() >
<table>
        <tr>
                <td>
                        $navigation.setTemplate( "header.vm" )
                </td>
        </tr>
        <tr>
                <td>
                        $screen_placeholder
                </td>
        </tr>
        <tr>
                <td>
                        $navigation.setTemplate( "footer.vm" )
                </td>
        </tr>
</table>

</body>
</html>

By using the navigation variable Turbine will know to look in the navigations directory.

At the moment of writing I have no clue what other default variables there are, and how they would expand.

Adding a static screen

Again, first make a file containing the content of your new page. As it will be included in layouts/Default.vm, you do not need to bother about html or body tags. The screen you design may hold at the very least a reference to the page variable, like
$page.setTitle( "My title" ), but does not require this.

an example file is screens/content1.vm:

<h1>Welcome to my content1</h1>
This is content

To be able to conviently reach this, we want a link on the first page (Index.vm) to point to our content. Add the following line to Index.vm:

<a href="$link.setPage( 'content1.vm' )">Content 1</a>

Alternatively, you may add a new navigation menu for all your links and incorporate it in Default.vm.

Adding a java filled screen

If you need to use your own variables, in stead of only the default ones like page, navigation and such, you can define a java class called like the screen it should service. In that class you may define all variables (name and content) used in that page.

An example Java class is

tutorial1/src/java/org/apache/turbine/app/tutorial1/modules/screens/content2.java:
package org.apache.turbine.app.tut1.modules.screens;

import org.apache.velocity.context.Context;
import org.apache.turbine.modules.screens.VelocityScreen;
import org.apache.turbine.util.RunData;

public class content2 extends VelocityScreen {
        public void doBuildTemplate( RunData data, Context context ) {
                try {
                        context.put( "content_field", "some text" );
                } catch( Exception e ) {
                        return;
                }
        }
}

and a screen tutorial1/src/templates/screens/content2.vm:

<h1>A field</h1>
The contents of content_field is $content_field <br>

You now can add a link to this java-driven screen in Index.vm, or a navigation part:

<a href="$link.setPage( 'content2.vm' )">Content 1</a>

Deploying

cd /home/leon/intranet/src/tutorial1
maven turbine:deploy
  • No labels