I\'m writing a short web form application using spring boot and thymeleaf on IntelliJ, but it seems that in the html file, all fields in the model cannot be resolved. Here is my
You can use Alt+Enter shortcut to invoke intention "Declare external variable in comment annotation" in order to get rid of "unresolved model attribute" in your views.
Add the following code to your html
file:
<!--/* Workaround for bug https://youtrack.jetbrains.com/issue/IDEA-132738 -->
<!--@thymesVar id="post" type="your.package.Post"-->
<!--@thymesVar id="title" type="String"-->
<!--@thymesVar id="content" type="String"-->
<!--*/-->
If you use extensions objects constructed automatically by ThymeLeaf, such as #temporals
from thymeleaf-extras-java8time
for conversion of java.time
objects:
<span th:text="${#temporals.format(person.birthDate,'yyyy-MM-dd')}"></span>
and IntelliJ cannot resolve them, use similar code, and just add #
in front of the object name:
<!--@thymesVar id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"-->
Status 2017.3
Support for Spring Boot autoconfigured MVC applications is complete, all bundled autoconfiguration view types are supported.
Fix versions: 2017.3
This is a problem with IntelliJ: IDEA-132738.
Basically IntelliJ is unable to locate the model variables when Spring Boot has been used to autoconfigure everything.
I had two different portions of code: the first was showing the error and the second was not doing it. I observed that there is a difference in the xmlns:th attribute.
First Page: Not working!
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
Second Page: Working!
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://thymeleaf.org">
I removed the www. and it works for me!
Manually enabling it, worked for me in Intellij 2018.1.6 Ultimate Edition. Steps followed
Open the Project tool window (e.g. View | Tool Windows | Project).
Right-click the project or the module folder and select Add Framework Support.
Official reference : https://www.jetbrains.com/help/idea/thymeleaf.html#0c8052be
In my case the problem was that I had the following in my application.properties:
spring.thymeleaf.prefix=file:src/main/resources/templates/
spring.thymeleaf.cache=false
Once I removed these properties, the spring mvc mappings are detected by Intellij again (in the Ultimate version, I'm using 2018.1). Also the thymeleaf objects are working now.
I used these properties to support fast development where a refresh would reload the thymeleaf template files.
To solve this issue, I use the following -D option in my run configuration of my spring boot application to tell spring boot where my property files are during development:
-Dspring.config.location=/dev/application/conf/application.properties
I want to add one more thing. As stated above, the issue has been fixed in IntelliJ 2017.3. I can also confirm this.
However, I noticed that this is only true if you define all your attributes directly inside the responsible controller function, like e.g. this:
@RequestMapping(value = "/userinput")
public String showUserForm(Model model){
model.addAttribute("method", "post");
model.addAttribute("user", new User());
return "userform";
}
If you are using a sub-function in which you define the model attributes (see Example below), IntelliJ can still not find the attributes in the HTML template.
Example:
@RequestMapping(value = "/userinput")
public String showUserForm(Model model){
return doIt(model);
}
private String doIt(Model model) {
model.addAttribute("method", "post");
model.addAttribute("user", new User());
return "userform";
}
So, always make sure you put your code directly inside the view function!