17

I'm looking for a framework that is better and easier to use than Apache Tiles (which so far, I have used a couple of times).

With Tiles, it seems that when I have 100 actions I need to creates 100 jsp files and create 100 definitions in tiles.xml.

Is there a better framework to manage my templates? I want to create, for example, 2 templates:

a) menu and column for content
b) menu, column for content, right column with banner

In both templates the menu is constant. In template b, the right column is constant, so only the content column is different. For this simple example I don't want to define each JSP file that extends the template a (just to provide a body). Thats lame imo. Or maybe I`m lame and I can define a DEFAULT template in Apache Tiles and I'm just not using it right. In anycase, all help appreciated.

Fixus
  • 4,631
  • 10
  • 38
  • 67
  • 3
    I think that in your case sitemesh can be a better approach since what you need is simple, just two templates. Once you setup it, you can forget about it. – jddsantaella Mar 17 '12 at 20:43

5 Answers5

15

Overall, I would recommend SiteMesh over Tiles.

Here's how to setup SiteMesh 3

You can use Tiles for in-page templates, but use SiteMesh for site-wide template. Nevertheless...

How to make Tiles suck less:

  1. Use convention over configuration. For example, put your definitions in webapp/WEB-INF/tiles.xml and there's no need to tell tiles where it is.

  2. Use wildcards:

<definition name="default" template="/WEB-INF/templates/default.jsp">
    <put-attribute name="titleKey" value=""/>
    <put-attribute name="body" value=""/>
</definition>

<definition name="*" extends="default">
    <put-attribute name="titleKey" value="{1}.title"/>
    <put-attribute name="body" value="/WEB-INF/views/{1}.jsp" />
</definition>

If your controller returns view name index, it will match the definition *, and use the JSP file /WEB-INF/views/index.jsp for the body, and use the message property index.title.

If your controller returns view name contact-us, it will match the definition *, and use the JSP file /WEB-INF/views/contact-us.jsp for the body, and use the message property contact-us.title

In your template, add:

<c:set var="titleKey"><tiles:getAsString name="titleKey" /></c:set>

and

<title><spring:message code="${titleKey}"/></title>

Add ReloadableResourceBundleMessageSource bean to your servlet application context.

Make a file /src/main/resources/messages.properties, with content like:

index.title = Welcome to Acme, Inc.
contact-us.title = Contact Us
Community
  • 1
  • 1
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • 4
    +1. Very helpful, but what if you have multiple default.jsp templates? Tiles wouldn't know how to differentiate between them with wildcards, right? Like in OP's case he has two templates. (I realize in his case he can just do an ignore, but that's not always something you want to do.) – riddle_me_this Apr 22 '15 at 16:01
7

(similar to this)

You don't need a definition for every action.

This boilerplate configuration is a hang-up from tiles-1 days. It really isn't necessary with tiles-2 when wildcards were introduced, and especially with tiles-3 along with the OptionsRenderer.

Here's a tutorial that will help you with

  • spring to tiles integration,
  • definitions with wildcards,
  • implementing a fallback pattern using the OptionsRenderer, and
  • definitions composition.
Community
  • 1
  • 1
mck
  • 1,152
  • 10
  • 10
7

An other approach is Sitemesh. It was designed to mesh views where you can not modify the original, so it is more a html transformation/decoration framework than a templating framework like Tiles.

In my personal opinion Tiles is the better approach for appliations, and I would try to implement some kind of resolver (based on some naming conventions) that makes the xml files obsolete, but this was not the question.

@See: This old introductions shows how SiteMesh works.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • ok but I would love to have a template engine with this kind of resolver. Is there something like that ? – Fixus Mar 17 '12 at 11:00
  • I dont know, but I expect that the way the resolver resolves the view is highy application dependend, and on the other way I expect that it is not hard to write one. – Ralph Mar 17 '12 at 20:39
  • Yes, Tiles-3 comes with the resolving ability you mention. Checkout the tutorial …( http://tech.finn.no/the-ultimate-view-tiles-3/ ) – mck Sep 21 '14 at 18:30
5

I ended up using JSF + Facelets. I`ve combined them with Spring MVC and it works like a charm.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Fixus
  • 4,631
  • 10
  • 38
  • 67
  • 4
    You cannot combine one MVC framework with another. Its a misunderstanding of ideas. JSF is MVC framework same as Spring MVC. You most likely composed Spring with JSF MVC. Better explanation is described here: http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc – Daniel Hajduk Jan 12 '16 at 12:05
-2

Based on experience, I strongly recommended Apache Wicket.

archetype
  • 134
  • 2