Exercise Solutions: Enum PracticeΒΆ

  1. Before adding any other fields to Planets, go to PlanetController and update the index handler to pass in a Model class argument (eg. Model model).

    @GetMapping()
    public String displayIndex(Model model) {
       model.addAttribute("planets", Planets.values());
       return "index";
    }
    

    Back to the exercises

  1. 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>
    

    Back to the exercises

  1. Update the template to display the planet names.

    <ol>
       <li th:each="planet : ${planets}" th:text="${planet.name}"></li>
    </ol>
    

    Back to the exercises

  1. Change the index template to display a table of each planet name and its year in earth days.

    1. In templates/index, create a table element and add the appropriate bootstrap class.

    2. The table can be styled to your liking.

    3. 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>
    

    Back to the exercises