Grails上Groovy的初学者问题

时间:2009-04-21 16:21:59

标签: exception grails groovy

我是Groovy的新手,我想修改我在互联网上发现的一个例子(博客一)。 我已经定义了两个类Post和Comment如下:

class Post {    

static hasMany = [comments:Comment]

String title
String teaser
String content    
Date lastUpdated

SortedSet comments

static constraints = {
    title(nullable:false, blank:false, length:1..50)
    teaser(length:0..100)
    content(nullable:false, blank:false)
    lastUpdated(nullable:true)

}

String toString() {title}   
}

Post post
String comment
String name
String url
String email
Date dateCreated = new Date()


static belongsTo = Post

static constraints = {
    comment(nullable:false, blank:false)
    name(nullable:false, blank:false)
    url(nullable:true, blank:true, url:true)
    email(nullable:true, blank:true, email:true) 
    dateCreated(nullable:true)
    post(nullable:false)
}

public int compareTo(Object o) {
    return dateCreated.compareTo(o.dateCreated)
}

String toString() {comment}
}    

到目前为止一切都好。

我已将控制器定义如下:

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.postId)   

    if(comment.save()) {        
        redirect(
                controller:'post',
                action:'list',
                id:params.postId)
    } 
}

}

class PostController {

def defaultAction = 'list'

def index = {
    render("En el norte de Alabama")
}

def edit = {
    render(view:'edit', model:[post:loadPost(params.id)])
}

def save = {
    def post = loadPost(params.id)
    post.lastUpdated = new Date()

    post.properties = params
    if(post.save()) {
        print "id " + post.id + "\n"     
        def offset = post.id - 1
        print "offset " + offset

        redirect(action:'list', offset:offset)
    } else {
        render(view:'edit', model:[post:post])
    }
}

def list = {
    if(!params.max)params.max = 1

    [ postList: Post.list( params ) ]

}


def view = {
    render(view:'view', model:[post:Post.get(params.id)])
}

private loadPost(id) {
    def post = new Post();
    if(id) {
        post = Post.get(id)
    }
    return post
}
}

最后我定义了views / post / list

如下:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
  <head><title>My postList</title>
  </head>
     <body>
        <div class="nav">
    <g:link controller="post" action="edit">New post</g:link>            
        </div>
          <span class="menuButton"><H1>Last posts</H1></span> 

 <table border = 1><tr><td>

<g:each in="${postList}" var="post">
 <div>
   <h2>${post.title}</h2>
       <p>Last update: <g:formatDate date="${post.lastUpdated}" 
          format="yyyy-MMM-dd HH:mm"/></p>
    <p><i>${post?.content}</i></p>      
 </div>

 <div class="paginateButtons">
    <g:link controller="comment" action="edit" params="[postId:post.id]">
    Add comment         
    </g:link>            
</div> 


   <g:each in="${post.comments}" var="comt">        
     ${comt.comment} <br />
   </g:each>        
</g:each>
<td></tr></table>


 <div class="paginateButtons">
        <g:paginate max="1" next="Next" prev="Prev." 
           total="${postCount == null ? Post.count(): postCount}" />
</div>      
</body>
</html>

我可以毫无问题地生成新帖子,但是当我想对我的帖子发表评论时,我最多可以为每个帖子添加一条评论。当我尝试放更多时,我得到一个ClassCastException,如下所示:

Message: Comment
Caused by: Error processing GroovyPageView: Comment
Class: /WEB-INF/grails-app/views/post/list.gsp
At Line: [-1] 

org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Comment

    at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.handleException(GroovyPageView.java:134)

    at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderWithTemplateEngine(GroovyPageView.java:112)

    at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderMergedOutputModel(GroovyPageView.java:86)

    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)

    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)

    at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:294)

    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)

    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1124)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361)

    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)

    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)

    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:334)

    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)

    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:293)

    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:269)

    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:261)

    at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:181)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:171)

    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:110)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.codehaus.groovy.grails.web.servlet.filter.GrailsReloadServletFilter.doFilterInternal(GrailsReloadServletFilter.java:101)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:65)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)

    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)

    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361)

    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)

    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)

    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)

    at org.mortbay.jetty.Server.handle(Server.java:324)

    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534)

    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:864)

    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:533)

    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:207)

    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:403)

    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)

    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:522)

Caused by: java.lang.ClassCastException: Comment

    at java.util.TreeMap.compare(TreeMap.java:1093)

    at java.util.TreeMap.put(TreeMap.java:465)

    at java.util.TreeSet.add(TreeSet.java:210)

    at java.util.AbstractCollection.addAll(AbstractCollection.java:318)

    at java.util.TreeSet.addAll(TreeSet.java:258)

    at D__Workspace_groovypublish_grails_app_views_post_list_gsp$_run_closure5.doCall(D__Workspace_groovypublish_grails_app_views_post_list_gsp:52)

    at D__Workspace_groovypublish_grails_app_views_post_list_gsp.run(D__Workspace_groovypublish_grails_app_views_post_list_gsp:21)

当试图放置嵌套的“每个”时,似乎问题出现在list.gsp文件中

任何提示

提前致谢

路易斯

2 个答案:

答案 0 :(得分:4)

好吧,从我在这里可以看到的,你使用SortedSet作为Post中的Comments的容器类型。这很好,但你必须使注释实现Comparable,因为这是所有包含SortedSet的对象的要求。您可能希望您的注释按dateCreated进行排序,因此您必须在某处进行SAY(比如您需要满足界面的compareTo函数)。

您是不是只使用List?似乎你不想在早期日期添加评论。

无论如何,如果不是这样,请评论,我会再看一下。

答案 1 :(得分:0)

我同意比尔詹姆斯,但我认为你应该注意到你对这个问题的分析有所改进。您的帖子包含有关您问题的良好数据。事实上,你拥有所需的一切。让我们看看那个堆栈跟踪。

以下是TreeMap.java的代码:1093

private int compare(K k1, K k2) {
    return (comparator==null ? ((Comparable</*-*/K>)k1).compareTo(k2)
                             : comparator.compare((K)k1, (K)k2));
}

从这里你可以看到注释被投射到Comparable是问题所在。稍微了解一下TreeMap,TreeSet和Comparable可能会让你解决这个问题。

请不要将此帖解释为以任何方式质疑您的问题或问题。你的问题很好,很好问。这有望成为帮助您学会捕鱼的另一项数据。