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 Feeding Data to Our Thymeleaf Templates

JT Keller
JT Keller
12,731 Points

Spring Basics - Intellij Not Recognizing Model Variables in HTML

I believe I found the information in the link below (it's a bug in IntelliJ and Spring Boot support), but wanted to confirm this with Chris or other Treehouse students. Intellij isn't able to resolve the Spring model variables in the HTML files (ie ${gif.name}). I do have the necessary getters and setters in the Gif class.

IntelliJ Bug: http://stackoverflow.com/questions/26520824/how-to-map-spring-controllers-to-their-thymeleaf-viewsintellij14

Here's My Temp Fix:

<div class="gif-detail container">
    <div class="frame row">
        <div class="col s12">
            <!--/*@thymesVar id="gif" type="com.thoughtstreamllc.giflib.model.Gif"*/-->
            <img th:src="@{'/gifs/' + ${gif.name} + '.gif'}" alt="gif" />
            <a href="#" class="mark favorite"></a>
        </div>
    </div>

HTML Snippet:

<div class="gif-detail container">
    <div class="frame row">
        <div class="col s12">
            <img th:src="@{'/gifs/' + ${gif.name} + '.gif'}" alt="gif" />
            <a href="#" class="mark favorite"></a>
        </div>
    </div>

Gif Class:

package com.thoughtstreamllc.giflib.model;

import java.time.LocalDate;

public class Gif {
    private String name;
    private LocalDate dateUploaded;
    private String username;
    private boolean favorite;

    public Gif(String name, LocalDate dateUploaded, String username, boolean favorite) {
        this.name = name;
        this.dateUploaded = dateUploaded;
        this.username = username;
        this.favorite = favorite;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LocalDate getDateUploaded() {
        return dateUploaded;
    }

    public void setDateUploaded(LocalDate dateUploaded) {
        this.dateUploaded = dateUploaded;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public boolean isFavorite() {
        return favorite;
    }

    public void setFavorite(boolean favorite) {
        this.favorite = favorite;
    }
}

8 Answers

Hi,

Issue of Intellij Not Recognizing Model Variables in HTML can be resolved by replacing xmlns:

Replace:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

WITH:

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

And its working great!!

Could you explain why this fix the issue? why is http://www.thymeleaf.org not valid?

Dmitry Ponomarenko
Dmitry Ponomarenko
18,175 Points

Yeah, I have the same problem. Project works fine, but these errors in IDE are hurting my perfectionism :)

Hi all

So what is the solution to the original concern "The Thymeleaf variables inside the .html docs are indeed marked as errors" . why are they not marked as errors in the instructor view, but marked as errors in our views?

Thanks

I got this fixed properly by

  • changing modelMap.put("gif", gif); to modelMap.addAttribute("gif", gif); in GifController.java
  • adding @Configuration annotation to AppConfig.java

Thanks! That fixed the issue for me.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

JT;

What version of IntelliJ are you using? I went through the course with IntelliJ 15.0.2 and had no issues.

Ken

JT Keller
JT Keller
12,731 Points

Ken,

I'm using 15.0.3 IntelliJ IDEA Ultimate. The code builds and runs fine, it's just that the IDE is indicating that the variable can't be resolved. I've tried bumping up and down the dependency versions without any luck. I'm guessing that the Spring Boot plugin isn't working correctly with the latest version. Just to make sure it wasn't an issue with my code, I downloaded and imported the project from the site and the errors still occur.

Ken Alger
Ken Alger
Treehouse Teacher

JT;

Ah yes, I miss read your original post I guess. The Thymeleaf variables inside the .html docs are indeed marked as errors on my system as well. I figured it was a glitch in the templating engine interface. Fortunately with Spring if your Thymeleaf does have errors your page doesn't render at all, right? You get a server error message, or at least that is where I think all those error messages I continue to get originate. :stuck_out_tongue:

Thanks for pointing it out here in the forum. I'm certain others will have similar questions.

Happy coding,
Ken

I feel your perfectionist pain, Dimitry --I too am getting the same errors,

and I thought these Treehouse courses got QA'd.

Must've slipped by the QA department on this course.


Here's my review of the course:

https://teamtreehouse.com/community/spring-basics-course-review


It's nice to have a few more Java related courses on Treehouse, but I think

most of the Java interest remains focused on using Java for Android apps

--until they have courses on developing Android apps with other languages such as:

1.) NDK (native development toolkit) using C/C++

2.) Scala

3.) Corona/Lua

4.) Frink:

https://futureboy.us/frinkdocs/android.html

5.) SL4A scripting:

https://github.com/damonkohler/sl4a

Dmitry Ponomarenko
Dmitry Ponomarenko
18,175 Points

To be honest, most of Java positions here in Ukraine are not related to Android. That's why I am trying to learn stuff demanded by HRs the most: Spring, Hibernate, J2EE etc. and I am happy to see that Treehouse started adding advanced and useful stuff for Java career.

HI Dimitry, What you say is interesting.

I know some of my Eastern European friends use J2EE (Java Enterprise Edition),

with the J2EE applications usually having a HTML, CSS, JavaScript, Struts, and/or Tiles front-end,

along side a java middle layer, and a database (following the MVC or MVVM or MOVE development patterns).

European employers who favoring this setup usually want you to know some ORM (Object Relation Mapping) frameworks and sometimes know about utilizing Tomcat servlet containers. That's the sort of thing I've seen on some European job boards..


Re: Hibernate

I've been looking forward to Treehouse covering Hibernate for some time now,

and so I'm really looking forward to the Hibernate course coming up this month (Feb 2016):

https://teamtreehouse.com/library/hibernate-basics/upcoming.

J2EE is rarely mentioned in Craigslist postings for the U.S. West Coast.

I guess it's all about where you live in the world that determines what's "hot".

Sagar Thakkar
Sagar Thakkar
8,814 Points

Ravi Kantarao is right it solved the issue he is true