|
Hi,
Some form elements generate a list of values. However, if there is only one element in the list, params.<value> will return that element, not a list. Is there any way to coerce the value to a list regardless of whether it is already a list? Basically, I would like the following behaviour: def items = params.data as List where items is either the original list, or a single-element list containing the original value. Note that the above will *not* work because "as List" converts a string into a list of single-character strings, which is similar behaviour to the ".each()" method on strings (which iterates over the characters). Cheers, Peter --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
On Monday 21 January 2008 03:24:07 pm Peter Ledbrook wrote:
> Hi, > > Some form elements generate a list of values. However, if there is > only one element in the list, params.<value> will return that element, > not a list. Is there any way to coerce the value to a list regardless > of whether it is already a list? Basically, I would like the following > behaviour: > > def items = params.data as List > > where items is either the original list, or a single-element list > containing the original value. Note that the above will *not* work > because "as List" converts a string into a list of single-character > strings, which is similar behaviour to the ".each()" method on strings > (which iterates over the characters). > class MyController def myAction = { // ... def items = paramToList( params['data'] ) // ... } private paramToList( String[] data ) { def list = [] (List)data?.each { list << it } return list } private paramToList( String data ) { return [ data ] } } --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by pledbrook
Maybe like this:
def items = [params.data].flatten() ? Cheers Christian
|
|
Thanks for the suggestions. I actually thought of another possibility:
override "asType()" via String's meta class so that def list = 'Some string' as List returns [ 'Some string' ]. This seems to work, so I might try that. Cheers, Peter --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
You can also do:
request.getParameterValues('data') which works when the application is running (params and request are both set from the query arguments), but will behave diferently in integration tests (where the parameters in params and request seem to be independent). On Jan 21, 2008, at 11:13 PM, Peter Ledbrook wrote: > Thanks for the suggestions. I actually thought of another possibility: > override "asType()" via String's meta class so that > > def list = 'Some string' as List > > returns [ 'Some string' ]. This seems to work, so I might try that. > > Cheers, > > Peter > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
| Powered by Nabble | Edit this page |
