My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎

tiles

Basic

Apache Tiles is implementation of Core J2EE Pattern - Composite View.

It's bad idea to do something without knowing - what you want to get.
So, this is our test layout:
layouts

1) Add libraries (via pom.xml):

<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-tiles</artifactId> <version>1.3.10</version> </dependency>

2) Idea is in creating html(xml) pages from parts(tiles), each part(tile) can consist of content and or other tiles.  In this way wee have strong template engine. We can easyly change content of any tile and this changes will be wisible on any page that uset this tampleate(layout).

This is our main tamplate tile(layouts\mainLayout.jsp):
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE html>
<html>
<head>
    <title><tiles:getAsString name="title" ignore="true" /></title>
</head>
<body>
    <div id="pageContainer">
        <div id="header">
            <tiles:insert attribute="header" ignore="true"/>
             <div id="user_stat">
                 <tiles:insert attribute="userstat" ignore="true"/>
             </div>
        </div>
        <div id="pageEntry">
            <div id="menu">
                <tiles:insert attribute="menu" ignore="true"/>
            </div>
            <div id="content">
                <tiles:insert attribute="content" ignore="true"/>
            </div>
        </div>    
        <div id="footer">
            <tiles:insert attribute="footer" ignore="true"/>
        </div>
    </div>
</body>
</html>
This JSP page is tile. We write actualy html, but some parts are not implement. We will use this file as layout.
Let's apply  this layout to main site page(index.jsp):
<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<tiles:insert page="/layouts/mainLayout.jsp" flush="true">
     <tiles:put name="title" value="Page title" type="string"/>
     <tiles:put name="header" type="string">
        <tiles:insert page="/includes/header.jsp" flush="false"/>
     </tiles:put>
     <tiles:put name="userstat" type="string">
        <tiles:insert page="/includes/status.jsp" flush="false"/>
     </tiles:put>
     <tiles:put name="menu" type="string">
        <tiles:insert page="/includes/menu.jsp" flush="false"/>
     </tiles:put>
     <tiles:put name="content" type="string">
        <tiles:insert page="/content/main.jsp" flush="false"/>
     </tiles:put>
     <tiles:put name="footer" type="string">
        <tiles:insert page="/includes/footer.jsp" flush="false"/>
     </tiles:put>
</tiles:insert>
All that left to do is to create files that use as tiles and fill them with some content.
These are they: /includes/header.jsp, /includes/status.jsp, /includes/menu.jsp, /content/main.jsp, /includes/footer.jsp.

In my case no additional configuration was needed (I just create template and add taglib in file header. However I not sure will it work everywhere, so better see instalation notes on tileses site: http://tiles.apache.org/tutorial/configuration.html)

Also possible define definitions in JSP:
<tiles:definition 
  id="storefront.default"
  page="/layouts/simpleLayout.jsp" 
  scope="request">
    <tiles:put name="paramet" value="sfsdf" />
</tiles:definition>
<tiles:insert beanName="storefront.default" beanScope="request"> 
</tiles:insert>
/layouts/simpleLayout.jsp:
<%@taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<tiles:getAsString name="paramet" ignore="true" />
Note: tiles:definition and tiles:insert beanName must be scope visible, so better to define definition in file for definitions or use some like: 
<%@include file="../common/storefront-defs.jsp" %>


And now more tech details:

Also tiles:definition  are accessible (however i do not find reason to use them, but its better to know about them)
    tiles:definition can be nestings.
    tiles:definition can be abstract.
    tiles:definition can extends.
    tiles:definition can be chenged before rendering via ViewPreparer subcalss and preparer property thats point to this class.
Tiles can be changed and compose in runtime (http://tiles.apache.org/tutorial/advanced/runtime.html).
Tiles can be used without supported framework( struts support ) via TilesDispatchServlet.
Also tiles can be used as rendering decorator via TilesDecorationFilter.
Tiles support localization.(But i prefer to use  struts:bean library).
Tiles can use beans as attributes.
>>Tiles can even use with Spring! http://bitbybit.ru/article/242/

Used sources:



Source:

Make styling yourself =)




ą
Vasyl Khrystiuk,
10 Jan 2012, 08:12
ċ
web-app.zip
(3970k)
Vasyl Khrystiuk,
11 Jan 2012, 17:44
Comments