我是 Grails 的新手。我从过去几天开始学习。我正在按照本教程创建博客。
我有 3 个域帖子、评论和评论员。一个评论属于一个帖子,一个帖子有很多评论,一个评论有一个评论者,一个评论者属于一个评论。看我的代码..
领域类 ---
后域类 -
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
评论域类 -
class Comment implements Comparable {
static belongsTo = Post
Post post
String comment
Commentator who = new Commentator()
Date dateCreated
public int compareTo(Object o) {
return dateCreated.compareTo(o.dateCreated)
}
static constraints = {
}
}
评论员域类 -
class Commentator {
static belongsTo = Comment
String name
String url
String email
Comment comment
static constraints = {
name(nullable:false, blank:false)
url(nullable:true, blank:true, url:true)
email(nullable:true, blank:true, email:true)
}
}
控制器类——
后控制器——
class PostController {
def defaultAction = 'list'
def edit = {
def post = Post.get(params.id)
if(!post) {
post = new Post()
}
render(view:'edit', model:[post:post])
}
def list = {
render(
view:'list',
model:[posts:Post.list(
sort:'lastUpdated',
order:'desc')])
}
def save = {
def post = loadPost(params.id)
post.properties = params
if(post.save()) {
redirect(action:'list')
} else {
render(view:'edit', model:[post:post])
}
}
private loadPost(id) {
def post = new Post();
if(id) {
post = Post.get(id)
}
return post
}
def view = {
render(view:'view', model:[post:Post.get(params.id)])
}
}
和 CommentController 类——
class CommentController {
def edit = {
render(view:'edit',
model:[
comment:new Comment(),
postId:params.postId])
}
def save = {
def comment = new Comment(params)
comment.dateCreated = new Date();
comment.post = Post.get(params.id)
if(comment.save()) {
redirect(
controller:'post',
action:'view',
id:params.postId)
} else {
render(view:'edit',
model:[comment:comment,
postId:params.postId])
}
}
}
查看部分——
发布/查看 ---
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>${post.title}</title>
<r:require modules="bootstrap"/>
<meta name="layout" content="main"/>
</head>
<body>
<div class="well">
<h1>${post.title}</h1>
<p>${post.teaser}</p>
<div>${post.content}</div>
<g:link controller="comment" action="edit" id="${post.id}" class="btn btn-success">
Add comment
</g:link>
<g:each in="${post.comments}" var="comment">
<div class="comment">
<p>${comment.comment}</p>
<p>Made by: ${comment.who.name} on ${comment.dateCreated}</p>
</div>
</g:each>
</div>
</body>
</html>
当我单击 On Add Comment 时,它被称为 comment/edit.gsp,它位于此处——
编辑.gsp——
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Create Comment</title>
</head>
<body>
<h1>Create a comment</h1>
<div id="validationerrors">
<g:renderErrors bean="${comment}"/>
</div>
<g:form controller="comment" action="save">
<g:hiddenField name="postId" value="${postId}"/>
<dl>
<dt>Your name:</dt>
<dd>
<g:textField name="who.name" value="${comment.who.name}"/>
</dd>
<dt>Your email:</dt>
<dd>
<g:textField name="who.email" value="${comment.who.email}"/>
</dd>
<dt>Your website/blog:</dt>
<dd>
<g:textField name="who.url" value="${comment.who.url}"/>
</dd>
<dt>Add your comment:</dt>
<dd>
<g:textArea name="comment" value="${comment.comment}" rows="20" cols="50"/>
</dd>
</dl>
<g:submitButton name="submit" value="Save"/>
</g:form>
</body>
</html>
当我单击保存按钮时,它显示验证错误
Property [post] of class [class groovypublish.Comment] cannot be null
错误来了,因为 postId 没有得到<g:hiddenField name="postId" value="${postId}"/>
。请帮忙..