Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 method declaration on its getProductsByCategory method:

    List<Product> getProductsByCategory(String category);
  4. Open the service implementation class ProductServiceImpl and add an implementation...