Java Spring Basics Modeling, Storing, and Presenting Data Create an Index Page That Presents a List of Data

This application has no explicit mapping for /error, so you are seeing this as a fallback.

When I tried to fun this in my browser I got this error:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Dec 06 17:10:27 GMT 2019 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/home.html]" - line 48, col 13)

Everything worked fine before that. Would anyone be able to tell me where the error might be, as I don't understand what it is saying.

I don't know how to paste code into this, so I can't share my code as it is right now, but if you can tell me where the error is then I can paste that line.

Any help would be much appreciated,

Thank you! Alastair

2 Answers

You have a typo in your home.html file. You are missing a quote for the class attribute of your div tag. Here is the line with the typo:

 <div th:each="gif : ${gifs}" class=col s12 l4">//<--- missing a quote just after the equals sign of class attribute

Thank you Isaiah! I literally never would have gotten that without your help!

You're welcome. Happy coding! :)

Unless I see the code, I can't tell you exactly what is going wrong here.

However, I would suggest that you ensure that you have the "@Controller" annotation for your controller class so that it is actually being correctly recognized as such.

Now I've figured out how to put the code in as code, I hope that this is more helpful. Though there is a lot of it.

Gif Controller

package com.teamtreehouse.giflib.controller;

import com.teamtreehouse.giflib.data.GifRepository;
import com.teamtreehouse.giflib.model.Gif;
import jdk.vm.ci.meta.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.List;

@Controller
public class GifController {
    @Autowired
    private GifRepository gifRepository;

    @RequestMapping(value = "/")
    public String listGifs(ModelMap modelMap) {
        List<Gif> allGifs = gifRepository.getAllGifs();
        modelMap.put("gifs",allGifs);
        return "home";
    }

    @RequestMapping("/gif/{name}")
    public String gifDetails(@PathVariable String name, ModelMap modelMap) {
        Gif gif = gifRepository.findByName(name);
        modelMap.put("gif",gif);
        return "gif-details";
    }

}

GifRepository

package com.teamtreehouse.giflib.data;

import com.teamtreehouse.giflib.model.Gif;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Component
public class GifRepository {
    private static final List<Gif> ALL_GIFS = Arrays.asList(
            new Gif("android-explosion", LocalDate.of(2015,2,13), "Chris Ramacciotti", false),
            new Gif("ben-and-mike", LocalDate.of(2015,10,30), "Ben Jakuben", true),
            new Gif("book-dominos", LocalDate.of(2015,9,15), "Craig Dennis", false),
            new Gif("compiler-bot", LocalDate.of(2015,2,13), "Ada Lovelace", true),
            new Gif("cowboy-coder", LocalDate.of(2015,2,13), "Grace Hopper", false),
            new Gif("infinite-andrew", LocalDate.of(2015,8,23), "Marissa Mayer", true)
    );

    public Gif findByName(String name) {
        for (Gif gif : ALL_GIFS) {
            if(gif.getName().equals(name)) {
                return gif;
            }
        }
        return null;
    }

    public List<Gif> getAllGifs() {
        return ALL_GIFS;
    }

}

from home.html

    <div class="gifs container">
        <div class="row">
            <div th:each="gif : ${gifs}" class=col s12 l4">
                <a th:href="@{'/gif' + ${gif.name}}">
                    <img th:src="@{'/gifs/' + ${gif.name} + '.gif'}" />
                    <a href="#" th:class="@{${gif.favorite} ? 'un' : ''} + 'mark favorite'"></a>
                </a>
            </div>
        </div>
    </div>

from gif-details.html

        <div class="row">
            <div class="col s12 l6">
                <div class="meta row">
                    <div class="col s3">
                        <img class="circle" src="http://api.adorable.io/avatars/205/avatar" alt="avatar" />
                    </div>
                    <div class="col s9">
                        <h4 th:text="${gif.username}">Craig Dennis</h4>
                        <p th:text="'Uploaded on ' + ${gif.dateUploaded}">Uploaded on Oct 30 2015</p>
                    </div>
                </div>
            </div>