Müsli
Müsli

Reputation: 1774

Migrating from Sitemesh to Apache Tiles

I'm developing my own project using spring-mvc and spring-webflow. After reading some articles about spring webflow and ajax, I understood the better option is the use of Apache Tiles for rendering the view.

In Sitemesh i used a tag call head (). That tag used in the template allows render the entire head attribute of a page rendered on the resulting HTML.

Is there any way to achive this in Apache Tiles? From my readings i presume that i have to do the following:

Two jps, one with the body of the page and another with the head definition. Here is an example with the template, a page and the tiles definition for better understanding.

the tiles definition

<tiles-definitions>
  <definition name="base" template="/WEB-INF/view/templates/tileslayout.jsp">
    <put-attribute name="title" value="Held - main page"/>
    <put-attribute name="body" value=""/>
    <put-attribute name="head" value=""/>
  </definition>

  <definition name="company.edit" extends="base">
    <put-attribute name="head" value="/WEB-INF/view/company/editHeader.jsp"></put-attribute>
    <put-attribute name="body" value="/WEB-INF/view/company/edit.jsp"></put-attribute>
  </definition>

</tiles-definitions>

the template:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> 
<html>
<head>
 -- css and scripts --
 <tiles:insertAttribute name="head"  ignore="true"/>
<!--  <decorator:head /> -->
</head>

<body>

--- menu definition ---

<div class="container-fluid">
    <tiles:insertAttribute name="body"/>
<!--   <decorator:body/> -->
</div>
<hr/>      
<footer>
    -----
</footer>

</body>
</html>

a company page

<div class="container">
-- the page html code
</div>

the head company page

<meta name="menu" content="company" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
.error {
    color: red;
}
</style>
<script type="text/javascript">
    $(document).ready(function() {
        $('#name').focus();
    });
</script>

sometimes the head maybe more complex.

The resulting html is ok. But i don't like to define two jps for something that should be simple.

I'm doing something wrong?

Is there a better way to do this?

Upvotes: 1

Views: 1913

Answers (1)

Quaternion
Quaternion

Reputation: 10458

What you have is correct. The decoration pattern which Sitemesh uses can address this while the composition which Tiles uses can not. However I would not say it is much of a worry.

Composition is the conceptually simpler strategy and it is less resource intensive. It is good you are learning Tiles the two systems are not exclusive and can be complimentary.

Tiles is about factoring your pages into tiles. What you have is as good as tiles currently allows. You have found that in the head there is script the re-occurs and so have made a tile of it. Although it isn't as minimal as doing what you are proposing, if you put all the site functionality into a single script file it will take a short time to load but after doing so there will be no overhead due to caching.

If security is an issue (ie you have different security levels in your application and don't want to give away what a high level user is even capable of by exposing their JS) then you could have the tiles attributes use EL to define which tiles to bring in. To do this use tiles version 2.2.2 and use the tiles listener "org.apache.tiles.extras.complete.CompleteAutoloadTilesListener", this will allow the use of wildcards, EL, OGNL and MVEL within the tiles defintions. The following will show how wldcards can greatly reduce the size of your definitions:

Your second definition "company.edit" if you have multiple companies, with multiple verbs then you could write the following definition instead:

<definition name="*.*" extends="base">
  <put-attribute name="head" value="/WEB-INF/view/{1}/{2}Header.jsp"></put-attribute>
  <put-attribute name="body" value="/WEB-INF/view/{1}/{2}.jsp"></put-attribute>
</definition>

Now you can simply create company after company under /WEB-INF/view/ along with editHeader, viewHeader, etc.

If you upgrade to 2.2.2 and use the "org.apache.tiles.extras.complete.CompleteAutoloadTilesListener" because of the multiple ways of interpreting the expressions you would need to write:

<definition name="WILDCARD:*.*" extends="base">
  <put-attribute name="head" value="/WEB-INF/view/{1}/{2}Header.jsp"></put-attribute>
  <put-attribute name="body" value="/WEB-INF/view/{1}/{2}.jsp"></put-attribute>
</definition>

If you decided to decided to put all the JS for a particular security level into a tile you get the appropriate tile by possibly saying:

<put-attribute name="head" value="/WEB-INF/view/{1}/OGNL:'session.securityLevel'+'.jsp'"></put-attribute>

OGNL is the default Struts2 expression language, in your case you'd use the appropriate EL or MVEL.

Upvotes: 2

Related Questions