Create a Model
In the next several pages, we will be making updates to codingevents
to demonstrate model creation,
how models relate to data, and the practice of model binding. The first of these steps is to move data
handling out of our controller classes and into a model class. As we discussed on the previous page, the
controller class is not responsible for holding data.
In codingevents
, we’ll remove the ArrayList
data from EventController
and create a proper
Java class to deal with event items. We’ll then update our controller methods to take
advantage of the new model and its properties, rather than the strings stored in the list.
Lastly, because the controller is updating, the template variables it relies upon will also need to
change to reflect the model properties.
Create a Model Class - Video
The starter code for this video is found at the add-bootstrap branch
of CodingEventsJava
repository. The final code presented in this video is found on the
create-model branch
. As always, code along with the videos on your own codingevents
project.
Create a Model Class - Text
Like controllers, model classes are conventionally located in a models
package. Structurally, model classes most closely resemble the kinds of classes we practiced
making at the start of this course, before introducing Spring Boot. In other words,
models are plain old Java objects, or POJOs.
To create a model to shape event data, we include a field for name
.
Of course, we’ll also like at least one constructor and some getters and setters.
In models/Event.java
:
|
|
Now that we’re working to move the data handling out from the controller classes and into a class of its own,
we’ll need to update the POST
handler that creates new events. Update the .add()
method inside of
processCreateEventForm
to add a new Event
instance:
|
|
And you’ll notice, we’re adding a different type of data to the ArrayList
, so we’ll have to update that too:
private static List<Event> events = new ArrayList<>();
Back in the events/index.html
template, update the HTML to use the Event
object’s fields, rather than
simply strings.
<td th:text="${event.name}"></td>
The syntax event.fieldName
runs a getter method behind the scenes in order to access a field.
Add a Model Property - Video
The starter code for this video is found at the create-model branch
of the codingevents-demo
repo. The final code presented in this video is found on the add-property branch
. As always, code along to the videos on your own codingevents
project.
Add a Model Property - Text
To round out the Event
class, we’ll add a description
field to showcase what our events are all about.
Updates to models/Event.java
:
|
|
Now that our data is object-oriented, it’s quick and easy to add a new property affiliated with an event. If after
this, we decide to add a date
or location
field, we would simply follow the pattern established. Before,
with events stored as name strings, we would have had more changes to make in order to add other information fields
to the shape of the data.
Update both the events/create.html
and events/index.html
templates to create an event object with a
description field and to display that description along with the event’s name.
events/create.html
:
|
|
events/index.html
:
<td th:text="${event.description}"></td>
Lastly, add a parameter to the
processCreateEventForm
to handle the form submission and pass the description
value into the creation of the Event object.
EventController
:
|
|
Check Your Understanding
True/False: Model code is framework independent.