gpt4 book ai didi

grails - grails 2.2 中的 Controller 继承

转载 作者:行者123 更新时间:2023-12-02 10:42:20 27 4
gpt4 key购买 nike

我有一些代码正在尝试从 Grails 1.3.7 移植到 Grails 2.2。

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

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.

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

PS:对于那些可以的人:请创建一个 grails-2.2 标签

最佳答案

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

关于grails - grails 2.2 中的 Controller 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14028149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com