Home Grails form g:textField parameters to Controller
Reply: 2

Grails form g:textField parameters to Controller

philburns
1#
philburns Published in 2017-12-04 17:19:11Z

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. By the help of this tutorial I've written:

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:

// 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. 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. 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

elixir
2#
elixir Reply to 2017-12-04 17:40:03Z

try with this

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

def myAction = {
    doSomething(params.parameter1, params.parameter2)
}
user615274
3#
user615274 Reply to 2017-12-04 22:22:50Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302595 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO