我设置了一个表单来向 Grails 控制器发送 POST 请求,该控制器使用命令对象作为其一个参数。命令对象包含一些正确绑定的属性以及未正确绑定的项目列表。我正在做的是通常通过 POST 请求发送其他参数,但将列表包装为 JSON 字符串,因为我不确定通过 POST 发送列表的另一种方式(例如,XML 字符串) . 让命令对象从字符串中正确绑定列表的最后一步是什么,或者有没有更好的方法将列表发送到命令对象?
编辑:这是一个简化版本:
测试 URI:
request.forwardURI = 'list1=[{"listprop1":"a","listprop2":"b"}]&prop1=c&prop2=d'
命令对象:
class MyListCommand {
String listprop1
String listprop2
static constraints = {
listprop1 nullable: true
listprop2 nullable: true
}
}
class MyCommand {
List<MyListCommand> list1 = [].withLazyDefault {
new MyListCommand('[]')
}
String prop1
String prop2
static constraints = {
prop1 nullable: true
prop2 nullable: true
}
}
表格:
<form action="${createLink(action: 'myAction')}" method="post">
<div ng-repeat="list1 in list1array">
<input type="hidden" name="list1[{{ $index }}].listprop1" value="{{list1.listprop1}}"/>
<input type="hidden" name="list1[{{ $index }}].listprop2" value="{{list1.listprop2}}"/>
</div>
<input name="prop1" type="text">
<input name="prop2" type="text">
</form>