开发者

SpringMVC 3 and Tiles 2 Localization of Page Title

开发者 https://www.devze.com 2023-03-25 23:03 出处:网络
I have a project setup using Spring 3, Apache Tiles 2 and Maven. Before I implement Tiles I was using the messages.properties file to dynamically populate the titles for a webpage (The part that appea

I have a project setup using Spring 3, Apache Tiles 2 and Maven. Before I implement Tiles I was using the messages.properties file to dynamically populate the titles for a webpage (The part that appears between the head and title tags). The reason for this was to allow localization in the future. However since I've integrated tiles, the tiles.xml file seems to control the titles for my page.

Is there a way to change this so the page title comes from messages.properties for each jsp I use as the body of a page?

tiles.xml is:

 <definition name="base.definition" template="/WEB-INF/views/layouts/layout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/views/includes/header.jsp" />
    <put-attribute name="menu" value="/WEB-INF/views/includes/menu.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/WEB-INF/views/includes/footer.jsp" />
</definition>

<definition name="home" extends="base.definition">
    <put-attribute name="title" value="Welcome from Tile" />
    <put-attribute name="body" value="/WEB-INF/views/home.jsp" />
</definition>

<definition name="new-deal-input" extends="base.definition">
    <put-attribute name="title" value="New Deal" />
    <put-attribute name="body" value="/WEB-INF/views/new-deal-input.jsp" />
</definition>

Where you see "Welcome from Tile" or "New Deal" as the title I would rather that this message comes from a messages.pr开发者_开发百科operties. I've tried putting the message in the title tags on the "body" page to no avail.

The project is setup on GitHub, you can take a look at this URL: Group-Deal-Clone


Another variant, without c:set:

in tiles-defs.xml:

<definition name="index" template="/WEB-INF/tiles/base.jsp">
  <put-attribute name="title" value="home.title"/>
  <put-attribute name="header" value="/WEB-INF/includes/iheader.jsp"/>
  ...
</definition>

in base.jsp:

<head>
 <tiles:importAttribute name="title" />
 <title><spring:message code="${title}"></spring:message></title>
</head>

in messages.properties:

home.title=Homepage title


You can put springs message key to tiles title attribute.

<definition name="home" extends="base.definition">
    <put-attribute name="title" value="message.key.welcome.text" />
    <put-attribute name="body" value="/WEB-INF/views/home.jsp" />
</definition>  

and use it in jsp something like this

     <c:set var="titleKey">
        <tiles:insertAttribute name="title" ignore="true" />
    </c:set>
    <title><spring:message key="${titleKey}" /></title>

I don't have right now tiles + spring project on hands so I can't check is syntax 100% correct but I have use this approach before.

And of course you could do something like this to get per page title

<title><spring:message key="welcome.${titleKey}"/></title>
0

精彩评论

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

关注公众号