How does grails pass arguments to controller methods?

后端 未结 1 1899
醉话见心
醉话见心 2020-12-16 07:18

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

相关标签:
1条回答
  • 2020-12-16 07:59

    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
        }
    }
    

    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
        }
    }
    

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

    I hope that helps.

    0 讨论(0)
提交回复
热议问题