Apache Velocity is a templating framework that can be used as a replacement for JavaServer Pages (JSP). Tiles can be used with Velocity through the use of Tiles Velocity package.
To use Velocity together with Tiles
userdirective=org.apache.tiles.velocity.template.AddAttributeDirective,\ org.apache.tiles.velocity.template.AddListAttributeDirective,\ org.apache.tiles.velocity.template.DefinitionDirective,\ org.apache.tiles.velocity.template.GetAsStringDirective,\ org.apache.tiles.velocity.template.ImportAttributeDirective,\ org.apache.tiles.velocity.template.InsertAttributeDirective,\ org.apache.tiles.velocity.template.InsertDefinitionDirective,\ org.apache.tiles.velocity.template.InsertTemplateDirective,\ org.apache.tiles.velocity.template.PutAttributeDirective,\ org.apache.tiles.velocity.template.PutListAttributeDirective
<servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/tools.xml</param-value> </init-param> <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/velocity.properties</param-value> </init-param> </servlet>
@Override protected void registerAttributeRenderers( BasicRendererFactory rendererFactory, TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory, TilesContainer container, AttributeEvaluator evaluator) { super.registerAttributeRenderers(rendererFactory, applicationContext, contextFactory, container, evaluator); VelocityAttributeRenderer velocityRenderer = new VelocityAttributeRenderer(); velocityRenderer.setApplicationContext(applicationContext); velocityRenderer.setEvaluator(evaluator); velocityRenderer.setRequestContextFactory(contextFactory); velocityRenderer.setParameter("org.apache.velocity.toolbox", "/WEB-INF/tools.xml"); velocityRenderer.setParameter("org.apache.velocity.properties", "/WEB-INF/velocity.properties"); velocityRenderer.commit(); rendererFactory.registerRenderer("velocity", velocityRenderer); }
This way you can specify an attribute that is rendered directly using this syntax:
<put-attribute name="myAttribute" value="/pages/myPage.vm" type="velocity" />
There are two ways to use Tiles-Velocity integration:
$tilesAlt.startAttributeContext() $set($templateAttribute = $tilesAlt.createTemplateAttribute("/page/myTemplate.vm")) $set($attributeContext = $tilesAlt.getAttributeContext()) $set($attribute = $tilesAlt.createAttribute()) $attribute.setValue("This is the title.") $attributeContext.putAttribute("title", $attribute}) $set($attribute = $tilesAlt.createAttribute()) $attribute.setValue("/velocity/header.vm") $attribute.setRenderer("velocity") $attributeContext.putAttribute("header", $attribute}) $set($attribute = $tilesAlt.createAttribute()) $attribute.setValue("/velocity/body.vm") $attribute.setRenderer("velocity") $attributeContext.putAttribute("body", $attribute}) $tilesAlt.render($templateAttribute) $tilesAlt.endAttributeContext()
#tiles_insertTemplate({"template":"/page/myTemplate.vm"}) #tiles_putAttribute({"name":"title", "value":"This is the title."})#end #tiles_putAttribute({"name":"header", "value":"/velocity/header.vm", "type":"velocity"})#end #tiles_putAttribute({"name":"body", "value":"/velocity/body.vm", "type":"velocity"})#end #end
Other tiles directives are:
For details about the tool and directives see the Javadocs.