Exercise Solutions: Enum PracticeΒΆ
Before adding any other fields to
Planets
, go toPlanetController
and update the index handler to pass in aModel
class argument (eg.Model model
).@GetMapping() public String displayIndex(Model model) { model.addAttribute("planets", Planets.values()); return "index"; }
In
templates/index
, create a list element and use the template variable you have just defined in the controller to list all of the planet values on the page.<ol> <li th:each="planet : ${planets}" th:text="${planet}"></li> </ol>
Update the template to display the planet names.
<ol> <li th:each="planet : ${planets}" th:text="${planet.name}"></li> </ol>
Change the index template to display a table of each planet name and its year in earth days.
In
templates/index
, create a table element and add the appropriate bootstrap class.The table can be styled to your liking.
Add a message to let the users know what data you are displaying with the
yearLength
field.
1 2 3 4 5 6 7 8 9 10 11
<table th:each="planet : ${planets}" class="table table-striped"> <thead > <tr> <th th:text="${planet.name}"> </th> </tr> </thead> <tr> <td th:text="${planet.yearLength} + ' Earth days in one year.'"></td> </tr> </table>