Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize page title with Spring and Tiles2?

I have a Spring application which uses Tiles for the view tier. So all my pages definitions look like this:

<definition name="main.page" template="/tiles/layout.jsp">
    <put-attribute name="title" value="Page Title"/>
    <put-attribute name="header" value="/tiles/header.jsp"/>
    <put-attribute name="body" value=""/>
    <put-attribute name="footer" value="/tiles/footer.jsp"/>
</definition>

<definition name="welcome.page" extends="main.page">
    <put-attribute name="title" value="Main Page"/>
    <put-attribute name="body" value="/pages/welcome.jsp"/>
</definition>

The code which sets page title is:

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

I would like to localize with Spring tag:

<spring:message>

Are there any "best practices" how to do that?

like image 553
Pavel Avatar asked Dec 15 '10 09:12

Pavel


2 Answers

Did you ever tried to put the message key in you tiles variable and use it as key for the spring message tag.

Something like that:

<definition name="welcome.page" extends="main.page">
    <put-attribute name="titleKey" value="page.main.title"/>
    <put-attribute name="body" value="/pages/welcome.jsp"/>
</definition>

jsp:

<set var"titleKey"><tiles:getAsString name="titleKey"/></set>
<title><spring:message code=${titleKey} /></title>
like image 182
Ralph Avatar answered Oct 29 '22 04:10

Ralph


The previous answer contains several little mistakes

tiles.xml

<definition name="main" template="/WEB-INF/jsp/template.jsp">
        <put-attribute name="titleKey" value="main.title" />
    <put-attribute name="body" value="/WEB-INF/jsp/main.jsp" />
</definition>

jsp (/WEB-INF/jsp/template.jsp)

<c:set var="titleKey"><tiles:getAsString name="titleKey"/></c:set>
<title><spring:message code="${titleKey}"></spring:message> </title>
like image 23
Jérôme Gloaguen Avatar answered Oct 29 '22 06:10

Jérôme Gloaguen