首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grails 2.2中的控制器继承

grails 2.2中的控制器继承
EN

Stack Overflow用户
提问于 2012-12-25 14:06:20
回答 1查看 2.1K关注 0票数 4

我有一些代码要从Grails 1.3.7移植到Grails 2.2。

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

代码语言:javascript
复制
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() {

    }
}

派生类的一个示例是

代码语言:javascript
复制
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编译我的应用程序时,我得到了以下错误:

代码语言:javascript
复制
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.

此模式不再受支持吗?我尝试将abstract添加到BaseController声明中(在Grails 1.3.7中这不是必需的),但这似乎没有任何区别。我是在清理之后编译代码的,如果这很重要的话。

PS:致那些有能力的人:请创建一个grails-2.2标签

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-25 19:22:45

删除def grailsApplication -该字段已经作为类型化字段(GrailsApplication)通过AST转换添加到类字节码中,因此您的def字段将创建另一个具有较弱类型(Object)的get/set对。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14028149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档