grails - grails 如何将参数传递给 Controller ​​方法?

标签 grails

在 grails Controller 示例中,我看到了 save(Model modelInstance) 和 save()。我两个都试过了,都有效。我想象 grails 用参数实例化了 modelInstance。我的假设正确吗?

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

这些参数的传递在下面是如何工作的?

最佳答案

如果你写一个这样的 Controller ......

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

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

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

Grails 编译器将把它变成这样的东西(不完全是这个,但这有效地描述了正在发生的事情,我认为它可以解决你的问题)......
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 检查、强加错误处理等。

我希望这有帮助。

关于grails - grails 如何将参数传递给 Controller ​​方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23511688/

相关文章:

grails - 在 GORM Grails 中保存和查询日期

Grails:如何使用非域抽象基类模拟域对象的属性

grails - 在 Grails 中 ServletContext 之后的这个运算符是什么?

select - Grails如何使用选择标记之类的复选框标记

grails - 基本密码散列 Grails

grails - 如何替换脚手架 Controller Action 并仍在Grails中调用原始 Action ?

multithreading - Grails从线程访问请求/ session 时未找到线程绑定(bind)请求

tomcat - 如何配置 grails 应用程序以将日志、堆栈跟踪和错误写入特定文件

grails - 从grails项目执行gant脚本

grails - grails/groovy大括号语法问题