Fork me on GitHub

View Preparers

Sometimes a definition, before it is rendered, needs to be "prepared". For example when showing a menu, the menu structure must be created and stored in the request scope.

For this reason, a View Preparer can be used: it is called before the definition is rendered, so all the things needed to render correctly the definition can be prepared.

Creating a view preparer

A View Preparer is simply a class that implement the ViewPreparer interface. The execute method allows to execute code before a definition is rendered. You can extend the ViewPreparerSupport class to avoid compiling problems in the case the ViewPreparer interface changes.

package my.package;

import org.apache.tiles.preparer.PreparerException;
import org.apache.tiles.preparer.ViewPreparer;
import org.apache.tiles.request.Request;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.Attribute;

public class TestViewPreparer implements ViewPreparer {

    public void execute(Request tilesRequest, AttributeContext attributeContext)
    throws PreparerException {
            new Attribute("This is the value added by the ViewPreparer"));

Associating a preparer

To associate a preparer to a definition, put its complete path name to the preparer attribute of the <definition> element:

<definition name="preparer.definition" preparer="org.apache.tiles.test.preparer.TestViewPreparer">
  <put-attribute name="foo" value="/bar/foo.jsp" />