Fendy Kusuma
Fendy Kusuma

Reputation: 672

Grails - How to remove multiple params from controller

Im new to grails and using version of 2.1.1

I have been working params for passing data from view to controller ie I have submit URL to controller :

http://example.com/save?param1=one&param2=two&param3=three&param4=four

and then the URL will handle with save() in controller, I use it and I will passing some param into another view. Code like this

redirect action: "index", params:params

but when I success to redirect, all param is include (param1,param2, param3 & param4)

http://example.com/index?param1=one&param2=two&param3=three&param4=four

I just want to have param1 in my index view. Currently I do this remove param using :

params.remove("param2")
params.remove("param3")
params.remove("param4")

Is there any simply way to remove multiple params in grails?

Upvotes: 1

Views: 6720

Answers (2)

Igor Artamonov
Igor Artamonov

Reputation: 35951

If you need just one parameter, the easiest way will be creating a new object:

redirect action: "index", params: [param1: params.param1]

Upvotes: 3

Burt Beckwith
Burt Beckwith

Reputation: 75681

Igor's answer is probably what you want, but you can remove multiple keys in one line with this:

['param1', 'param2', 'param3'].each { params.remove it }

or you can remove all but one with this:

params.keySet().asList().each { if ('param1' != it) params.remove(it) }

I'm using asList() to copy the set to avoid a ConcurrentModificationException

Upvotes: 4

Related Questions