Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In order to create a dynamic detail page that changes according to a URI, we'll need to define a `@PathVariable` and include a placeholder in the URI of the `@RequestMapping` annotation. In this video, you'll learn exactly how to do that.
Correction
At the beginning of the video (0:51), I say that we are asking for the "compiler-bot" from our repository on each call to the controller method. In code, we're actually referencing the "android-explosion" GIF. Though I misspoke there, the idea is the same: we are fetching the same GIF every time, and we need to be able to fetch a GIF that depends on the request. In short, we need to make our unchanging controller method dynamic!
Working with Attributes in Thymeleaf
To learn more about all the supported attributes in Thymeleaf, as well as custom attributes, check out the Thymeleaf docs for Setting Attribute Values.
Custom Date Formatting
If you'd like to custom format the date, you'll want to alter your Thymeleaf template. By default, you're limited in what you can display with a LocalDate
object. What you'll need to do is add the ability to format dates by adding a dialect to the template engine. A nice one to use is org.thymeleaf.extras.java8time.dialect.Java8TimeDialect
. To use it, you'll need to take care of three items:
(1) Add the thymeleaf-extras-java8time dependency to your Gradle build file:
compile 'org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE'
(2) Add the Java8TimeDialect
to the template engine upon startup by altering your AppConfig
class to include the following
package com.teamtreehouse.giflib;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
@EnableAutoConfiguration
@ComponentScan
public class AppConfig implements CommandLineRunner {
@Autowired
private SpringTemplateEngine templateEngine;
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
@Override
public void run(String... args) throws Exception {
templateEngine.addDialect(new Java8TimeDialect());
}
}
Notice I've @Autowired
the SpringTemplateEngine
, which will be created by Spring at application startup. Then, since I've implemented CommandLineRunner
, which requires only an implementation of the run(String...)
method, our run
method will be called after SpringApplication.run
is finished.
In our run
method is where you finally see the Java8TimeDialect
added to the template engine.
(3) Format a date in your Thymeleaf template
You now have available all the options listed on the dialect's Github repo. The snippets listed there are what you'd use in your Thymeleaf template.
For example, if ${gif.dateUploaded}
referenced January 1, 2015, then ${#temporals.format(gif.dateUploaded, 'MMM d yyyy')}
would give you "Jan 1 2015".
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up