struts 2, tiles 2 dynamic title
Asked Answered
A

6

6

I am using tiles 2.0.6 as my template framework together with struts 2.1.6. I am writing a simple cms page and want to let the user to define the title of each html page.

I have a title definition like this

    <definition name="base" template="/WEB-INF/jsp/templates/base.jsp">
        <put-attribute name="title" value=" "/>
        <put-attribute name="header" value="/WEB-INF/jsp/templates/header.jsp"/>  
        <put-attribute name="content" value="dummy"/>
        <put-attribute name="footer" value="/WEB-INF/jsp/templates/footer.jsp"/>   
        <put-attribute name="search" value="/WEB-INF/jsp/search.jsp"/>
    </definition>    
    <definition name="staticview" extends="base">
        <put-attribute name="title" value=" - Static"/>
        <put-attribute name="content" value="/WEB-INF/jsp/static/view.jsp"/>
    </definition>  

Instead of making the title a jsp, is there a way to dynamically override the title (String) on my header.jsp in the later jsp attribute, for example view.jsp. Or even 1 step further using EL

<put-attribute name="title" value="%{title}"/>

and have it pick up the title on the struts ognl dynamically.

Please advise

Thanks in advance

Apocalypse answered 11/10, 2009 at 23:37 Comment(0)
H
3

In the view page we need to have this -

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

Above will get you the title for the page. Except, since we want the page title to be dynamic, in the tiles.xml configuration, I added

<definition name="page1" extends="base">
    <put-attribute name="title" value="Page 1"/>
    <put-attribute name="content" value="/WEB-INF/jsp/page1.jsp"/>
</definition>
<definition name="page2" extends="base">
    <put-attribute name="title" value="Page 2"/>
    <put-attribute name="content" value="/WEB-INF/jsp/page2.jsp"/>
</definition>

Now this may seem like typing it will make it look like it is static. But every time you view that page, the title should be the same for that page. What better place to have this information that on tiles.xml.

For me it was not the title itself, but I needed different page headings. I didn't want to look at the context attribute to get the path of the page and determine the heading for the page. So, this worked for me and kept everything loosely coupled.

This works if you want a different dynamic heading for each page or anything similar.

Hein answered 28/2, 2013 at 4:36 Comment(3)
+1. But as far as what better place to put that information, I wish the title could be pulled from a bundle to be internationalized. This route means that you'd need a different tiles definition for every language.Infantilism
@bphilipnyc - I totally agree. Do you know of a way to make this internationalization friendly ?Cutlor
Unfortunately, no. For our project, I avoided putting titles in Tiles for this reason (I call the message bundle from the service layer to get an internationalized title). Related post: https://mcmap.net/q/1914695/-tiles-struts-el-expressionsInfantilism
S
1

Keep the tiles definition like this:

    <put-attribute name="title" value=""/>

Add title as a property of in your action class.

And in view.jsp page use this:

    <tiles:insertDefinition name="staticview">
        <tiles:putAttribute name="title"> 
            ${title} <%--OR, <s:property value="title"/>--%>
        </tiles:putAttribute>
        <%--Remainning content--%>
    </tiles:insertDefinition>
Silt answered 9/9, 2011 at 6:24 Comment(0)
V
1

I tried this, and it works.

Code1

<tiles:putAttribute name="title"> 
          You String
</tiles:putAttribute>

Code 2

<tiles:insertAttribute name="title" />

But code1 must execute before code2.

Vasiliu answered 26/3, 2012 at 17:7 Comment(1)
Can you make "You string" dynamic, like can you put code there rather than a static string? (Preferable some code that can fetch the page title from a resource file)Shoe
R
0

The best solution in my opinion is to use expression instead of value in the tiles definition and pass ${title}. But you would have to do it in each view explicitly, unfortunately. See more at http://symfony-world.blogspot.com/2013/02/dynamic-attribute-values-with-apache.html

Reinhardt answered 14/2, 2013 at 22:18 Comment(0)
C
0

Found this simple solution elsewhere:

Tiles attribute definition as usual:

 <put-attribute name="title"  value="welcome.title"/>

On an jsp page the attribute is first imported and then it can be used with struts tags:

<tiles:importAttribute name="title" />
<title><s:text name="%{#attr['title']}"/></title> 
Cacie answered 13/8, 2013 at 10:25 Comment(0)
C
-1

I add the title to the request in the action class, here's my JSP code for the template (tiles 1):

<title>
    <tiles:getAsString name="title"/>
    <%-- add additional title (if found) --%>
    <logic:present scope="request" name="title"><bean:write scope="request" name="title"/></logic:present>
</title>
Coping answered 20/11, 2009 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.