grails 如何将参数传递给控制器​​方法? [英] How does grails pass arguments to controller methods?

查看:21
本文介绍了grails 如何将参数传递给控制器​​方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 grails 控制器示例中,我见过 save(Model modelInstance) 和 save().我两个都试过了,都有效.我想象 grails 用参数实例化了 modelInstance.我的假设正确吗?

In grails controller examples, I have seen save(Model modelInstance) and save(). I tried them both, both of them works. I imagine grails instantiates the modelInstance with the params. Is my assumption correct?

我也注意到在 index(Integer max) 中,参数是否必须命名为 max?或者任何名称都可以,只要它是一个数字?

I also noticed in index(Integer max), does the param has to be named max? or any name would work as long as it is a number?

这些参数的传递在底层是如何工作的?

How does these passing of arguments work underneath?

推荐答案

如果你写一个这样的控制器...

If you write a controller like this...

class MyController {
    def actionOne() {
        // your code here
    }

    def actionTwo(int max) {
        // your code here
    }

    def actionThree(SomeCommandObject co) {
        // your code here
    }
}

Grails 编译器将把它变成这样的东西(不完全是这样,但这有效地描述了正在发生的事情,我认为可以解决您的问题)...

The Grails compiler will turn that in to something like this (not exactly this, but this describes effectively what is happening in a way that I think addresses your question)...

class MyController {
    def actionOne() {
        // Grails adds some code here to
        // do some stuff that the framework needs

        // your code here
    }

    // Grails generates this method...
    def actionTwo() {
        // the parameter doesn't have to be called
        // "max", it could be anything.
        int max = params.int('max')
        actionTwo(max)
    }

    def actionTwo(int max) {
        // Grails adds some code here to
        // do some stuff that the framework needs

        // your code here
    }

    // Grails generates this method...
    def actionThree() {
        def co = new SomeCommandObject()
        bindData co, params
        co.validate()
        actionThree(co)
    }

    def actionThree(SomeCommandObject co) {
        // Grails adds some code here to
        // do some stuff that the framework needs

        // your code here
    }
}

还有其他事情要做,比如强加 allowedMethods 检查、强加错误处理等.

There is other stuff going on to do things like impose allowedMethods checks, impose error handling, etc.

希望能帮到你.

这篇关于grails 如何将参数传递给控制器​​方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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