Grails中的验证错误

时间:2013-07-05 09:37:29

标签: grails groovy grails-2.0 grails-domain-class

我是Grails的全新人物。我从过去的几天开始学习。我正在关注此tutorial来创建博客。

我有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)        
}
}

控制器类 -

PostController -

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>

当我点击“添加评论”时,其名为comment / edit.gsp就在这里 -

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

1 个答案:

答案 0 :(得分:1)

我认为你的控制器中你正在使用params.postId,而在id参数中发送post.id的视图中,所以你应该在params.id中获取post id而不是params.postId,我想如果你改变你的在控制器中编辑函数:

  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])

现在你发送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" 
}
}

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

相关问题