IntelliJ 中的 Spring boot 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";
    }
}

模型类:

 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>

“post”、“title”、“content”下面老是有红线,不知道怎么解决。是 IntelliJ 的问题还是我的代码的问题?

原文由 Shuhan Liu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

先决条件

  • Thymeleaf 插件已安装

  • The html element has an XML namespace declaration for th that is set to http://www.thymeleaf.org (with www. ), for example:

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

IntelliJ 版本 >= 2017.3

检测应该自动进行。然而,有些人抱怨它仍然对他们不起作用),问题 IDEA-132738 应该被修复(@FloatOverflow:“我确认在版本 2017.3 build 25.Oct.2017 中问题已经解决”):

现状 2017.3

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

修复版本:2017.3

如果未自动检测到变量,您仍然可以添加 @thymesVar 注释,如下所示。

IntelliJ 版本 < 2017.3

正如 安德鲁所写,这是一个已知错误 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 自动构造的扩展对象,例如 #temporals 来自 thymeleaf-extras-java8time 用于转换 java.time 对象:

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

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

 <!--@thymesVar id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"-->

原文由 Honza Zidek 发布,翻译遵循 CC BY-SA 4.0 许可协议

更新

TL;DR: 跳到下面接受的答案: https ://stackoverflow.com/a/44804086/474034

正如多人评论中提到的,这个解决方案是不正确的。 Thymeleaf 文档(请参阅 http://thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html )在所有示例中都有一个包含 www. 的版本:

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

另请参阅 Github 上的 Standard- Dialect.xml,它声明 namespace-uri 为:

 namespace-uri="http://www.thymeleaf.org"

原始答案

我有两个不同的代码部分:第一个显示错误,第二个不执行。我观察到 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。 它对我有用!

原文由 Mustapha El Kojji 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题