Thymeleaf in IntelliJ: cannot resolve variables
Asked Answered
U

4

9

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..."

Unspoken answered 30/5, 2018 at 0:5 Comment(3)
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.Trilbi
yeah it's working fine, it's just a little bit annoying.Unspoken
I agree with you, but on the other hand, IntelliJ would almost have to compile the code in the background to be able to automatically resolve the variables.Trilbi
T
5

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

Trilbi answered 30/5, 2018 at 1:1 Comment(3)
This post can be improved. Downvote for "dead" links to screenshots.Easily
@naXa Thanks for the heads up, fixed dead links.Trilbi
I think this is the only workable solution particularly when using thymeleaf fragments as there are only so many external variables you can declare. The other reason is when using source control these errors keep showing up as a popup prior to commits + pushSimplicity
R
7

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.

Richardricharda answered 4/9, 2019 at 17:41 Comment(0)
T
5

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

Trilbi answered 30/5, 2018 at 1:1 Comment(3)
This post can be improved. Downvote for "dead" links to screenshots.Easily
@naXa Thanks for the heads up, fixed dead links.Trilbi
I think this is the only workable solution particularly when using thymeleaf fragments as there are only so many external variables you can declare. The other reason is when using source control these errors keep showing up as a popup prior to commits + pushSimplicity
C
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">

Creodont answered 7/4, 2021 at 6:12 Comment(2)
This worked for me but I wonder why!?Mignonmignonette
That's because the URI is not being recognized anymore. So the namespace can not be loaded and is of no use this way.Tholos
F
1

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.

Firebug answered 7/4, 2022 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.