【问题标题】:Spring boot + thymeleaf in IntelliJ: cannot resolve varsIntelliJ中的Spring boot + thymeleaf:无法解析变量
【发布时间】:2016-12-07 05:26:31
【问题描述】:

我正在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";
    }
}

模型类:

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;
    }
}

然后是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>

“帖子”、“标题”和“内容”下总是有红线,但我不知道如何解决。是 IntelliJ 的问题还是我的代码的问题?

【问题讨论】:

  • 这应该在 IntelliJ 2013.3 中修复,请参阅我编辑的答案。

标签: java spring web intellij-idea thymeleaf


【解决方案1】:

这发生在我的 Ultimate 2021.2.1 上。

在一种@GetMapping 方法中,我使用了@ModelAttribute('transactionForm')。该方法的 Thymeleaf 页面没有错误。

但是在另一个@GetMapping 方法中,我没有使用@ModelAttribute('newCustomerForm')。该方法的 Thymeleaf 页面有错误。当我将@ModelAttribute 添加到方法中时,错误就消失了。当我把它拿出来时,错误又回来了。

如果模型的名称与类名相同,我知道 Spring MVC 不需要 @ModelAttribute。所以我不应该这样做,我这样做只是为了安抚IDE。

【讨论】:

    【解决方案2】:

    对于那些来自 ReactJS 并希望使用 Springboot 作为后端的人。

    我忘记在 html 中添加 html 样板代码,这就是我看到此错误的原因。不要忘记添加样板代码。

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>HTML 5 Boilerplate</title>
        <link rel="stylesheet" href="style.css">
      </head>
      <body>
        <script src="index.js"></script>
      </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      这个问题来自thymeleaf web link。

      问题:xmlns:th="http://www.thymeleaf.org"

      解决办法:xmlns:th="http://thymeleaf.org"

      【讨论】:

        【解决方案4】:
        1. 如果您的 IntelliJ 版本是 ,则它是 Andrew wrote,已知错误 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"-->
        <!--*/-->
        

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

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

        而IntelliJ无法解析,使用类似的代码,只需在对象名前添加#即可:

        <!--@thymesVar id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"-->
        
        1. 如果您的 IntelliJ 版本 >= 2017.3(但是有些人抱怨它仍然对他们不起作用),问题 IDEA-132738 应该得到修复(@FloatOverflow:“我确认在 2017.3 版本中构建25.Oct.2017 问题已解决”):

        状态 2017.3

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

        修复版本:2017.3

        【讨论】:

        • 我确认在 2017.3 build 25.Oct.2017 版本中问题已经解决!
        • @FloatOverflow 我正在运行 2017.3 并且我的对象仍然带有下划线。知道我可能做错了什么吗?例如,在下面的代码中,testname 都带有下划线。 &lt;h1 th:text="'Thanks for helping us test, ' + ${test.name} + '!'"&gt;&lt;/h1&gt;
        • @justinraczak 我建议您创建一个最小、完整且可验证的示例 (stackoverflow.com/help/mcve) 并将其报告给 Jetbrains。您可以将其添加到上述问题中,也可以创建一个新问题。
        • 答案对 IntelliJ IDEA 2017.3.4 Ultimate 版本有帮助
        • 2019.3.2这个bug依然存在。
        【解决方案5】:

        我有两段不同的代码:第一段显示错误,第二段没有显示。 我观察到 xmlns:th 属性存在差异。

        首页:不工作!

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

        第二页:工作中!

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

        我删除了 www.,它对我有用!

        【讨论】:

        • 一个令人难以置信的解决方案 - 但它有效。我正在使用 IntelliJ 2019.1.3 UE。
        • 这会删除变量的下划线,因为th 命名空间完全无效。使用上述“解决方案”,所有th 属性现在应该有一个错误:Attribute th:**** is not allowed here.
        • 这看起来很可疑。这违反了 Thymeleaf 文档 (thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html),他们在所有示例中都有 &lt;html xmlns:th="http://www.thymeleaf.org"&gt;。你也可以在这里看到:github.com/thymeleaf/thymeleaf/blob/3.0-master/src/main/…:namespace-uri="http://www.thymeleaf.org"
        • 正如@RiZKiT 所说,这样做你只是破坏了你的命名空间定义......如果你放在那里,同样的结果:xmlns:th="mybrokenuri.org">
        • 我同意@RiZKiT。此解决方案不正确!
        【解决方案6】:

        手动启用它,在 Intellij 2018.1.6 Ultimate Edition 中为我工作。后续步骤

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

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

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

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

        【讨论】:

        • 我在这里是因为“添加框架支持”对话框没有显示 thymeleaf,只有 groovy 和 maven
        • 这最终对我有用。需要注意的一点是,一旦我添加了框架支持,我必须执行 FIle->Invalidate Caches and Restart。
        【解决方案7】:

        还有一种有趣的情况,如果你不直接从控制器返回对象,Intellij 将无法识别变量,但她仍然可以工作

        【讨论】:

        • 这并没有提供问题的答案。一旦您有足够的声誉,您就可以对任何帖子发表评论;相反,提供不需要提问者澄清的答案。
        【解决方案8】:

        就我而言,问题在于我的 application.properties 中有以下内容:

        spring.thymeleaf.prefix=file:src/main/resources/templates/
        
        spring.thymeleaf.cache=false
        

        删除这些属性后,Intellij 会再次检测到 spring mvc 映射(在 Ultimate 版本中,我使用的是 2018.1)。 thymeleaf 对象现在也正在工作。

        我使用这些属性来支持快速开发,其中刷新会重新加载 thymeleaf 模板文件。

        为了解决这个问题,我在我的 spring boot 应用程序的运行配置中使用了以下 -D 选项来告诉 spring boot 在开发过程中我的属性文件在哪里:

        -Dspring.config.location=/dev/application/conf/application.properties
        

        【讨论】:

          【解决方案9】:

          此功能仅在 Ultimate 版本中受支持。

          Click here了解更多详情

          【讨论】:

          • 这并没有提供问题的完整答案,尽管它可能是相关的。当您获得足够的声誉后,您将能够将诸如此类的信息发布为问题或其他答案的 cmets,但您不应发布答案,除非他们直接解决了所提出的问题。
          • 这看起来像评论。这种粘贴答案的不正确方法请参阅如何写出好的答案stackoverflow.com/help/how-to-answer
          【解决方案10】:

          我想再补充一件事。如上所述,该问题已在 IntelliJ 2017.3 中得到修复。我也可以确认一下。

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

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

          如果您使用定义模型属性的子函数(参见下面的示例),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";
          }
          

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

          【讨论】:

          • 和/或如果您的 RequestMappings 在 Kotlin 中,IDE 不会像使用私有方法那样看到属性。
          • @Chris.Jenkins 你知道有没有办法解决这个问题?我刚遇到这个问题。
          • 我可以确认,从 IntelliJ 2019.3.2 Ultimate 开始,这种解决方法(使用 model.addAttribute())仍然是让 IntelliJ 识别表单对象所必需的。或者 Honza Zidek 建议的其他解决方法,使用 @thymesVar 注释注释。
          【解决方案11】:

          这是 IntelliJ 的问题:IDEA-132738

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

          【讨论】:

          • 我使用的是 IntelliJ 2017.2,这个问题在 IDE 中仍然存在。当我访问我的 html 文件时,这非常重要。为什么 JetBrains 的人还没有为 Thymeleaf 支持解决这个问题?
          • @MAC 这应该在 IntelliJ 2013.3 中修复,请参阅我编辑的答案。
          • @HonzaZidek 由于某种原因,我在 2019.2 中仍然拥有它......另外,我已经检查了他们的错误跟踪器网站,似乎仍然有用户对新版本有问题,例如2017 年等
          • @HaimLvov 我建议您创建一个最小、完整且可验证的示例 (stackoverflow.com/help/mcve) 并将其报告给 Jetbrains。您可以将其添加到问题youtrack.jetbrains.com/issue/IDEA-132738,或创建一个新问题。
          • @HaimLvov 我认为他们会忽略您在 IDEA-132738 下的评论。查看 Yann Cebron 在 2017 年 11 月 13 日 10:08 发表的评论:请停止向此问题添加 cmets。如果您遇到问题,请提交一个 NEW ISSUE 并附上最小示例项目。请注意,静态 Web 资源目录目前仍然需要在 Web Facet 中手动设置一次,请参阅 IDEA-121038。
          猜你喜欢
          • 2018-11-08
          • 2019-06-11
          • 1970-01-01
          • 2019-12-17
          • 2021-05-30
          • 2020-05-14
          • 2020-06-27
          • 1970-01-01
          • 2018-10-08
          相关资源
          最近更新 更多