Grails에서 완전히 새로운 기능입니다. 나는 지난 며칠 동안 배우기 시작했다. 나는이 블로그를 만들기 위해 tutorial을 팔로우하고 있습니다.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)
}
}
컨트롤러 Cla SSES -
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>
내가 코멘트 추가를 클릭하면 다음 이는 해당 호출 코멘트/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}"/>
에 못 미치기 때문에옵니다. 제발 도와주세요 ..
GSP 페이지에 postId 변수가 있는지 확인하기 위해 텍스트에 숨겨진 속성을 만들 수 있습니까? –
확인하고 있습니다. –
숨겨진 속성을 텍스트로 변경합니다. 그 어떤 이드도 보이지 않습니다. 수단 ID가 반입되지 않습니다. –