简体   繁体   English

使用JSP遍历ArrayList

[英]Iterate over ArrayList with JSP

I have this Spring application. 我有这个Spring应用程序。 How to iteraete over allBooks in jsp file because in the way that i am trying it did not work. 如何对jsp文件中的allBooks进行迭代,因为在我尝试的方式中它不起作用。 I do not know how to tell the jsp file to get the list (allBooks). 我不知道如何告诉jsp文件获取列表(allBooks)。

Controller 控制者

@Controller
public class ListBooksController {

  @Autowired
  ListOfBooks listOfBooks;

  @RequestMapping("/books")
  public String books(Model model) {
    listOfBooks.addBooks();
    model.addAttribute("books", listOfBooks.listBooks());
    return "books";
  }

}

ListOfBooksImpl ListOfBooksImpl

@Repository
public class ListOfBooksImpl implements ListOfBooks {

  protected List<Book> allBooks = new ArrayList<>();

  @Override
  public void addBooks() {
    Book tutun = new Book("Тютюн", "Димитър Талев", 50);
    Book lotr = new Book("Lord Of The Rings", "Tolkin", 35);
    allBooks.add(tutun);
    allBooks.add(lotr);
  }

  @Override
  public List<Book> listBooks() {
    return allBooks;
  }
}

Book

public class Book {

  private String bookName;
  private String author;
  private int price;

  public Book(String bookName, String author, int price) {
    this.bookName = bookName;
    this.author = author;
    this.price = price;
  }

  public String getBookName() {
    return bookName;
  }

  public void setBookName(String bookName) {
    this.bookName = bookName;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public int getPrice() {
    return price;
  }

  public void setPrice(int price) {
    this.price = price;
  }
}

books.jsp books.jsp

<body>
<section>
    <div class="jumbotron">
        <div class="container">
            <h1>Books</h1>
            <p>All the available books in our store</p>
        </div>
    </div>
</section>
<section class="container">
    <div class="row">
        <c:forEach items="${allBooks}" var="book">
            <div class="col-sm-6 col-md-3" style="padding-bottom: 15px">
                <div class="thumbnail">
                    <div class="caption">
                        <h3>${book.name}</h3>
                        <p>${book.author}</p>
                        <p>$${book.price}</p>
                    </div>
                </div>
            </div>
        </c:forEach>
    </div>
</section>

It doesn't work because you pass object named "books" not "allBooks" 它不起作用,因为您传递了名为“ books”而不是“ allBooks”的对象

model.addAttribute("books", listOfBooks.listBooks());

change to 改成

model.addAttribute("allBooks", listOfBooks.listBooks());

and it will work 它会工作

EDIT 编辑

so add the books to your collection first, probably it's empty 因此,请先将这些书添加到您的收藏中,可能是空的

you have to call listOfBooks.addBooks() somewhere in your code (eg create another controller method if you are just practicing it) 您必须在代码中的某个地方调用listOfBooks.addBooks() (例如,如果您只是在练习它,则创建另一个控制器方法)

anyway you cant store the books like this in the array on the server because when you turn off your server all data will be gone, you have to persist data to the db 无论如何,您不能将这样的书存储在服务器上的阵列中,因为当您关闭服务器时,所有数据都将消失,您必须将数据持久保存到数据库中

EDIT 2 编辑2

According to your comments i can see that you dont understand how does model work, let me explain it for you: 根据您的评论,我可以看到您不了解模型是如何工作的,让我为您解释一下:

when you wrote 当你写

model.addAttribute("books", listOfBooks.listBooks());

you added an array of books(List) to your model and assigned it as "books" 您向模型中添加了一系列的书(列表),并将其分配为“书”

model is just a map containing key and value so you just simply added a key "books" with a value of List 模型只是一个包含键和值的地图,因此您只需添加具有List值的键“ books”

nextly 接下来

you wrote in your view 你写的观点

  <c:forEach items="${allBooks}" var="book">

allBooks obviously doesn't exist in the model map, it doesn't matter what is the variable name in the object ListOfBooksImpl when you manually assigned the key to the List, you have a books key instead allBooks显然不存在于模型图中,当您将键手动分配给List时,对象ListOfBooksImpl中的变量名是什么都没有关系

EDIT 3 编辑3

So if you want to add the books just create another controller method and invoke it first 因此,如果要添加书籍,只需创建另一个控制器方法并首先调用它

@Controller
public class ListBooksController {

  @Autowired
  ListOfBooks listOfBooks;

  @RequestMapping("/addBooks")
  public String addBooks() {
    listOfBooks.addBooks();
    return "books";
  }

  @RequestMapping("/books")
  public String books(Model model) {
    model.addAttribute("books", listOfBooks.listBooks());
    return "books";
  }

}

or if you want to add books only once 或者如果您只想添加一本书

@Controller
public class ListBooksController {

  @Autowired
  ListOfBooks listOfBooks;

  @RequestMapping("/books")
  public String books(Model model) {
    if(listOfBooks.listBooks().isEmpty())
         listOfBooks.addBooks();
    model.addAttribute("books", listOfBooks.listBooks());
    return "books";
  }

}

Try it please. 请尝试一下。

<c:forEach items="${books}" var="book">
        <div class="col-sm-6 col-md-3" style="padding-bottom: 15px">
            <div class="thumbnail">
                <div class="caption">
                    <h3>${book.bookName}</h3>
                    <p>${book.author}</p>
                    <p>${book.price}</p>
                </div>
            </div>
        </div>
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM