2012-12-25 172 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)。