是否可以从 JSTL forEach 循环中的列表访问特定的 element/object?

Is it possible to access particular element/object from a list in a JSTL forEach loop?

上面截图中table的代码

    <table>
    <tr>
        <th class="tg-address">Title</th>

        <th class="tg-houseprice">Price</th>

        <th  class="tg-crimerating">Author</th>

        <th  class="tg-schools">Category</th>

        <th  class="tg-university">Book Cover</th>

        <th  class="tg-bars">Amount in Stock</th>

        <th  class="tg-bars"> </th>
    </tr>

    <c:forEach var="o" items="${booksList}">
        <tr>
            <td width="3.5%" height="50"><c:out value="${o.title}"/></td>

            <td width="3.5%" height="50"><c:out value="${o.price}"/></td>

            <td width="3.5%" height="50"><c:out value="${o.author}"/></td>

            <td width="3.5%" height="50"><c:out value="${o.category}"/></td>

            <td width="3.5%" height="50"><c:out value="${o.image}"/></td>

            <td width="3.5%" height="50"><c:out value="${o.quantity}"/></td>

            <td width="3.5%" height="50"><a href="shoppingCart">Add Book to Shopping Cart</a></td>

         </tr>
    </c:forEach>

控制器方法将列表打印到 JSP 页面

    @RequestMapping(value = "viewallbooks", method = RequestMethod.GET)
public String bookList(@Valid Book book, Model model) {
    List<Book> booksList = new ArrayList<>();
    booksList = bookRepository.findAll();

    model.addAttribute("booksList", booksList);

    return "bookListPage";
}

我通过将 MySQL table 的值传递给 List 并将此列表传递给 c:forEach 循环来打印

的内容40=] 页 bookListPage。如果您查看 table and/or 屏幕截图,您会注意到我可以选择将图书添加到购物车(这是用户对象 Class 中图书对象的列表) , 这会将相关图书添加到当前登录的用户图书列表中。

屏幕截图中的table由不同的图书对象列表组成(即目前所有库存图书只有3本),我的问题是如何在之后访问相关的图书对象按对应的Add Book to Shopping Cartlink。

我在单击 link 后调用此 Controller 方法,它将找到当前登录的用户,并将我要传递的书添加到他们的列表中。但是我似乎无法解决中间的问题。

    @RequestMapping(value = "/shoppingCart", method = RequestMethod.GET)
public String shoppingCart(@Valid Book book, Model model) {

      // This gets the currently logged in user
      Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
      String username = loggedInUser.getName(); // Authentication for 
      User user = userRepository.findByUsername(username);

      // This will add the book to the user's booklist
      user.saveBookToShoppingCart(book);

    return "shoppingcart";
}

也许您没有将 jsp 代码封装在 form 标签中,例如:

<form action="#" th:action="@{shoppingCart}" th:object="${o}" method="post">
  <td width="3.5%" height="50">
    <a href="shoppingCart">Add Book to Shopping Cart</a>
  </td>
</form>

我认为既然你要传递一个对象,你必须使用 post 方法。但这只有在您想 create/modify 一本书时才有意义。由于您只想检索哪本书被选中添加到购物车中,我建议您也遵循 lucumt 的建议并仅传递 ID,然后检索该书。像这样:

<td width="3.5%" height="50">
  <a href="<c:url value='/shoppingcart/${o.id}' />">
    Add Book to Shopping Cart
  </a>
</td>

最后,您的控制器方法应该如下所示:

@RequestMapping(value = " /shoppingcart/{bookId}", method=RequestMethod.GET)
public String shoppingCart(@PathVariable("bookId")int bookId, Model model) {

  // This gets the currently logged in user
  Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
  String username = loggedInUser.getName(); // Authentication for 
  User user = userRepository.findByUsername(username);

  //Retrieve book
  Book book = bookService.findBookById(bookId);

  // This will add the book to the user's booklist
  user.saveBookToShoppingCart(book);

  return "shoppingcart";
}