gpt4 book ai didi

grails - Grails中的验证错误

转载 作者:行者123 更新时间:2023-12-02 15:32:27 24 4
gpt4 key购买 nike

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

Controller 类

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

最佳答案

我认为在您的 Controller 中您正在使用params.postId,而从发送id参数中的post.id的 View 来看,因此您应该在params.id中获取post id而不是params.postId,我想如果您在 Controller 为:

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

并且在您的后期 Controller 中,您还有电话:
render(view:'edit', model:[post:post])

现在,您正在发送要查看的帖子对象:edit,但是在edit 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"
}
}

让我知道您是否还有任何问题:)

关于grails - Grails中的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17485685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com