Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring Basics Modeling, Storing, and Presenting Data Using @PathVariable to Create a Dynamic Detail Page

Dialect is declared twice: org.thymeleaf.extras.java8time.dialect.Java8TimeDialect

I'm trying to format the date uploaded using Java8TimeDialect. But i got an error after i run the app. It's say "Dialect is declared twice: org.thymeleaf.extras.java8time.dialect.Java8TimeDialect"

Has anyone got this problem?

My gradle file

group 'com.teamtreehouse'
version '1.0-SNAPSHOT'

buildscript {
    repositories{
        mavenCentral()
    }
    dependencies{
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.4.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
    compile 'org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE'
}

AppConfig.java

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());
    }
}

3 Answers

Roman Mayer
Roman Mayer
10,925 Points

I don't understand what Chris did when he changed the AppConfig.java file. I changed mine like this instead:

package com.teamtreehouse.giflib;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;


@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class AppConfig { 

    public static void main(String[] args) {
        SpringApplication.run(AppConfig.class, args);
    }

    @Bean
    public Java8TimeDialect java8TimeDialect() {
        return new Java8TimeDialect();
    }
}

The changes in gif-details.html are wrong as well. Use this:

<p th:text="'Uploaded on ' + ${#temporals.format(gif.dateUploaded, 'MMM dd yyyy')}">Uploaded on Oct 30 2015</p>

Let me know if it works. And please mark as best answer.

Jose Patria
Jose Patria
1,600 Points

Maybe he try to don't use the @Bean annotation because we have never used before. But your code works perfectly. Roman Mayer

Boban Talevski
Boban Talevski
24,793 Points

Well, with latest updates at this time, it seems we don't need any additional configuration (except adding the dependency of course) to use this library.

It doesn't work as in the Teacher's notes anymore, and Roman Mayer's suggestion works, but even that is not needed anymore according to this.

So I reverted my AppConfig.java to its original form and it just works. I have commented out the code I've tried adding so far just to note the suggested updates to the class up to this point.

package com.bobantalevski.giflib;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan
public class AppConfig {

//    @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());
//    }

//    @Bean
//    public Java8TimeDialect java8TimeDialect() {
//        return new Java8TimeDialect();
//    }
}