Question

I'm new to Apache-Tiles. In fact, the biggest reason I'm using it is because it is the default renderer when creating a Spring Roo application. I know it has a big following so I figured I would give it a shot.

I am very confused, however, how to accomplish a simple task. I'm looking to add some declarations to the <head> section of the page from within my layout. I know/realize that I can create a separate jsp page for the header include and one for the body include and specify them in my views.xml file (as pointed out in this SO posting), but this seems very clunky and not appropriate.

For example, in a specify view, I would like to include some additional JS libraries. As well, I need to add some jQuery JS script that I want to run on the page. By convention, I like keeping my <body> clear of any such code, and put all the JS in the <head> section.

However, the need to create 2 files for each view (a head.jsp and a body.jsp) does not seem right. Afterall, this implies that I need to have 2 files open (head and body) as I am writing my JS jQuery code to see exactly which elements from the body are being manipulated, etc.

I know in Sitemesh I can accomplish this easily within the same page. I also know that Sitemesh and Tiles don't purport to do the same thing, but I would have expected there to be an easier way to do this than two seperate files.

Is there no way to define different regions/includes from within the same view jsp (as in Sitemesh)? Is the only choice to have 2 different files - one for header and one for body? Is there nothing I can do such that I can have both and defns in the index.jspx page?

Current setup is:

Default.jspx:

<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:spring="http://www.springframework.org/tags" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" >  

    <jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />

    <jsp:directive.page contentType="text/html;charset=UTF-8" />  
    <jsp:directive.page pageEncoding="UTF-8" /> 

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=8" />    

        <util:load-scripts />

        <spring:message code="application_name" var="app_name" htmlEscape="false"/>
        <title><spring:message code="welcome_h3" arguments="${app_name}" /></title>
    </head>

    <body class="tundra spring">
        <div id="wrapper">
            <tiles:insertAttribute name="header" ignore="true" />
            <tiles:insertAttribute name="menu" ignore="true" />   
            <div id="main">
                <tiles:insertAttribute name="body"/> 
                <tiles:insertAttribute name="footer" ignore="true"/>
            </div>
        </div>
    </body>
</html>

Views.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
    <definition extends="default" name="rates/index">
        <put-attribute name="body" value="/WEB-INF/views/rates/index.jspx" />
    </definition>
</tiles-definitions>

Index.jspx:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page" 
    xmlns:spring="http://www.springframework.org/tags" 
    xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" 
    xmlns:jQWidgets="urn:jsptagdir:/WEB-INF/tags/jQWidgets" 
    version="2.0">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <jsp:output omit-xml-declaration="yes"/>
  <spring:message code="label_rates_index" htmlEscape="false" var="title"/>
  <util:panel id="title" title="${title}">
    <spring:message code="application_name" htmlEscape="false" var="app_name"/>
    <h3>
      <spring:message arguments="${app_name}" code="welcome_titlepane"/>
    </h3>
  </util:panel>

</div>
Was it helpful?

Solution

With Tiles you are working with the composite pattern, not the decorator pattern as you were with Sitemesh.

Tiles is powerful and very flexible, you have a number options from string attributes, or list attributes to wildcards. Take a look through the Tiles documentation.

Another blog post that takes you through the more advanced capabilities of Tiles is on tech.finn.no

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top