作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个带有grails的博客中工作,事情是我创建了一个名为Post
的域类,在其中我将其定义为String content, Date date, String title
属性,并且由于帖子可以包含多个注释,因此我还创建了一个域类"Comment" with: String author, File avatar, String content, Date commentDate;
,因此我声明了一个与许多关系如下:Post域类中的static hasmany = [statements: Comment]
。然后在blog.gsp中,我想显示一个包含所有注释的帖子,因此我试图将< g:each >
标记与一个帖子一起用作变量,这个标签的想法是遍历该注释列表单个帖子,而不是所有帖子。如何实现这一目标?
最佳答案
我将使用“标准” Grails变量名来避免混淆。
如果您的 Controller 发回了Post
对象,您可以像下面这样迭代:
//PostController.groovy
def blog() {
def postInstance = Post.read(params.id)
[postInstance: postInstance]
}
//blog.gsp
${postInstance.title} //just to make sure your postInstance is correctly populated
<g:each in="${postInstance?.statements}" var="commentInstance">
${commentInstance.content}
<g:each>
//Post.groovy
static hasMany = [statements: Comment]
Comment
属于
Post
//Comment.groovy
static belongsTo = [post : Post]
关于loops - 如何使用<g:each>标签遍历单个元素Grails-Gsp的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16805792/
我是一名优秀的程序员,十分优秀!