Exercise 16: Model Data - jkneal/spring-angular-train GitHub Wiki

Goals

Learn how to invoke services and populate model data for views.

Instructions

  1. In edu.train.shop, create a new controller class named 'ShopController' that maps to the request path '/shop'

  2. Now create a method named 'getAllProducts' that maps to '/shop' and accepts Http Get methods. The method return type should be a String

  3. In getAllProducts, add a method argument of type org.springframework.ui.Model and with name 'model'

  4. Populate the model with the following attributes (note to invoke the repositories you will need to autowire them as dependencies for the controller):

Name Value
store Invoke the edu.train.store.StoreRepository method find with the values: "Joe's Sports Store", "Joe"
products Invoke the edu.train.product.ProductRepository method findAll
  1. Finally in getAllProducts, return the String "shop"

  2. In the same controller, expose a model attribute named "shoppingOptions". This should be a List with the following values: "In Store", "Online", and "Order"

VERIFICATION

  1. Start the training application and go to the app home page
  2. Click on the link 'Exercise 16'. Verify you get the result:

Joe's Sports Store

joesports.com

Products

Product Id: 1

Product Name: Basketball

Product Id: 2

Product Name: Football

Product Id: 3

Product Name: Baseball

Product Id: 4

Product Name: Softball

Product Id: 5

Product Name: Tennisball

Shopping Options

Option: In Store

Option: Online

Option: Order