grails 中layout的问题

[size=medium]定义页面布局(Layouts)
页面布局可以通过Grails对SiteMesh的支持来创建,有两种方法来创建页面布局,一种是在视图页面中设置名为"layout"的meta标签的值来指定需要使用的页面布局:

<html>
<head>
<meta name="layout" content="main"></meta>
</head>
<body>This is my content!</body>
</html>

现在在"grails-app/views/layouts"目录下创建名为"main.gsp"的布局就可以了!内容如下:

<html>
<head>
<title><g:layoutTitle default="An example decorator" /></title>
<g:layoutHead />
</head>
<body οnlοad="${pageProperty(name:'body.onload')}">
<div class="menu"><!--my common menu goes here--></menu>
<div class="body">
<g:layoutBody />
</div>
</body>
</html>

这个布局使用了 GSP 标签 来将布局应用到目标页面。[/size]


[size=large]GSP对于页面布局也提供了支持,这就是Layout。它是以Sitemesh为基础,同样也是GSP文件,文件位于grails-app/views/layouts。Layout定义涉及3个主要元素:

layoutTitle,输出目标页Title
layoutHead,输出目标页Head
layoutBody,输出目标页Body
使用示例:

<html>
<head>
<title><g:layoutTitle default="An example decorator" /></title>
<g:layoutHead />
</head>
<body οnlοad="${pageProperty(name:'body.onload')}">
<div class="menu">
<!--my common menu goes here?
<div class="body">
<g:layoutBody />
</div>
</div>
</body>
</html>
其中,pageProperty,可用于输出目标页的某个属性。

对于Layout的使用,4种方式。

法1:在<head>之间使用<meta name="layout" content="main"></meta>。

法2:使用规约。通过存放位置来确定。当有多个匹配时,以详细的优先使用:

应用于Controller:grails-app/views/layouts/controller-name.gsp
Action:grails-app/views/layouts/controller-name/action-name.gsp
法3:使用Controller的layout属性

class BookController {
//该controller相关的所有view都将使用
//grails-app/views/layouts/customer.gsp为Layout
static layout = 'customer'
def list = { … }
}
法4,使用<g:applyLayout>,对内容区域、URL、template应用模板:

<g:applyLayout name="myLayout" template="bookTemplate" collection="${books}" />

<g:applyLayout name="myLayout" url="http://www.google.com" />

<g:applyLayout name="myLayout">
The content to apply a layout to
</g:applyLayout>
在GSP中使用inlcude,有以下的方式:

作为标签:<g:include controller="book" action="list"/>
或者跟applyLayout合用:

<g:applyLayout name="myLayout">
<g:include controller="book" action="list"/>
</g:applyLayout>作为方法调用:def content = include(controller:"book", action:"list")
在Layout中,我们还可以使用内容块,这将让我们可以更灵活地使用layout。它的使用分成2部分:

在Layout中使用<g:pageProperty />预留:

<div id="left1">
<g:pageProperty name="page.layout1.left.content"/>
</div>
<div id="top2">
<div id="toolbar"></div>
</div>
<div id="center2">
<g:pageProperty name="page.layout2.center.content"/>
</div>
<div id="bottom2">
<g:pageProperty name="page.layout2.bottom.content"/>
</div>
<g:pageProperty name="page.other"/>
在内容页中使用<content>填充:

<content tag="layout1.left.content"> <!-- 注意没有page前缀 -->
<div id="toc"></div>
</content>
<content tag="layout2.center.content">
<div id="terminal"></div>
</content>
<content tag="layout2.bottom.content">
<div id="helpstone"></div>
</content>
<content tag="other">
<div id="how-to" style="text-align:left;">
...
</div>
</content>[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Grails实现两表关联有多种方式,以下是其一种常用的方法: 假设我们有两个领域模型类:`Author`和`Book`,其`Book`类需要关联`Author`类。 1. 在`Author`类定义主键: ``` class Author { static hasMany = [books: Book] static mapping = { id column: 'author_id' } Integer authorId String name } ``` 2. 在`Book`类定义外键: ``` class Book { static belongsTo = [author: Author] static mapping = { author column: 'author_id' } Integer bookId String title Author author } ``` 在这里,我们使用`belongsTo`来指定`Book`类属于`Author`类,并使用`mapping`来指定外键列名。同时,我们还在`Book`类定义了一个`author`属性,用于访问与此书相关联的作者。 3. 在数据库创建表格: 在数据库创建两张表格,分别为`author`和`book`表。在`book`表需要有一个名为`author_id`的外键列,用于与`author`表建立关联。 4. 保存数据: ``` def author = new Author(name: 'John Doe') author.save() def book = new Book(title: 'My Book', author: author) book.save() ``` 在此示例,我们先创建一个作者对象,然后创建一个书籍对象,并将作者对象赋给书籍对象的`author`属性。最后,我们分别保存作者和书籍对象到数据库。 5. 查询数据: ``` def author = Author.get(1) def books = author.books ``` 在这里,我们使用`get()`方法获取id为1的作者对象,并使用`books`属性获取与此作者相关联的书籍列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值