Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Spring Basics Modeling, Storing, and Presenting Data Feeding Data to Our Thymeleaf Templates

Henry Lin
Henry Lin
11,636 Points

It seems the key name of a modelMap is used to access the field of a gif object in HTML right?

In order to verify this, I did this:

 @RequestMapping("/gif")
    public String gifDetails(ModelMap modelMap) {
        Gif gif = new Gif("cowboy-coder", LocalDate.of(2018,10,26),"Henry Lin",true);
        modelMap.put("changeKeyNameTest",gif);
        return "gif-details";
    }

And access it in html like this:

<img th:src="@{'/gifs/' + ${changeKeyNameTest.name} + '.gif'}" alt="gif" />

When I access the /gif page, it gave me:

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

Fri Oct 26 15:57:22 EDT 2018
There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "gif.name" (gif-details)

Does anyone know why? Please Help

3 Answers

I have the same problem, Can't not access keyName of ModelMap from html file

Martin Klestil
Martin Klestil
5,520 Points

I had a similar error, check out the error message from Intellj. It should show you what goes wrong with RequestMapping.

One way to see for sure what element Thymeleaf is generating, once you run the app and get the error, hit f12 and look at the error that should be highlighted somehow in that f12 display. In one case for me, I was creating a Gif object with an image name that included the file extension; so, Thymeleaf was generating image.gif.gif. If the app runs successfully, check again in the f12 area, in the 'elements' tab to see the correct path generated in <img> tag.