'Thymeleaf in IntelliJ: cannot resolve variables

Intellij Not Recognizing Model Variables in HTML. How to resolve model variables. I don't get any idea for this issue.

Here is my Controller

@Controller 
public void someController {
  @RequestMapping("/")
  public String someMethod() {
    model.addAttribute("message", "message");
    return "index";
}

And here is my "index.html"

<p th:text="${message}"> </p>

and of course in my html tag i'm using thymeleaf :

<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">

the problem is in myth:text="${message}" i see red squiggly lines saying that "Cannot resolve "message" variable..."



Solution 1:[1]

I've been ignoring that issue for as long as I've been using Thymeleaf. Even though it shows the squiggly lines, it should still work when you run the application.

IntelliJ would almost have to compile the code in the background to be able to automatically (and accurately, since you could have multiple methods who uses the same template) resolve the variables.

I've never given a tip like this, but after reading your comment that you just find the wiggly line annoying, I decided to suggest it anyways:

Disable the tip.

configure inspections

disable expression variables validation

I feel absolutely barbaric for posting this answer, forgive me SO

Solution 2:[2]

For recent versions of IntelliJ:

With the cursor on the variable, press Alt-Enter and you should see a menu option to "Declare external variable in comment annotation". When you select this option, you'll get a comment template with the cursor positioned to type in the data type of the variable.

When complete, you'll have something that looks like this:

    <!--/*@thymesVar id="productIds" type="java.util.Map"*/-->
    <div data-th-each="p : ${productIds}">

The Alt-enter menu doesn't seem to work within expressions such as ${#maps.isEmpty(productIds)}. In this case, manually creating the comment might make the UI get rid of the "unresolved" indicator.

Solution 3:[3]

these red lined could be very annoying I just removed www. from my thymleaf and it worked

In your .html thymeleaf file Replace line 1 with line 2
Line1 : <html xmlns:th="http://www.thymeleaf.org">

Line2 : <html xmlns:th="http://thymeleaf.org">

Solution 4:[4]

Just stumbled upon this while having the same problem. In my case it was caused by having the @SpringBootApplication class not in the same Maven module as the template and controller. Including the application module in the pom.xml with <scope>provided</scope> solved it for me.

I think @TwiN is right. IntelliJ indeed has to compile the whole thing and since it couldn't find an application it had no idea how to make a component lookup.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 kris larson
Solution 3 Sumedh Deshpande
Solution 4 Sebastian Reiners