Exercise 16: Model Data - jkneal/spring-angular-train GitHub Wiki
Goals
Learn how to invoke services and populate model data for views.
Instructions
-
In
edu.train.shop, create a new controller class named 'ShopController' that maps to the request path '/shop' -
Now create a method named 'getAllProducts' that maps to '/shop' and accepts Http Get methods. The method return type should be a String
-
In getAllProducts, add a method argument of type
org.springframework.ui.Modeland with name 'model' -
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 |
-
Finally in getAllProducts, return the String "shop"
-
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
- Start the training application and go to the app home page
- 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