kosoant
kosoant

Reputation: 11629

Best way to set HTML head title in a Spring+Tiles2 application?

I have a usability problem in my Spring webapp which uses Tiles as the view technology. At the moment all of the pages display the same HEAD_TITLE and the PAGE_TITLE is page specific:

 <html>
 <head><title>HEAD_TITLE</title></head>
 <body>
 <h1>PAGE_TITLE</h1>
 </body>
 </html>

This is a major usability problem as the browsers history lists all different pages of the application with the same title. The reason why the HEAD_TITLE is same for all pages is that I haven't found a reasonable way to use the PAGE_TITLE as the HEAD_TITLE.

In most cases the PAGE_TITLE comes from a message bundle with <fmt:message /> tag and some parameters are passed to it. The Tiles layout is such that the HEAD_TITLE should be already set at that point because all pages of the webapp use the same common layout which defines the <HEAD> elements of the pages amongst other stuff.

Any suggestions how to fix this usability problem? Should I set a "pageTitle" request attribute in my Spring controllers for all pages and use that as the PAGE_TITLE and also as the HEAD_TITLE? Or is it possible to somehow set the HEAD_TITLE in the page specific JSP?

Upvotes: 3

Views: 729

Answers (1)

dira
dira

Reputation: 30594

Create a general definition and define headTitle and pageTitle attributes.

<definition name="threeColumnLayout" template="/WEB-INF/ThreeColumnLayout.jsp" >
    <put-attribute name="headTitle" value="" />
    <put-attribute name="pageTitle" value="" />
    <put-attribute name="left" value="/WEB-INF/left.jsp" />
    <put-attribute name="middle" value="" />
    <put-attribute name="right" value="/WEB-INF/right.jsp" />
</definition>

Set appropriate values in more specific definition.

<definition name="/user/new" extends="threeColumnLayout">
    <put-attribute name="headTitle" value="Administration" />
    <put-attribute name="pageTitle" value="Create User" />
    <put-attribute name="middle" value="WEB-INF/views/UserCreate.jsp" />
</definition>

Use <tiles:getAsString /> tag to retrieve such values in jsp page.

<head>
    <title><tiles:getAsString name="headTitle"/></title>
</head>
<body>
    <h1>
        <title><tiles:getAsString name="pageTitle"/></title>
    </h1>
</body>

Reference:- http://tiles.apache.org/framework/tiles-jsp/tagreference.html#tiles:getAsString

Upvotes: 1

Related Questions