2011-10-05 41 views
0

我是Play Framework的入門者。當我傳遞參數時遇到問題。
我想將視圖中的集合傳遞給控制器​​。我不知道該怎麼做。當我從視圖中獲取集合時,我總是會得到「空」。 我下面的代碼:
守則控制器:Play Framework如何將collection傳遞給action create()?

public static void create(List<Book> books) throws Exception { 
    for(Book book : books){ 
      System.out.println(book.get(0).author) // i got null :(
    } 
} 

代碼在HTML

Book 1: 
<input type="text" name="books.author" /> 
<input type="text" name="books.title" /> 
Book 2: 
<input type="text" name="books.author" /> 
<input type="text" name="books.title" /> 

當我提出,我想2條記錄添加到數據庫中包括第一冊和第二冊。請支持我

感謝

回答

4

您可以通過simplying數組指針添加到您的HTML代碼

Book 1: 
<input type="text" name="books[0].author" /> 
<input type="text" name="books[0].title" /> 
Book 2: 
<input type="text" name="books[1].author" /> 
<input type="text" name="books[1].title" /> 

我已經測試了這種解決方案使這項工作,並能正常工作。

另請注意,您的println將無法編譯,因爲您正在Book對象上調用get(0),而不是List對象。如果您只打印book.author,則會根據需要輸出作者。

+0

問題是,我不知道有多少書將被提交。有時2或5或更多。那麼,有沒有其他的方式,而不是索引每本書? –

+0

你可以用JavaScript做些事情,所以對於每本新書,你都可以輸出代碼,以便它具有正確的索引標識符。 – Codemwnci

0

如果有人需要爲dyanmically添加和刪除書籍(JQUERY需要)的JavaScript的例子:

<script type="text/javascript"> 
    $(document).ready(function() { 
     var bookCount=0; 
     $('#btnAddBook').click(function() { 
      bookCount++; 
      //newElem = go up a to the parent div then grab the previous container 
      var newElem = $(this).parent().prev().clone().attr('id', 'book[' + bookCount + ']'); 
      //for each input inside the div, change the index to the latest bookCount 
      $(newElem).find("input").each(function(){ 
       var name = $(this).attr('name'); 
       var leftBracket = name.indexOf("["); 
       var rightBracket = name.indexOf("]"); 
       var beforeBracketString = name.substring(0,leftBracket+1);//+1 to include the bracket 
       var afterBracketString = name.substring(rightBracket); 
       $(this).attr('name', beforeBracketString + bookCount + afterBracketString); 
      }); 
      //insert it at the end of the books 
      $(this).parent().prev().after(newElem); 
      $(newElem).find("input").each(function(){ 
        $(this).attr('id', $(this).attr('id') + bookCount); 
       }); 
      //enable the remove button 
      $('#btnRemovebook').removeAttr('disabled'); 
      //If we are at 16 divs, disable the add button 
      if (bookCount == 15) 
       $(this).attr('disabled','disabled'); 
     }); 
     $('#btnRemoveBook').click(function() { 
      bookCount--; 
      //remove the last book div 
      $(this).parent().prev().remove(); 
      //in case add was disabled, enable it 
      $('#btnAddbook').removeAttr('disabled'); 
      //never let them remove the last book div 
      if (bookCount == 0) 
       $(this).attr('disabled','disabled'); 
     }); 
    }); 
</script> 
<!-- HTML Snippet --> 
<div id="book[0]"> 
    <label> Book: </label> 
    <input type="text" name="books[0].author" value="Author" /> 
    <input type="text" name="books[0].title" value="Title" /> 
</div> 
<div> 
    <input type="button" id="btnAddbook" value="Add another book" /> 
    <input type="button" id="btnRemovebook" value="Remove last book" disabled="disabled" /> 
</div> 
<!-- REST of the HTML --> 
相關問題