Download the tiles2 file from the maven repository and verify the newest version at homepage do tiles2.
You also need in the following jars in your /WEB-INF/lib folder from the jarkata commons project:
In order to use tiles2 we have to add the servlet definition/mapping to the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Nome da aplicação</display-name>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext </param-name>
<param-value>messages</param-value>
</context-param>
<servlet>
<servlet-name>vraptor2</servlet-name>
<servlet-class>org.vraptor.VRaptorServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vraptor2</servlet-name>
<url-pattern>*.logic</url-pattern>
</servlet-mapping>
<!--servlet tiles2 -->
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.apache.tiles.servlet.TilesServlet</servlet-class>
<init-param>
<param-name>definitions-config</param-name>
<param-value>/WEB-INF/tiles-defs.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>Let's create the basic layout. In our example we use a table, which has some tiles tags embedded. These tags will be replaced dynamically.
Put this file in the /templates/ folder of your web application (like any other template file).
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<head>
<title>
<tiles:getAsString name="title"/>
</title>
</head>
<body>
<table>
<tr>
<td>
<tiles:attribute name="header"/>
<td>
</tr>
<tr>
<td>
<tiles:attribute name="body"/>
</td>
</tr>
</table>
</body>
</html>We divided the page in two parts: header e body.
This layout would be the same in Tiles 1.
Create a jsp file header.jsp for the header information, for example:
<b>VRaptor and Tiles2</b>
Also create the body.jsp, for example:
The is the body .....
The tiles-defs.xml is mapping file between layout and header/body. Put the file in the WEB-INF/ folder with the following content:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="base.layout" template="/templates/layout.jsp">
<put name="title" value=":: Tiles with Vraptor ::" />
<put name="header" value="/templates/header.jsp" />
<put name="body" value="/templates/body.jsp" />
</definition>
</tiles-definitions>The dtd version should be 2.0, please don't copy the any different version.
Let's create a page index.jsp to use our new defined layout. It will use the tiles:insertDefinition tag to call the layout.
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <tiles:insertDefinition name="base.layout"/>
That's it. Tiles 2 is configured and ready to use with VRaptor.
For this tutorial we used the following configuration: