IntelliJ中的Spring boot + thymeleaf:无法解析变量

Shu*_*Liu 23 java spring intellij-idea web thymeleaf

我正在IntelliJ上使用spring boot和thymeleaf编写一个简短的Web表单应用程序,但似乎在html文件中,模型中的所有字段都无法解析.这是我的代码:

控制器类:

@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";
    }
}
Run Code Online (Sandbox Code Playgroud)

型号类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是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>
Run Code Online (Sandbox Code Playgroud)

"post","title"和"content"下总是有红线,但我不知道如何解决它.这是IntelliJ的问题还是我的代码问题?

Hon*_*dek 16

  1. 如果你的IntelliJ版本是<2017.3,那就像Andrew写的那样,是一个已知错误IDEA-132738.有一种解决方法如何摆脱IDE中的错误标记.IntelliJ还支持下面提到的代码的半自动生成:

您可以使用Alt+ Enter快捷方式调用意图"在注释注释中声明外部变量",以便在视图中删除"未解析的模型属性".

将以下代码添加到您的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"-->
<!--*/-->
Run Code Online (Sandbox Code Playgroud)

如果您使用由ThymeLeaf自动构建的扩展对象,例如#temporals来自对象的thymeleaf-extras-java8time转换java.time:

<span th:text="${#temporals.format(person.birthDate,'yyyy-MM-dd')}"></span>
Run Code Online (Sandbox Code Playgroud)

并且IntelliJ无法解析它们,使用类似的代码,只需#在对象名称前面添加:

<!--@thymesVar id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"-->
Run Code Online (Sandbox Code Playgroud)
  1. 如果你的IntelliJ版本是> = 2017.3(但有些人抱怨它仍然不适合他们),问题应该修复(@FloatOverflow:"我确认在版本2017.3 build 25.Oct.2017问题已经解决"):

状态2017.3

完全支持Spring Boot自动配置的MVC应用程序,支持所有捆绑的自动配置视图类型.

修复版本:2017.3

  • 2019.3.2该bug依然存在。 (3认同)
  • @FloatOverflow我正在运行2017.3并且我的对象仍然带有下划线.知道我可能做错了什么吗?例如,在下面的代码中,`test`和`name`都加下划线.`<h1 th:text ="'感谢您帮我们测试,'+ $ {test.name} +'!'"> </ h1>` (2认同)

小智 16

我有两个不同的代码部分:第一个显示错误,第二个未执行。我观察到xmlns:th属性有所不同。

第一页:不起作用!

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
Run Code Online (Sandbox Code Playgroud)

第二页:工作!

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://thymeleaf.org">
Run Code Online (Sandbox Code Playgroud)

我删除了www。它对我有用!

  • 一个令人难以置信的解决方案 - 但它有效。我正在使用 IntelliJ 2019.1.3 UE。 (4认同)
  • 正如 @RiZKiT 所说,这样做你只是破坏了你的命名空间定义......如果你放在那里,结果会是相同的: xmlns:th="http://mybrokenuri.org"&gt; (3认同)
  • 这对我有用,我正在使用IntelliJ 2018+ (2认同)
  • 这消除了变量的下划线,因为名称空间的第th个变得完全无效。通过上述“解决方案”,所有的第th个属性现在都应具有错误:此处不允许使用属性th:****。 (2认同)
  • 这看起来很可疑。它违反了 Thymeleaf 文档(https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html),他们在所有示例中都有 `&lt;html xmlns:th="http://www.thymeleaf .org"&gt;`. 您也可以在这里看到它:https://github.com/thymeleaf/thymeleaf/blob/3.0-master/src/main/resources/org/thymeleaf/xml/thymeleaf/Standard-Dialect.xml:`namespace-uri= “http://www.thymeleaf.org”` (2认同)
  • 我同意@RiZKiT 的观点。这个解决方案是不正确的! (2认同)

And*_*rew 15

这是IntelliJ的一个问题:IDEA-132738.

基本上,当使用Spring Boot自动配置所有内容时,IntelliJ无法找到模型变量.

  • 我正在使用IntelliJ 2017.2,并且IDE中仍然存在此问题。当我访问我的html文件时,这是如此占优势。为什么JetBrains的人还没有为Thymeleaf的支持解决此问题? (2认同)
  • @HaimLvov 我认为他们会忽略您在 IDEA-132738 下的评论。请参阅 Yann Cebron 在 2017 年 11 月 13 日 10:08 发表的评论:请**停止向此问题添加评论**。如果您遇到问题,请**提交新问题并附上最小示例项目**。请注意,静态 Web 资源目录目前仍需要在 Web Facet 中手动设置一次,请参阅 IDEA-121038。 (2认同)

use*_*048 7

我想再添加一件事。如上所述,此问题已在IntelliJ 2017.3中修复。我也可以确认这一点。

但是,我注意到只有在您直接在负责任的控制器函数内定义所有属性时,这才是正确的,例如:

@RequestMapping(value = "/userinput")
public String showUserForm(Model model){
    model.addAttribute("method", "post");
    model.addAttribute("user", new User());
    return "userform";
}
Run Code Online (Sandbox Code Playgroud)

如果使用的子功能在其中定义模型属性(请参见下面的示例),则IntelliJ仍无法在HTML模板中找到属性。

范例

@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";
}
Run Code Online (Sandbox Code Playgroud)

因此,请始终确保将代码直接放在视图函数中!


xyz*_*xyz 5

手动启用它,在 Intellij 2018.1.6 Ultimate Edition 中为我工作。遵循的步骤

  1. 打开项目工具窗口(例如查看 | 工具窗口 | 项目)。

  2. 右键单击项目或模块文件夹并选择添加框架支持。

  3. 在打开的添加框架支持对话框的左侧窗格中,选中 Thymeleaf 复选框。

官方参考:https : //www.jetbrains.com/help/idea/thymeleaf.html#0c8052be

  • 这最终对我有用。需要注意的一件事是,一旦添加了框架支持,我必须执行 FIle-&gt;Invalidate Caches and Restart。 (2认同)