IntelliJ 中的 Spring Boot + thymeleaf:无法解析变量 [英] Spring boot + thymeleaf in IntelliJ: cannot resolve vars

查看:51
本文介绍了IntelliJ 中的 Spring Boot + thymeleaf:无法解析变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

控制器类:

@Controller公共类索引控制器{@RequestMapping(value = "/", method = RequestMethod.GET)公共字符串索引(){返回索引";}@RequestMapping(value="/", method = RequestMethod.POST)public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model){如果(bindingResult.hasErrors()){返回索引";}model.addAttribute("title",post.getTitle());model.addAttribute("content",post.getContent());返回你好";}}

模型类:

public class Post {@Size(min=4, max=35)私人字符串标题;@Size(min=30, max=1000)私有字符串内容;公共字符串 getTitle() {返回标题;}公共无效设置标题(字符串标题){this.title = 标题;}公共字符串 getContent() {返回内容;}公共无效集内容(字符串内容){this.content = 内容;}}

然后是index.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"/><身体><h3>Spring Boot 和 Thymeleaf</h3><form action="#" th:action="@{/}" th:object="${post}" method="post"><表格><tr><td>标题:</td><td><input type="text" th:field="*{title}"/></td><td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">标题错误信息</td></tr><tr><td>内容:</td><td><input type="text" th:field="*{content}"/></td><td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">内容错误信息</td></tr><tr><td><button type="submit">提交帖子</button></td></tr></表单>

post"、title"、content"下面总是有红线,但是不知道怎么解决.是 IntelliJ 的问题还是我的代码的问题?

解决方案

  1. 如果您的 IntelliJ 版本是 <2017.3,正如安德鲁写道,已知错误IDEA-132738.有一种解决方法可以消除 IDE 中的错误标记.IntelliJ 还支持以下提到的代码的半自动生成:

<块引用>

您可以使用Alt+Enter快捷键来调用意图在注释注释中声明外部变量";为了摆脱未解析的模型属性";在您看来.

将以下代码添加到您的 html 文件中:

<!--/* 解决错误 https://youtrack.jetbrains.com/issue/IDEA-132738 --><!--@thymesVar id="post";type=your.package.Post"--><!--@thymesVar id="title";类型=字符串"--><!--@thymesVar id="content";类型=字符串"--><!--*/-->

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

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

  1. 如果您的 IntelliJ 版本是 >= 2017.3(但是有些人抱怨它仍然对他们不起作用),则问题 IDEA-132738 应该已修复(@FloatOverflow:我确认在 2017.3 版本 25.Oct.2017 中问题已解决"):

<块引用>

状态 2017.3

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

修复版本:2017.3

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:

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:

<!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?

解决方案

  1. 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:

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"-->

  1. If your IntelliJ version is >= 2017.3 (however some people complain that it still does not work for them), the issue IDEA-132738 should be fixed (@FloatOverflow: "I confirm that in version 2017.3 build 25.Oct.2017 the problem has been solved"):

Status 2017.3

Support for Spring Boot autoconfigured MVC applications is complete, all bundled autoconfiguration view types are supported.

Fix versions: 2017.3

这篇关于IntelliJ 中的 Spring Boot + thymeleaf:无法解析变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆