我想知道是否可以使用 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/