0

我是 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}"/>。请帮忙..

4

1 回答 1

1

我认为在您的控制器中您正在使用 params.postId,而从视图中发送 post.id 在 id 参数中,因此您应该在 params.id 而不是 params.postId 中获取帖子 id,我认为如果您更改编辑功能控制器为:

  def edit = {
     if(params.id){
      render(view:'edit',
            model:[
                    comment:new Comment(),
                    postId:params.id])
    }else{
      render "No post Id available"
    }
  }

并且在您的后控制器中,您还可以调用:

render(view:'edit', model:[post:post])

现在您正在发送帖子对象以查看:编辑,但是在编辑 gsp 页面中您使用的是 postId,我认为您应该选择其中任何一个,或者发送对象并在 edit.gsp 中使用 object.id,或者如果您正在使用 postId 那么您应该将渲染更改为:

render(view:'edit', model:[postId:post.id])

并将您的保存操作更改为:

def save = {
    def comment = new Comment(params)
    comment.dateCreated = new Date();

Post postInstance = Post.get(params.postId)
if(postInstance){
comment.post = postInstance
    if(comment.save(failOnError:true)) {
        redirect(
                controller:'post',
                action:'view',
                id:params.postId)
    } else {
        render(view:'edit',
                model:[comment:comment,
                        postId:params.postId])
    }
}else{
render "No Post instance found" 
}
}

如果您还有任何问题,请告诉我:)

于 2013-07-05T09:52:34.263 回答