Grails g :include can it be done?

标签 grails grails-controller

我想知道是否可以使用 g:include 来仅包含给定页面的正文内容。

假设我有一个主布局页面,如下所示:

<html>
  <head>
    <title>My start page</title>
    <g:layoutHead>
  </head>
  <body>
    <g:layoutBody>
  </body>
</html>

然后是主页(index.gsp)

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    THIS IS MY INDEX BODY CONTENT WITH AN INCLUDE
    <g:include controller="book" action="list"/>
    <g:link controller="book" action="list">See the full list!</g:link>
  </body>
</html>

最后是书籍/列表页面

<html>
  <head>
    <!-- main layout reference -->
    <meta name="layout" content="main"/>
  </head>
  <body>
    <table>
    <g:each in="${books}">
      <tr>
        <td>${it.author}</td>
        <td>${it.title}</td>
        <td>${it.price}</td>
      </tr>
    </g:each>
    </table>
  </body>
</html>

所以我想要实现的是主页(index.gsp)仅包含书籍/列表页面中定义的表格。但是,当我尝试这样做时,它包含定义的整个 html( <html> 标签和所有)。

有可能以某种方式做到这一点吗?我尝试过类似 <g:include controller="book" action="list" view="someView.gsp"/>但这不起作用。我真的想避免向“索引 Controller ”添加图书列表逻辑,我想重用现有的 Controller 。

我不是第一个拥有这个用例的人,你们想出了什么解决方案?

最佳答案

您可以使用applyLayout标签。只需创建一个 empty.gsp 布局,只需:

<g:layoutBody/>

然后用 applyLayout 装饰您的 include 标记:

<g:applyLayout name="empty">
  <g:include controller="book" action="list"/>
</g:applyLayout>

请参阅 Grails guide 上的条目以供进一步引用。

关于Grails g :include can it be done?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6340714/

相关文章:

grails - Grails 2.1.0 需要 Groovy 1.8 吗?

Grails 命令对象未绑定(bind)到 'def' 类型

grails - 无法登录Grails应用

java - Controller 中的 Grails 重定向从 https 切换到 http。为什么?

Grails oauth 插件 : Unknown provider linkedin, 检查你的配置

java - 使用 Jenkins 在构建时修改 web.xml 中的 <display-name>

mysql - 如何通过单击 grails 中的按钮(.Groovy Server Pages 页面)从 Mysql 数据库获取表的所有值/行?

hibernate - 删除grails域对象的代理部分?

hibernate - 在GORM中使用复合键