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

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

问题描述

在grails控制器的例子中,我看到了save(Model modelInstance)和save()。我尝试了他们两个,他们都有效。我想象着grails用params实例化modelInstance。我的假设是正确的吗?



我也注意到在索引(整数最大)中,参数是否必须被命名为max?或者任何名字只要是一个数字就可以工作?



这些传递参数如何在底下工作?

解决方案

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

  class MyController {
def actionOne(){
//你的代码在这里
}

def actionTwo(int max){
//你的代码在这里
}
$ b $ def actionThree(SomeCommandObject co){
//您的代码在这里
}
}

Grails编译器会将其转化为类似的内容(不完全是这样,但是这样可以有效地描述正在发生的事情,以我认为解决您的问题的方式)......

  class MyController {
def actionOne(){
// Grails在这里添加一些代码到
//做一些框架需要的东西

//你的代码在这里
}

// Grails生成这个方法.. 。
def actionTwo(){
//该参数不必被称为
//max,它可以是任何东西。
int max = params.int('max')
actionTwo(max)
}

def actionTwo(int max){
// Grails在这里添加一些代码到
//做一些框架需要的东西

//你的代码在这里
}

// Grails生成这个方法...
def actionThree(){
def co = new SomeCommandObject()
bindData co,params
co.validate()
actionThree(co)


def actionThree(SomeCommandObject co){
// Grails在这里添加一些代码到
//做一些框架需要的东西

//你的代码在这里


$ / code $ / pre

做强制allowedMethods检查,强加错误处理等等。



我希望有帮助。


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?

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

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.

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

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