SpringMVC 3 和 Tiles 2 页面标题本地化

发布于 2024-11-27 23:59:44 字数 1416 浏览 3 评论 0原文

我有一个使用 Spring 3、Apache Tiles 2 和 Maven 的项目设置。 在实现 Tiles 之前,我使用 messages.properties 文件动态填充网页的标题(出现在 head 和 title 标签之间的部分)。这样做的原因是为了将来允许本地化。然而,由于我集成了图块,tiles.xml 文件似乎控制了我的页面的标题。

有没有办法更改此设置,以便页面标题来自我用作页面正文的每个 jsp 的 messages.properties?

tiles.xml 是:

 <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>

当您看到“Welcome from Tile”或“New Deal”作为标题时,我宁愿这条消息来自 messages.properties。我尝试将消息放入“正文”页面上的标题标签中,但无济于事。

该项目已在 GitHub 上安装,您可以查看以下 URL:Group-Deal-Clone

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.properties. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-12-04 23:59:44

另一种变体,没有 c:set:

在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>

在base.jsp中:

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

在messages.properties中:

home.title=Homepage title

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
一个人练习一个人 2024-12-04 23:59:44

您可以将 springs 消息键放入tiles标题属性中。

<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>  

并在jsp中使用它,就像这样

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

我现在手上没有tiles + spring项目,所以我无法检查语法是否100%正确,但我以前使用过这种方法。

当然,你可以这样做来获取每页标题

<title><spring:message key="welcome.${titleKey}"/></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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文