2015-09-27 44 views
0

我有一個表單設置爲發送POST請求到Grails控制器,該控制器使用命令對象作爲其一個參數。命令對象包含一些正確綁定的屬性以及不能正確綁定的項目列表。我正在做的是通過POST請求正常發送其他參數,但將列表作爲JSON字符串包裝起來,因爲我不確定通過POST發送列表的另一種方式(除了XML字符串之外) 。獲取命令對象從字符串中正確綁定列表的最後一步是什麼,或者有更好的方式將列表發送到命令對象?Grails 2.5.0 - 命令對象使用JSON處理POST請求

編輯: 下面是一個簡化的版本:

測試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> 
+0

您需要包含命令對象以及您的表單(或者您如何撰寫您發佈的數據),以便任何人都能就如何解決這個問題給出一個很好的答案。 –

+0

我添加了相關信息。我加入forwardURI(我從Fiddler簡化了)。 – Anonymous1

回答

0

嘗試發送請求是這樣的:

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

更妙

更好的方法是使用Ajax與g:remoteForm標籤。

+0

表單將它編碼爲list1%5B0%5D.listprop1 = a&list1%5B0%5D.listprop2 = b&prop1 = c&prop2 = d當我在Fiddler中看到它時。那是問題嗎?我收到一個錯誤「類[com.mypackage.MyCommand]類的屬性[list1]不能爲空」 – Anonymous1

+0

您可以發佈表單gsp代碼嗎? –

+0

我添加了代碼。請注意,AngularJS正在填充list1array。很抱歉,這個簡化代碼中的名字越來越時髦。 – Anonymous1