grails如何将参数传递给控制器​​方法?

fro*_*roi 4 grails

在grails控制器示例中,我看到了save(Model modelInstance)和save().我试过他们两个,两个都有效.我想grails用params实例化modelInstance.我的假设是否正确?

我还注意到索引(整数最大值),param必须命名为max吗?或者只要是数字,任何名称都可以使用?

这些论证的传递如何在下面起作用?

Jef*_*own 13

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

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

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

    def actionThree(SomeCommandObject co) {
        // your code here
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

还有其他一些事情要做,例如强制执行allowedMethods检查,强制执行错误处理等.

我希望有所帮助.