Grails 表单对象数组提交方案2

页面
<input type="text" name="user.1-name" />
<input type="text" name="user.2-name" />

后台:
def users = params.remove('user')
Map split = users?.inject([:]) { map, param ->

                List list = param.key.split('-')
                if (list.size() != 2) { // only allow for 1 '-' in a param
                    return map
                }
                String firstKey = list.first()
                String lastKey = list.last()
                if (!map.get(firstKey)) {
                    map."$firstKey" = [:] // init map for this key
                }
                map."$firstKey"."$lastKey" = param.value
                return map
            }

for(String key : map.keySet()){
            println "key :"+key+"   value:"+map.get(key)

            def user = new User(map.get(key))
            println user.name
        }

你可能感兴趣的:(grails)