I am trying to use Grails Command Object to filter action parameters. But I want to set a default value if the parameter is not present in URL.
class ListCommand {
String order = 'desc'
String sort = 'startDate'
}
def list(ListCommand cmd) {
println cmd.order
}
I thought the behaviour would be same as if I was creating a domain object. I don't want to handle each parameter in the action like:
cmd.order = params.order ?: 'desc'
if you always use such action declarations:
def list(ListCommand cmd) { ... }
or
def list = {ListCommand cmd -> ...}
you may try this:
class ListCommand {
String order
String sort
def beforeValidate() {
order = order ?: 'desc'
sort = sort ?: 'startDate'
}
}
because in those action definitions validate() method always calls for command objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With