2012-12-25 174 views
4

我有一些代码,我试图从Grails 1.3.7移植到Grails 2.2。控制器继承grails 2.2

目前的问题是,我有一个BaseController类,它定义了一些便利方法,以及从它继承的特定控制器(实际由Grails实例化的控制器)。

package com.fxpal.querium 

import grails.converters.JSON 
import groovy.lang.Closure; 

abstract class BaseController { 

    protected def executeSafely(Closure c) { 
     def resp = null 
     try { 
      populateContext(); 
      resp = c() 
     } 
     catch(Exception ex) { 
      resp = [error: ex.message] 
      ex.printStackTrace() 
     } 
     def json = resp as JSON 
     return json 
    } 

    protected void populateContext() { 

    } 
} 

派生类的一个例子是

package com.fxpal.querium 

import grails.converters.JSON 
import grails.plugins.springsecurity.Secured 
import javax.servlet.http.HttpServletResponse 

@Secured(['IS_AUTHENTICATED_REMEMBERED']) 
class DocumentController extends BaseController { 

    def grailsApplication 

    @Secured(['IS_AUTHENTICATED_ANONYMOUSLY']) 
    def getText = { 
     try { 
      String text = new URL(grailsApplication.config.querium.docurl + params.paperId).text 
      render contentType: 'text/plain', text: text    
     } 
     catch(Exception ex) { 
      render contentType: 'text/plain', text: "Error loading document: ${ex.getMessage()}; please retry" 
     } 
    } 

... 
} 

这个工作Grails中1.3.7。当我尝试使用Grails 2.2编译我的应用程序时,出现以下错误:

C:\code\querium\AppServer-grails-2\grails-app\controllers\com\fxpal\querium\DocumentController.groovy: -1: The return ty 
pe of java.lang.Object getGrailsApplication() in com.fxpal.querium.DocumentController is incompatible with org.codehaus. 
groovy.grails.commons.GrailsApplication getGrailsApplication() in com.fxpal.querium.BaseController 
. At [-1:-1] @ line -1, column -1. 

此模式不再受支持吗?我试着添加abstractBaseController声明(在Grails 1.3.7中这不是必需的),但这似乎没有任何区别。如果有问题,我在清理后编译我的代码。

PS:对于那些谁可以:请创建grails-2.2标签

回答

13

删除def grailsApplication - 现场通过一个AST转换已经加入到类的字节码作为一个类型的字段(GrailsApplication),所以你def领域创建了第二个获得/设置较弱的类型(Object)。