Grails 2.5.0 - 命令对象处理带有JSON

时间:2015-09-27 18:39:52

标签: grails command-objects

我有一个表单设置为向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>

1 个答案:

答案 0 :(得分:0)

尝试发送这样的请求:

request.forwardURI = 'list1[0].listprop1=a&list1[0].listprop2=b&prop1=c&prop2=d'

更好

更好的方法是使用带有g:remoteForm标记的Ajax。

相关问题