控制器@Mixin只需重新编译运行的应用程序即可运行 [英] Controller @Mixin just works after recompile of running app

查看:104
本文介绍了控制器@Mixin只需重新编译运行的应用程序即可运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最新的grails 2.3.0项目中,我使用 @Mixin 注释来混合助手类来保持我的控制器 more DRY。

如果在控制器内进行了一些更改以强制重新编译控制器,则mixin正在工作。在初始编译之后( grails run-app ),帮助程序没有混入 - 我得到了一个 MissingMethodException 尝试访问辅助类中的方法。



以下是我的帮助: src / groovy

  class ProjectHelper {
def withProject(id,Closure c){
def project = Project.get(id)
if(project){
c.call project
} else {
flash.message ='找不到项目!'
渲染视图:'myView'
返回
}
}
}

控制器它使用 ProjectHelper

  @Mixin(ProjectHelper)
class ProjectController {
def index(){
withProject params.projectId,{project - >
//对项目做些什么



$ c


$ b

当我使用 grails clean 清理项目并启动应用程序时,访问 project / index $ b

处理请求时发生MissingMethodException:
[GET] /< myApp> / project /
没有方法签名:< myPackage> .withProject()适用于参数类型:
(java.lang.String,< myPackage> .ProjectController $ _index_closure1_closure10)values:
[ 1,< myPackage> .ProjectController $ _index_closure1_closure10 @ 40d889b5]

ReportController (例如,添加一个空格)grails可以编译2个源文件,并且可以使用 withProject 方法。访问项目/索引按预期工作。



这里出了什么问题?它是一个错误还是我错过了什么?



更新



我完全错过了,使用 grails.util.Mixin 给我另外一个例外( MissingPropertyException )由于缺少访问混合的类属性(在我的例子中: flash )(参见 JIRA这个问题),它与 groovy.lang.Mixin (在重新编译之后)。



有没有办法在运行时手动重新编译/注入/混入 groovy.lang.Mixin ,或者是否必须为找到另一个错误处理?



任何建议?

解决方案

在使用mixins执行类似的代码重用模式时,我经常遇到同样的 MissingMethodException

在我的例子中,将 groovy.lang.Mixin 更改为 grails.util.Mixin (或者更具体地说,为 grails.util.Mixin 添加一个导入到我的控制器)完全解决了这个问题。



至于无法访问控制器变量,您可能会停留在等待 GRAILS-9905 即将解决。不过,我应该注意到,在缺陷讨论中列出了一些建议的解决方法。


Within my latest grails 2.3.0 project I'm using the @Mixin annotation to mixin a helper class to keep my controller more DRY.

The mixin is just working if a made some changes within the controller to force a recompile of the controller. After the initial compile (grails run-app) the helper isn't mixed in - I get a MissingMethodException trying to access a method from the helper class.

Here is my helper witin src/groovy:

class ProjectHelper {
    def withProject(id, Closure c) {
        def project = Project.get(id)
        if (project) {
            c.call project
        } else {
            flash.message = 'Project not found!'
            render view: 'myView'
            return
        }
    }
}

And (one of) the controller which uses the ProjectHelper:

@Mixin(ProjectHelper)
class ProjectController {
    def index() {
        withProject params.projectId, {project ->
            // do something with the project
        }
    }
}

When I cleane the project using grails clean and starting the app, I get the following error after accessing project/index:

MissingMethodException occurred when processing request: 
    [GET] /<myApp>/project/
    No signature of method: <myPackage>.withProject() is applicable for argument types: 
    (java.lang.String, <myPackage>.ProjectController$_index_closure1_closure10) values: 
    [1, <myPackage>.ProjectController$_index_closure1_closure10@40d889b5]

After some changes in ReportController (e.g. adding a single space) grails compiles 2 source files and the method withProject could be used. Accessing project/index works as expected.

What's going wrong here? Is it a bug or do I miss something?

Update

It turns out that I totally missed, that using the grails.util.Mixin gives me another exception (MissingPropertyException) due to missing access to the mixed in class properties (in my case: flash) (see JIRA this issue) which is working with groovy.lang.Mixin (after recompile).

Is there a way to manually recompile/inject/mixin the groovy.lang.Mixin at runtime or do I have to find another error handling for the else part till the issue is fixed?

Any suggestions?

解决方案

I had been regularly running into the same MissingMethodException when following a similar code reuse pattern using mixins.

In my case, changing groovy.lang.Mixin to grails.util.Mixin (or more specifically, adding an import for grails.util.Mixin to my controller) resolved the issue completely.

As for not having access to the controller variables, you may be stuck waiting for GRAILS-9905 to be resolved. I should note that there are some suggested work-arounds listed on the defect discussion, though.

这篇关于控制器@Mixin只需重新编译运行的应用程序即可运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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