AI Features

Showing the Cart

Learn how to load data through the reactive repository, its drawbacks, and a solution to use a nonreactive CRUD repository.

Showing the cart on the web page

Before we start manipulating the cart, it would be useful to show it on the web page.

For this, we need to retool ourHomeController. To do that, we first need to inject our new repositories:

@Controller //1
public class HomeController {
private ItemRepository itemRepository;
private CartRepository cartRepository;
public HomeController(ItemRepository itemRepository, CartRepository cartRepository) { //2
this.itemRepository = itemRepository;
this.cartRepository = cartRepository;
}
...
}
HomeController getting repositories via constructor injection

Here’s the breakdown of the code written above:

  1. In line 1, @Controller signals this as a Spring Web controller that serves up templated views.

  2. In line 6, the constructor is used by Spring to inject the itemRepository ...