Thursday, December 11, 2008

Using Tiles 2 in a Prancoe project

First of all add the Tiles dependency to your pom.xml:


<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.1.0</version>
</dependency>


Specify the places of the Tiles configurations and the Tiles view resolver in your parancoe-servlet.xml file:


<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/defs/default.xml</value>
</list>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
<!-- Default values for prefix and suffix
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
-->
</bean>


Add the /WEB-INF/tiles/defs/default.xml file with the definitions of your views:


<?xml version="1.0" encoding="UTF-8"?>
<!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="template.main" template="/WEB-INF/tiles/templates/main.jsp">
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp"/>
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>
</definition>

<definition name="login" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/login.jsp"/>
</definition>

<definition name="admin/conf" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/conf.jsp"/>
</definition>

<definition name="admin/index" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/index.jsp"/>
</definition>

<definition name="admin/logs" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/logs.jsp"/>
</definition>

<definition name="admin/spring" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/spring.jsp"/>
</definition>

<definition name="admin/users/list" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/users/list.jsp"/>
</definition>

<definition name="admin/users/edit" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/admin/users/edit.jsp"/>
</definition>

<definition name="message" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/message.jsp"/>
</definition>

<definition name="genericError" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/genericError.jsp"/>
</definition>

<definition name="accessDenied" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/accessDenied.jsp"/>
</definition>

<definition name="welcome" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/welcome.jsp"/>
</definition>

<definition name="404" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/404.jsp"/>
</definition>

<definition name="500" extends="template.main">
<put-attribute name="main" value="/WEB-INF/jsp/500.jsp"/>
</definition>

</tiles-definitions>


Add the template in /WEB-INF/tiles/templates/main.jsp:


<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ include file="/WEB-INF/jsp/common.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/WEB-INF/jsp/head.jspf" %>
</head>
<body>
<div id="nonFooter">
<tiles:insertAttribute name="header"/>
<div id="content">
<div id="content_main">
<tiles:insertAttribute name="main"/>
</div>
<tiles:insertAttribute name="menu"/>
</div>
</div>
<tiles:insertAttribute name="footer"/>
</body>
</html>


Now You can simplify your JSP pages, removing from them all the layout-related parts. For example the welcome page (/WEB-INF/jsp/welcome.jsp) was:


<%@ include file="common.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="head.jspf" %>
</head>
<body>
<div id="nonFooter">
<jsp:include page="header.jsp"/>
<div id="content">
<div id="content_main">
<c:choose>
<c:when test="${requestScope.lang eq 'it'}">
Questa è l'applicazione template per il framework
<a href="http://wwww.parancoe.org">Parancoe</a>.<br />
<br />
Da questo punto di partenza puoi iniziare a costruire la
tua nuova applicazione, sfruttando tutti i benefici che
derivano dall'uso di Parancoe.<br />
<br />
Per maggiori informazioni visita il sito di Parancoe:<br />
<br />
<a href="http://wwww.parancoe.org">http://wwww.parancoe.org</a>.<br />
</c:when>
<c:otherwise>
This is the template application of the
<a href="http://wwww.parancoe.org">Parancoe</a> framework.<br />
<br />
From this starting point you can build your own application,
with all benefits of the using of the Parancoe Framework.<br />
<br />
For more infos, visit the Parancoe framework Web site:<br />
<br />
<a href="http://wwww.parancoe.org">http://wwww.parancoe.org</a>.<br />
</c:otherwise>
</c:choose>
</div>
<jsp:include page="menu.jsp"/>
</div>
</div>
<jsp:include page="footer.jsp"/>
</body>
</html>


Now it's just:


<%@ include file="/WEB-INF/jsp/common.jspf" %>
<c:choose>
<c:when test="${requestScope.lang eq 'it'}">
Questa è l'applicazione template per il framework
<a href="http://wwww.parancoe.org">Parancoe</a>.<br />
<br />
Da questo punto di partenza puoi iniziare a costruire la
tua nuova applicazione, sfruttando tutti i benefici che
derivano dall'uso di Parancoe.<br />
<br />
Per maggiori informazioni visita il sito di Parancoe:<br />
<br />
<a href="http://wwww.parancoe.org">http://wwww.parancoe.org</a>.<br />
</c:when>
<c:otherwise>
This is the template application of the
<a href="http://wwww.parancoe.org">Parancoe</a> framework.<br />
<br />
From this starting point you can build your own application,
with all benefits of the using of the Parancoe Framework.<br />
<br />
For more infos, visit the Parancoe framework Web site:<br />
<br />
<a href="http://wwww.parancoe.org">http://wwww.parancoe.org</a>.<br />
</c:otherwise>
</c:choose>

No comments: