Class 4 Prep Exercises
Get some hands-on practice with views in MVC. Work on these exercises within the CheeseMVC app that was created during our Intro to Spring Boot: Views video lessons.
- Add a
descriptionfield to the application. This will involve:- Modifying the static
cheeseslist to be aHashMap* object with key/value pairs that correspond to the name/description of the cheese. - Adding a new form input field in
cheese/add.htmlto allow for submission of the description. - Modifying the
processAddCheeseFormmethod to insert the name and description of the new cheese into thecheesesHashMap. ** - Displaying the description field in the
cheese/index.htmlview template. ***
- Modifying the static
- Add some styles to
resources/static/css/styles.cssto improve the look and feel of your application. We added stylesheets to our application in Intro to Spring Boot: Views (Part 3) - Answer the following questions:
- What does it mean that "Thymeleaf templates are natural templates"?
- What are the benefits of using a
fragments.htmlbase template? - Describe some best practices involving template organization and annotating request paths. (Hint: these are covered in Intro to Spring Boot: Views (Part 1).)
* You can review HashMap syntax here.
** To use multiple @RequestParams, use this syntax:
public String multiParamExample (@RequestParam String one, @RequestParam String two)
*** When iterating over a map, the components of each item can be accessed using .key and .value:
<tr th:each="mapItem : ${map}">
<td th:text="${mapItem.key}">Key</td>
<td th:text="${mapItem.value}">Value</td>
</tr>
Also, you may find Thymeleaf syntax like the following useful:
<p th:text="${oneThing + '! ' + anotherThing}"></p>
Where oneThing and anotherThing are data passed in from the controller. For example, if the value of oneThing were "This rocks" and the value of anotherThing were "Doesn't it?", the above would render in the browser as:
This rocks! Doesn't it?
Don't forget to use this Thymeleaf Cheatsheet if you need a reference!