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";
    }
}
型号类:
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的问题还是我的代码问题?
Hon*_*dek 16
您可以使用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"-->
状态2017.3
完全支持Spring Boot自动配置的MVC应用程序,支持所有捆绑的自动配置视图类型.
修复版本:2017.3
小智 16
我有两个不同的代码部分:第一个显示错误,第二个未执行。我观察到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。它对我有用!
And*_*rew 15
这是IntelliJ的一个问题:IDEA-132738.
基本上,当使用Spring Boot自动配置所有内容时,IntelliJ无法找到模型变量.
我想再添加一件事。如上所述,此问题已在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";
}
因此,请始终确保将代码直接放在视图函数中!
手动启用它,在 Intellij 2018.1.6 Ultimate Edition 中为我工作。遵循的步骤
打开项目工具窗口(例如查看 | 工具窗口 | 项目)。
右键单击项目或模块文件夹并选择添加框架支持。
官方参考:https : //www.jetbrains.com/help/idea/thymeleaf.html#0c8052be
| 归档时间: | 
 | 
| 查看次数: | 21119 次 | 
| 最近记录: |