Play 框架 如何将集合传递给动作 create()?

发布于 2024-12-08 10:01:48 字数 658 浏览 0 评论 0原文

我是 Play 框架的初学者。我在传递参数时遇到问题。
我想将集合从视图传递到控制器。我不知道该怎么做。当我从视图中获取集合时,我总是得到“null”。 我的代码如下:
控制器中的代码:

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 条记录,包括 Book1 和 Book2。请支持我

谢谢

I am starter with Play Framework. I got a problem when i passed parameters.
I want to pass a collection from view to controller. And i do not know how to do this. I always get "null" when i get a collection from view.
My code below:
Code in controller:

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

Code in 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" />

When i submit, i want to add 2 records into database include Book1 and Book2. Please support me

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2024-12-15 10:01:48

您只需将数组指示符添加到您的 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 对象。如果您只是 println book.author,它会根据需要输出作者。

You can make this work by simplying add the array indicator to your HTML code

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" />

I have tested this solution, and it works fine.

Also note that your println will not compile, as you are calling get(0) on the Book object, and not the List object. If you just println book.author, it outputs the author as required.

梦幻的味道 2024-12-15 10:01:48

如果有人需要动态添加和删除书籍的 Javascript 示例(需要 JQUERY):

<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 -->

In case anyone needs an example of the Javascript for dyanmically adding and removing books (JQUERY needed):

<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 -->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文