1

I've been asking myself this question for quite some time and I haven't found a nice solution for this on the web.

So I am using Tiles2 and Spring MVC and I'd like to set the page title dynamically within the body tile. Is there a way?

<definition name="mainTemplate" template="/WEB-INF/template/main.jsp">
 <put-attribute name="header" value="/WEB-INF/template/header.jsp" />
 <put-attribute name="footer" value="/WEB-INF/template/footer.jsp" />
 <put-attribute name="body" value="/WEB-INF/template/blank.jsp" />
</definition>

<definition name="list" extends="mainTemplate">
 <put-attribute name="body" value="/WEB-INF/jsp/list.jsp" />
</definition>

my current solution is setting the title within the controller

 model.addAttribute("pageTitle", "blubb");

and the doing a c:out in the template

suicide
  • 760
  • 4
  • 13
  • 20

4 Answers4

4

Tiles Technique

If by "I want to set the page title dynamically" you mean "I want to set the page title based on the tile that is being displayed and I want to use a tiles feature to do it" then:

  1. Define a title property; something like this: <put-attribute name="pageTitle" value="Title"/>
  2. Reference the pageTitle property in the layout for the page; something like this: <title><tiles:getAsString property="pageTitle"/></title>
  3. Set the pageTitle property in any tile that matters; <definition blah blah blah><put-attribute name="pageTitle" value="blah blah blah"/></definition>

Variable Technique

The simplest way to do this technique is to add an attribute to the model and to reference said attribute with an el expression. For example,

You might do this in your controller:

String pageTitle;

pageTitle = "something";
Model.add("PageTitle", pageTitle);

Then reference the "PageTitle" attribute in your page like this:

<head>
<title>${PageTitle}</title>

You can use c:out like this:

<head>
<title><c:out value="${PageTitle}"/></title>
DwB
  • 37,124
  • 11
  • 56
  • 82
  • Sorry, I should have been more clear about that. Your solution works if I know exactly what the title is supposed to be before execution. But I like to change the title depending on the content that is retrieved by the controller. Is there any way to do that from the "content" tile? – suicide Mar 28 '11 at 21:37
  • you can call the dynamic value for the title in the pageTitle class from a database in the above example instead of the hardcoded value i guess. – Dev Sep 21 '16 at 07:08
1

You can also combine DwB's two answers so you get the best of both worlds:

<title>
    <tiles:insertAttribute name="title" ignore="true" />
    <c:if test="${not empty pageTitle}">
        <c:out value="${pageTitle}"></c:out>
    </c:if>
</title>

Useful when you want some pages to have static titles (so you only need to set it in the tiles.xml file), some pages to have fully dynamic titles (don't set anything in tiles.xml, just add pageTitle to your model object) or a bit of both (my favourite) where you have a static first half and a dynamic second half.

simonlord
  • 4,347
  • 1
  • 19
  • 12
  • I think this is the best solution of them but still..putting view code in the controller doesn't sound to me like the best thing to do. – checklist Mar 17 '13 at 10:52
1

This is working for me. Is there anything wrong with it?

TILES:

<put-attribute name="myProjectRevision" value="1.0" type="string" />

JSP:

<span id="my-project-revision"><c:out value="${myProjectRevision}"/></span>
-2

tiles.xml:

<definition ... >
    ...
    <put-attribute name="title" value="My Title" />
</definition>

JSP:

<h1><tiles:getAsString name="title"/></h1>

But this is only a good solution if your application has only one language.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107