简体   繁体   English

Grails将g:textField参数形式传递给Controller

[英]Grails form g:textField parameters to Controller

I'm quite new to Grails and I've modified some previously existing button functionality so that it now should send 2 parameters to the controller. 我对Grails还是很陌生,并且已经修改了一些以前存在的按钮功能,因此现在应该向控制器发送2个参数。 By the help of this tutorial I've written: 借助于本教程,我写了:

gsp : gsp

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Some Title</h4></div>
            <div class="modal-body">
                <g:form>
                    <div class="input-group">
                        <span class="input-group-addon">Parameter 1</span>
                        <g:textField name="parameter1" size="24" class="form-control" />
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon">Parameter 2</span>
                        <g:textField name="parameter2" size="24" class="form-control" />
                    </div>
                    <g:actionSubmit action="myAction" value="Go" class="btn btn-success"
                                    data-toggle="tooltip" data-placement="top"
                                    title="${grailsApplication.config.myTitle}" />
                </g:form>
            </div>
        </div>
    </div>
</div>

MyController : MyController

// Though it is not mentioned in the tutorial, I've added 2 class properties.
// Otherwise I'd get a groovy.lang.MissingPropertyException
def parameter1
def parameter2

def myAction = {
    doSomething(parameter1, parameter2)
}

// previously : 
// def myAction = {
//     doSomething()
// }

The two parameters are 'null' when the method is invoked. 调用该方法时,两个参数为“ null”。 What can I do to make the parameters being handled to the controller method? 如何使要处理的参数成为控制器方法?

File names, imports and things alike should not be the problem here as the call of the function does well without parameters. 文件名,导入和类似内容在这里不应该成为问题,因为在没有参数的情况下函数的调用效果很好。 The modal is new to the code as it is used to get the values of parameter1 and parameter2. 模态对于代码来说是新的,因为它用于获取parameter1和parameter2的值。 The modal itself works well, as long as the parameters are not in the signature of the controller's method. 只要参数不在控制器方法的签名中,模态本身就可以很好地工作。

I've tried "grails clean" according to this answer . 我已经根据此答案尝试“清理干净”。 Grails version 2.5.4 Grails版本2.5.4

try with this 试试这个

// def parameter1 // you do not need this
// def parameter2 // you do not need this

def myAction = {
    doSomething(params.parameter1, params.parameter2)
}

I'm not sure of the exact version but from grails 2.x instead of closures you must use methods to define actions of a controller. 我不确定确切的版本,但是从grails 2.x而不是闭包开始,您必须使用方法来定义控制器的动作。 This code should work 此代码应该有效

def myAction() {
    doSomething(params.parameter1, params.parameter2)
}

Or it could be like this, assuming both parameters are strings 或者假设两个参数都是字符串

def myAction(final String parameter1, final String parameter2) {
    doSomething(parameter1, params)
}

In the example that you share the form does not define a target action so you should go to the current action using the post method 在您共享表单的示例中,未定义目标操作,因此您应使用post方法转到当前操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM