O'Reilly logo

Spring MVC Beginner’s Guide by Amuthan G

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – showing products based on category

Let's add a category view to the products page using the path variable:

  1. Open the ProductRepository interface and add one more method declaration on its getProductsByCategory method:
    List<Product> getProductsByCategory(String category);
  2. Open the implementation class InMemoryProductRepository and add an implementation for the previously declared method as follows:
    public List<Product> getProductsByCategory(String category) {
      List<Product> productsByCategory = new ArrayList<Product>();
        
      for(Product product: listOfProducts) {
        if(category.equalsIgnoreCase(product.getCategory())){
          productsByCategory.add(product);
        }
      }
      
      return productsByCategory;
    }
  3. Similarly, open the ProductService interface and add one more ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required