Instructor: Let's go ahead and get started.
So the first thing we're going to do is create
a project at the Spring Initializer website.
So I'll go to that start.spring.io.
Okay, so we're at the website.
The one thing I want to do is update
the project metadata for group and artifact.
So for the group, I'll give com.luv2code.springboot.
For the artifact ID, I'll give thymeleafdemo.
I'll move over here for dependencies.
I'll start off by giving the web dependency.
I'll also give the Thymeleaf dependency
and I'll also put in DevTools, just so I can
take advantage of that automatic restart.
Okay, so make sure these appear here on your screen.
From there, just go ahead and generate the project.
So I'll just kind of move over to my file system.
I'll move into my downloads directory here
and I'll simply unzip the file.
I'll also copy this folder and
I'll move it to a different location.
Just swing over to my left co-directory.
I'll just drop it in here in the dev-spring-boot.
Again, you can place it anywhere
you'd like in your file system
and I'll just paste this folder.
Excellent, so that's the file that was generated
by the Spring Initializer website, cool.
So let's go ahead and move into Eclipse
and I'll do the normal housekeeping work, right?
I'll stop all apps that are running
and also close all windows.
Now we'll go ahead and import
that Maven project into our ID.
I'll select file import.
In this wizard here, I'll just type Maven,
the filter on those Maven wizards
and I'll choose the existing Maven projects.
I'll just browse over to that directory
of where that project is located
and I'll choose my dev-spring-boot Thymeleaf demo.
Okay, that looks pretty good.
Great, so there's our new project
that we just imported, Thymeleaf demo.
Alrighty, so our project's open.
But we need to add work around for the Eclipse bug.
So let's go ahead and open up our pom.xml file
and you know the drill, right?
We just move down to this properties section
and then we add support for the maven-jar-plugin.version.
Alrighty, that's in place so we
can go ahead and save this file.
Going to get rid of the error message here in the pom.xml.
And then also we can simply go over
to our project and simply do a Maven update project
and then we'll be good to go.
The first thing I want to do actually
is just rename the project and give
it a more descriptive name.
I'll call it 30-thymeleafdemo-helloworld.
Awesome, so there's my new project
that was just renamed.
So the first thing I want to do is verify
some information in the pom.xml file.
So I'll go ahead and open up this file
and I'll expand the window.
I'll move down here and I'll just verify
the dependencies that were added.
Okay, so in this section, we can
see that they've added the Thymeleaf support
using that spring-boot-starter-thymeleaf.
This is great, we're kind of good to go here.
Now I'll go through and actually create a new package.
I'll use this new package to add my controllers.
I'll call this package .controller.
Okay, there's my new package.
In step two, I'm actually going to develop
a Spring MVC controller and I'll place it in this package.
The name of this controller, I'll call it DemoController.
Alrighty, I'll expand the window here.
This is my demo controller.
I'll write some quick comments to myself.
Basically we just created mapping for /hello.
So on top, I'll give the annotation for @Controller.
And then I'll set up a GetMapping
at the bottom here for /hello.
I'll create a method here called sayHello
that takes a Model.
I'll simply add an attribute to the model.
The name of the attribute is theDate
and I'll pass in a new Java Date instance.
And then finally, I simply return helloworld.
Remember, since we have the Thymeleaf dependency
in the Maven POM file, Spring Boot
will auto-configure to use Thymeleaf.
When we return helloworld, it's going
to look in src/main/resources/templates/helloworld.html.
Okay, so that's taken care of.
So in step three, we're going to create
a Thymeleaf template.
Be sure to create it in the templates directory.
So the name of this file, we'll
call it helloworld.html.
I'll expand this window and I'll start out
by just creating a very basic html file.
And the one thing that's important
when using Thymeleaf is to setup the XML namespace
for :th and we give the appropriate reference here.
So by setting up this namespace,
this will allow us to use Thymeleaf expressions in our page.
I'll set up a very basic head with a title.
And then I'll create this paragraph tag
and I'll make this a Thymeleaf expression th:text.
It's actually outputs and text.
I'll say Time on the server is
and this make this a + to do catenation
and then I reference theDate and it's
the date that we pull from theModel.
And remember in the controller,
we added data to the attribute so the time
the template can actually access the date
from that Spring MVC Model.
And then based on this information,
that's how we get the generated output for this page.
Okay, so let's go ahead and run this application.
The app is up and running.
Let's go ahead and swing over to our web browser
and we'll go to localhost:8080/hello.
And, all right, good job.
We get our desired output, so time on the server is
and they simply plugin that information, all right?
So remember, we get this by making use
of the Thymeleaf expressions based
on data that's placed in the model.
We can actually view the Page Source
just to see what the generated page looks like.
So there's our normal html, our body,
the paragraph and then notice how
all this information's been generated dynamically
by making use of Thymeleaf.
So this is pretty cool, I like it.
All right, so that was a nice little example
of building a Spring Boot application
that makes use of Thymeleaf, along
with those Thymeleaf expressions.
So I'd like to say good job.