3
votes

I'm using Spring MVC 3 with Apache Tiles 3.

I want to add a custom title for one of my pages. I want this title to live in the specific view jsp and not in the layout jsp.

Here's what I'm doing:

Definition in tiles.xml

<definition name="availability" extends="base.definition">
    <put-attribute name="title" expression="${requestScope.title}"/>
    <put-attribute name="page" value="/WEB-INF/views/availability.jsp" />
</definition>

Here's the availability.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="title" value="Availability" scope="request"/>

Here's the layout.jsp (used by base.definition)

<title>App::<tiles:getAsString name='title' /></title>

This results in a null pointer exception in the tiles layer when rendering attribute.

I can set the title via model when the request goes to my controller as suggested here, but I would like to do this from within the view JSP and not the controller.

It looks like, the way the tiles composes and renders pages, this is not possible. Is that right?

2

2 Answers

1
votes

This is how you can use custom title (it works for all pages which uses tiles):

In template.jsp file:

<title><tiles:insertAttribute name="title" /></title>

In your page.jsp (in your case - availability.jsp) use this:

<tiles:putAttribute name="title" value="PageTitle" />

name = title, sets your page title by value = PageTitle.

Extra:

you can put your page titles in messages.properties file and use then use it like this:

<tiles:putAttribute name="title">
    <spring:message code="page.title"/>
</tiles:putAttribute>
0
votes

This is not possible if availability.jsp is being rendered after layout.jsp If you can not change the order then i'd suggest using javascript to update the title element in the dom.

Otherwise you should be looking to encapsulate the template and its values. That is the "title" element should be in a template called title.jsp (or head.jsp) and you use the specific title.jsp for each request. ( you don't need a separate definition for each different title.jsp ! )