Lesson Description

The "MVC with Thymeleaf" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:

Josh demonstrates the capabilities of Thymeleaf, a Java template engine for processing and creating HTML, XML, JavaScript, CSS, and text. A controller is added to the application, and the `mvc` endpoint output is mapped to a `users.html` server-side template.

Preview
Close

Transcript from the "MVC with Thymeleaf" Lesson

[00:00:00]
>> Josh Long: So thymeleaf is one of a half a dozen different supported server-side templating technologies. I'm going to create a server side template here called Users. I'm gonna paste all this in there, right, so I've just got this. I'm not going to write JavaScript for you live on stage because I respect you too much.

[00:00:17]
But basically it's just there, It's a table, ID name, username, street, blah, blah, blah. So for each user in the attribute variable called users in the model, render this table cell, and then that's it. Okay, so now we go back to our code here. I'll create an MVC user class MVC controller.

[00:00:38]
And notice that I don't have ResponseBody here, right? ResponseBody tells Spring MVC to not interpret the return value of one of these methods as the name of a view. So normally if I return a string from one of these controller handler methods, that tells Spring MVC to look up a view by a convention.

[00:00:58]
So it'll look up users by source main resources, blah HTML. So this'll get turned into, behind the scenes, there's something called a ViewResolver. Src/main/resources/templates + BLAH + .html, okay, there you go. So this is the convention and if you don't have response body, then Spring MVC is going to be looking for a view name.

[00:01:26]
So when you do the response body, it tells Spring, okay, I might have some other intent for this response that's coming from this controller handler method. And then usually that's just the object to be rendered into JSON or XML or whatever. So what about the data? So I'm going to inject a model, okay, there we go.

[00:01:45]
Import the class, here we go, and I'll say model.addAttributes, users, and I'm gonna use the declarative client. One of the nice things I love about IntelliJ is that it has this feature where I can do this. I can say declarative users, client users. See how that automatically added that constructor for me and the dependency injected type and all that.

[00:02:14]
So, Is that gonna work? It goes in the templates folder. The IDE tried to warn me, but I didn't listen. Okay, so there you go, simple static server side stuff. I know most of us are doing client side stuff, but this is still here. And I did a whole livestream with the person who created htmx a month ago or two.

[00:02:45]
You can actually use this server side template support. Plus there's good support for rendering fragments in Spring. So if I want to send two out of band HTML bodies or chunks and have them replace different islands at the same time. You can do that with HTML spring mvc.

[00:03:03]
You can actually return a collection. Instead of a string, you return a collection of model and view or a collection of. I think you can do a collection of string as well, but multiple views will get rendered at the same time.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now