当前位置: 首页 > 知识库问答 >
问题:

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

麻超
2023-03-14
@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;
    }
}
<!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的问题,还是仅仅是我的代码的问题?

共有1个答案

景星光
2023-03-14
  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"-->
<!--*/-->

如果您使用由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"-->

2017.3状况

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

 类似资料:
  • 本文向大家介绍SpringBoot thymeleaf的使用方法解析,包括了SpringBoot thymeleaf的使用方法解析的使用技巧和注意事项,需要的朋友参考一下 1.pom.xml添加相应依赖 2.application.properties 3.common.xml文件,注意文件路径 4.添加TemplateController.java 5.添加app.java 6.访问路径,完成

  • 我想在HTML中显示一个请求的对象,我得到了一个错误,我不知道是什么原因导致了我的错误。谷歌帮不上忙,现在我试着问你。我认为错误不是来自我的表,因为我把它注释掉了,错误仍然是Occour。该错误也不是来自“http://localhost:8081/simulation”,因为我使用有效值重新接收了一个有效的JSON。谢谢你的帮助:)。 下面是我的代码: 我通过调用“http://localhos

  • 本文向大家介绍SpringBoot中的Thymeleaf用法,包括了SpringBoot中的Thymeleaf用法的使用技巧和注意事项,需要的朋友参考一下 Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a m

  • 我的问题是我未能显示胸腺叶模板。我怀疑配置错误,但我似乎找不到它。提前感谢:) 波姆。xml: 模板在: Spring配置: 控制器: 我正在尝试访问:并获得404。 另一件事,有人能解释(或给文档一个链接)哪个servlet被用来“返回”模板html吗?是Spring调度员servlet吗? 好的,所以我忘记了,谢谢@Benjamin c.然而,添加它会产生:

  • 正如您所看到的,所有其他selenium库都可以毫无问题地导入,但是支持库根本不想工作。我该怎么办?

  • 我想知道如何在spring表单mvc平台中传输参数。首先,下面的代码是spring格式的java文件。 下一个文件是有界编辑。html文件 表单的输入链接url如下所示, 但是spring mvc控制器代码中抛出了异常。 例外的是 我不知道如何在Spring-Thymeleaf表单模板中传递参数。