Richard’s Weblog

February 19, 2009

Apache Tiles 2, ViewPreparer example.

Filed under: template system,Web development — Richard @ 5:29 pm
Tags: ,

In a previous post, I made a brief example of using Tiles 2 as a web template system. This time, I will extend the same example to make my menu use a ViewPreparer.

A ViewPreparer is a class whose code will be executed just before a certain tile (in my example, a jsp file) is rendered. This example will use ViewPreparer to have some java code prepare a menu, put it in the request scope for the menu tile to display it. This way my menu can be generated by java code, and the menu tile (the view) just renders the generated menu.

Configuring the views to use a ViewPreparer

To make a big picture of this, I’ll first tell my template to use a ViewPreparer.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
  <definition name="commonPage" template="/template.jsp" preparer="sample.MenuPreparer">
<put-attribute name="header" value="/defaultHeader.jsp" />
<put-attribute name="menu" value="/defaultMenu.jsp" />
<put-attribute name="footer" value="/defaultFooter.jsp" />
  </definition>

  <definition name="homePage" extends="commonPage">
<put-attribute name="body" value="/home_body.jsp" />
  </definition>

  <definition name="aboutPage" extends="commonPage">
<put-attribute name="body" value="/about_body.jsp" />
  </definition>

</tiles-definitions>

In this configuration, I tell all my pages that uses template.jsp to run my sample.MenuPreparer before being rendered.

Displaying the menu

As I said above, the menu will be generated by a ViewPreparer. The menu tile will list all menu items found in the “menu” request attribute. Each menu item must specify a caption and a url :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div>
<ul>
    <c:forEach var="menuItem" items="${requestScope.menu}">
      <c:url var="url" value="${menuItem.url}"/>
	<li><a href="${url}">${menuItem.caption}</a></li>
</c:forEach></ul>
</div>

Coding the ViewPreparer

The only thing that’s left to do now is to code the configured ViewPreparer :

package sample;

import org.apache.tiles.preparer.ViewPreparer;
import org.apache.tiles.preparer.PreparerException;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.AttributeContext;

import java.util.ArrayList;

public class MenuPreparer implements ViewPreparer {

  public void execute(TilesRequestContext context, AttributeContext attributeContext)
    throws PreparerException {
    ArrayList<MenuPreparer.MenuItem>	menu = new ArrayList<MenuPreparer.MenuItem>();
    menu.add(new MenuItem("Home", "home.jsp"));
    menu.add(new MenuItem("About","about.jsp"));
    context.getRequestScope().put("menu", menu);
  }

  public static class MenuItem {
    private String url;
    private String caption;
    public MenuItem(String caption, String url) {
    	this.caption = caption;
    	this.url = url;
    }
    public String getUrl() {return url;}
    public String getCaption() {return caption;}
  }
}

Here it is, now my menu is generated by pure Java code instead of being hard-coded in the tile. This example is simple just to understand basic ViewPreparer configuration and implementation. A real MenuPreparer could read menu items from an XML file or a database, or even take care of internationalization for each menu caption.

About these ads

7 Comments »

  1. Hi

    Great example just what I need for populating a dropdown box in a jsp in a tile definition

    Not to anyone else. if you are using struts tags
    the equivalant of
    ${requestScope.menu}
    is

    Comment by Alan — April 12, 2009 @ 9:55 am | Reply

  2. Is it possible to get hold of the request parameters in ViewPreparer#execute()?

    Comment by Duncan — September 16, 2009 @ 2:43 pm | Reply

  3. Hello Richard,

    I have followed your last Tiles tutorials, and when I try to implement this example I have facing an exception:

    Nov 20, 2009 10:19:10 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet jsp threw exception
    java.lang.ClassNotFoundException: javax.el.ValueExpression
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)

    Could you please tell what I’m doing wrong?
    Should I add other Jar file?

    E

    Im using tomcat 5.5, Eclipse Java EE IDE for Web Developers.

    Regards.

    Comment by Jesus — November 20, 2009 @ 2:18 pm | Reply

  4. Thanks for the tut.

    Comment by Faram — December 9, 2010 @ 11:12 pm | Reply

  5. Hello, I’m a university student in Korea. Thank you for your post. It made me understand ViewPreparer exactly.
    I have a question, and if you okay, would you reply to my question?

    I want to know how you know to use ViewPreparer.
    To find the usage of the ViewPreparer, I searched some hints of it in tiles.apache.org, but I could not found exact example.
    How do you know the usage of the ViewPreparer you posted??

    If you have enough time, plz send me your e-mail.

    Thank you so much.
    Have a nice day~:)

    Comment by Hello — March 28, 2011 @ 5:18 am | Reply

  6. Superb Article. Very helpful. Thanks.

    Comment by Badal Chowdhary — October 3, 2011 @ 7:55 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: