开发者

Setting page title dynamically with tiles2 and spring mvc

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 usin开发者_如何学运维g 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


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>


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>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜