我有以下代码:
def bindingMap = [title: 'The Lamb Lies Down On Broadway',
'players[0]': [name: 'Steve Hackett'],
'players[1]': [name: 'Peter Gabriel'],
'players[2]': [name: 'Tony Banks']]
我想得到"数组"来自上图的球员。 所以像下面这样的行就可以了:
def players = bindingMap.players
assert players == [[name: 'Steve Hackett'], [name: 'Peter Gabriel'], [name: 'Tony Banks']]
感谢您的帮助
答案 0 :(得分:3)
在此特定地图结构中,您可以findAll
其键以players
开头的条目,然后从这些条目中收集value
:
def bindingMap = [title: 'The Lamb Lies Down On Broadway',
'players[0]': [name: 'Steve Hackett'],
'players[1]': [name: 'Peter Gabriel'],
'players[2]': [name: 'Tony Banks']]
def players = bindingMap
.findAll { key, value -> key.startsWith("players") }*.value
assert players == [
[name: 'Steve Hackett'], [name: 'Peter Gabriel'], [name: 'Tony Banks']]
答案 1 :(得分:1)
原始问题并不表示这是关于Grails中的参数绑定,而是问题后面的注释。由于这是你正在做的事情,这可以完成,而无需自己编写任何参数解析代码。
// grails-app/controllers/demo/DemoController.groovy
package demo
class DemoController {
def index(MyCommand command) {
// command.people will be populated if the request
// includes parameters like...
// people[0].name=Jeff
// people[0].town='St. Louis'
// people[1].name='Matt'
// people[1].town='San Jose'
// ...
}
}
class MyCommand {
List<People> people
}
class People {
String name
String town
}
与编写自己的参数解析代码相比,这有很多好处。