如果从脚手架模板调用,则找不到控制器方法 [英] Controller method not found if called from scaffolding template

查看:119
本文介绍了如果从脚手架模板调用,则找不到控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始了一个新的Grails项目,我尽可能地使用脚手架功能。我使用grails install-templates安装了这些模板,并对它们进行了修改,以便在某些方法可用的情况下它们依赖于当前控制器(例如,如果可用,show方法将调用renderShow方法)。

我遇到的问题是,当从脚手架模板调用该方法时,找不到它,因此引发异常。如果从实际的控制器调用相同的方法,它会按预期工作。



我缺少什么?



<为了解决这个问题,我创建了一个小型项目。有趣的提交是这样的:
https://github.com/deigote/GrailsS​​caffoldingMethodCallProblem / commit / 0fce966bad6e8004a7133511149c09de54be24bf



在运行应用程序时,如果进入testDemoMethod(... / GrailsS​​caffoldingMethodCallProblem / demo / testDemoMethod),你好,我'm a method呈现,并且打印出以下内容:

  [public java.lang.Object grailsscaffoldingmethodcallproblem.DemoController.this $ 2但是如果你去了索引(... / GrailsS​​caffoldingMethodCallProblem / demo / demo),那么你可能会发现, testDemoMethod)打印一个空列表,我得到一个异常:

 消息:没有签名的方法:grailsscaffoldingmethodcallproblem.DemoController.demoMethod( )适用于参数类型:()values:[] 
Line |方法
- >> 11 | index在grailsscaffoldingmethodcallproblem.DemoController

任何线索为什么会发生这种情况或如何避开它?



编辑
Ian Robert的答案解决了以下问题:

解决方案

因为脚手架模板中这个没有引用声明静态scaffold = true 。当您在控制器类 A 中使用运行时动态脚手架时,脚手架模板用于生成一个单独的类(将其称为 B ,但实际上它可能具有相同的名称作为 A ,但是可以通过不同的类加载器加载),并且每个 A 实例将委托给 B 的实例 B 定义但 A 不显式覆盖。



如果您需要从脚手架代码中获取真实控制器实例,你可能不得不使用类似于

 导入grails.util.GrailsWebUtil 
...
的技巧def index(){
def realController = GrailsWebUtil.getControllerFromRequest(request)
println realController.respondsTo('demoMethod')
realController.demoMethod()
redirect(action:list ,params:params)
}


I recently started a new Grails project and I'm trying to use the scaffold feature as much as I can. I installed the templates with grails install-templates and modified them so they rely on the current controller if certain methods are available (for example, the show method would call the renderShow method if available).

The problem I have is that when that method is called from the scaffold template, it's not found hence an exception is thrown. If the same method is called from the actual controller, it works as expected.

What I'm missing?

To ilustrate the question, I created a small project. The interesting commit is this one: https://github.com/deigote/GrailsScaffoldingMethodCallProblem/commit/0fce966bad6e8004a7133511149c09de54be24bf

When running the app, if you go to the testDemoMethod (.../GrailsScaffoldingMethodCallProblem/demo/testDemoMethod) you "Hi, I'm a method" rendered, and the following is printed:

[public java.lang.Object grailsscaffoldingmethodcallproblem.DemoController.this$2$demoMethod()]

But if you go to the index (.../GrailsScaffoldingMethodCallProblem/demo/testDemoMethod) an empty list is printed and I obtain an exception:

Message: No signature of method: grailsscaffoldingmethodcallproblem.DemoController.demoMethod() is applicable for argument types: () values: []
   Line | Method
->>  11 | index    in grailsscaffoldingmethodcallproblem.DemoController

Any clue why is this happening or how to get around it?

Edit: Ian Robert's answer solved the question: Controller method not found if called from scaffolding template

解决方案

Because this within the scaffolding template does not refer to the controller class that declares the static scaffold = true. When you use runtime dynamic scaffolding in a controller class A the scaffolding template is used to generate a separate class (call it B, though in fact it will probably have the same name as A but be loaded via a different classloader) and each instance of A will delegate to an instance of B any actions that B defines but A does not explicitly override.

If you need to get at the real controller instance from inside the scaffolded code you'll probably have to use a trick like

import grails.util.GrailsWebUtil
...
def index() { 
    def realController = GrailsWebUtil.getControllerFromRequest(request)
    println realController.respondsTo('demoMethod')  
    realController.demoMethod() 
    redirect(action: "list", params: params) 
}

这篇关于如果从脚手架模板调用,则找不到控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆