Java IntelliJ 中的 Spring Boot + thymeleaf:无法解析变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38710585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot + thymeleaf in IntelliJ: cannot resolve vars
提问by Shuhan Liu
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 code:
我正在 IntelliJ 上使用 spring boot 和 thymeleaf 编写一个简短的 Web 表单应用程序,但似乎在 html 文件中,模型中的所有字段都无法解析。这是我的代码:
Controller class:
控制器类:
@Controller
public class IndexController{
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(){
return "index";
}
@RequestMapping(value="/", method = RequestMethod.POST)
public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model){
if(bindingResult.hasErrors()){
return "index";
}
model.addAttribute("title",post.getTitle());
model.addAttribute("content",post.getContent());
return "hello";
}
}
Model Class:
型号分类:
public class Post {
@Size(min=4, max=35)
private String title;
@Size(min=30, max=1000)
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Then is the index.html:
然后是 index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Spring Framework Leo</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h3>Spring Boot and Thymeleaf</h3>
<form action="#" th:action="@{/}" th:object="${post}" method="post">
<table>
<tr>
<td>Title:</td>
<td><input type="text" th:field="*{title}" /></td>
<td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title error message</td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" th:field="*{content}" /></td>
<td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Content error message</td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</form>
There are always red lines under "post", "title" and "content", but I don't know how to solve it. Is it a problem of IntelliJ or just a problem of my code?
“帖子”、“标题”和“内容”下总是有红线,但我不知道如何解决。是 IntelliJ 的问题还是我的代码的问题?
采纳答案by Honza Zidek
- If your IntelliJ version is < 2017.3, it is, as Andrew wrote, a known error IDEA-132738. There is a workaround how to get rid of the error marks in the IDE. IntelliJ also supports the semi-automatic generation of the below mentioned code:
- 如果您的 IntelliJ 版本 < 2017.3,正如Andrew 所写的,这是一个已知错误IDEA-132738。有一种解决方法可以消除 IDE 中的错误标记。IntelliJ 还支持以下提到的代码的半自动生成:
You can use Alt+Entershortcut to invoke intention "Declare external variable in comment annotation" in order to get rid of "unresolved model attribute" in your views.
您可以使用Alt+Enter快捷方式调用意图“在注释注释中声明外部变量”,以摆脱视图中的“未解析的模型属性”。
Add the following code to your html
file:
将以下代码添加到您的html
文件中:
<!--/* 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:
如果使用 ThymeLeaf 自动构造的扩展对象,例如#temporals
fromthymeleaf-extras-java8time
用于java.time
对象转换:
<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:
而IntelliJ无法解析,使用类似的代码,#
在对象名前加上:
<!--@thymesVar id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"-->
- If your IntelliJ version is >= 2017.3(however some people complain that it still does not work for them), the issue IDEA-132738should be fixed (@FloatOverflow: "I confirm that in version 2017.3 build 25.Oct.2017 the problem has been solved"):
- 如果您的 IntelliJ 版本 >= 2017.3(但是有些人抱怨它仍然对他们不起作用),则IDEA-132738问题应该得到修复(@FloatOverflow:“我确认在 2017.3 版本 2017 年 10 月 25 日构建的问题已解决”):
Status 2017.3
Support for Spring Boot autoconfigured MVC applications is complete, all bundled autoconfiguration view types are supported.
Fix versions: 2017.3
状态 2017.3
对 Spring Boot 自动配置 MVC 应用的支持是完整的,支持所有捆绑的自动配置视图类型。
修复版本:2017.3
回答by Andrew
This is a problem with IntelliJ: IDEA-132738.
这是 IntelliJ 的一个问题:IDEA-132738。
Basically IntelliJ is unable to locate the model variables when Spring Boot has been used to autoconfigure everything.
基本上,当使用 Spring Boot 自动配置所有内容时,IntelliJ 无法定位模型变量。
回答by user7346048
I want to add one more thing. As stated above, the issue has been fixed in IntelliJ 2017.3. I can also confirm this.
我想再补充一件事。如上所述,该问题已在IntelliJ 2017.3 中修复。我也可以确认这一点。
However, I noticed that this is only true if you define all your attributes directlyinside 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 Examplebelow), IntelliJ can still not find the attributes in the HTML template.
如果您正在使用定义模型属性的子函数(请参见下面的示例),IntelliJ 仍然无法在 HTML 模板中找到这些属性。
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!
因此,始终确保将代码直接放在视图函数中!
回答by zoukun
This feature is supported in the Ultimate edition only.
此功能仅在 Ultimate 版中受支持。
Click herefor more details
点击这里了解更多详情
回答by Ekkelenkamp
In my case the problem was that I had the following in my application.properties:
就我而言,问题是我的 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.
一旦我删除了这些属性,Intellij 就会再次检测到 spring mvc 映射(在 Ultimate 版本中,我使用的是 2018.1)。此外百里香对象现在正在工作。
I used these properties to support fast development where a refresh would reload the thymeleaf template files.
我使用这些属性来支持快速开发,其中刷新将重新加载 thymeleaf 模板文件。
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:
为了解决这个问题,我在我的 spring boot 应用程序的运行配置中使用以下 -D 选项来告诉 spring boot 我的属性文件在开发过程中的位置:
-Dspring.config.location=/dev/application/conf/application.properties
回答by Georgi Peev
There is one more interesting situation ,if you DONT return directly the object from your controller ,Intellij will not recognize the variable ,but she will still work
还有一种更有趣的情况,如果您不直接从控制器返回对象,Intellij 将无法识别该变量,但她仍然可以工作
回答by xyz
Manually enabling it, worked for me in Intellij 2018.1.6 Ultimate Edition. Steps followed
手动启用它,在 Intellij 2018.1.6 Ultimate Edition 中为我工作。遵循的步骤
Open the Project tool window (e.g. View | Tool Windows | Project).
Right-click the project or the module folder and select Add Framework Support.
- In the left-hand pane of the Add Frameworks Support dialog that opens, select the Thymeleaf checkbox.
打开项目工具窗口(例如查看 | 工具窗口 | 项目)。
右键单击项目或模块文件夹并选择添加框架支持。
- 在打开的添加框架支持对话框的左侧窗格中,选中 Thymeleaf 复选框。
Official reference : https://www.jetbrains.com/help/idea/thymeleaf.html#0c8052be
官方参考:https: //www.jetbrains.com/help/idea/thymeleaf.html#0c8052be
回答by Mustapha EL KOJJI
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:thattribute.
我有两个不同的代码部分:第一个显示错误,第二个没有执行。我观察到xmlns:th属性存在差异。
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!
我删除了www。它对我有用!