Better alternative to Apache Tiles

梦想的初衷 提交于 2019-11-29 09:13:34
Neil McGuigan

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

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.

mck

(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.
Fixus

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

Based on experience, I strongly recommended Apache Wicket.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!