Spring boot + thymeleaf in IntelliJ: 無法解析 vars更新原始答案先决条件IntelliJ 版本 > = 2017.3IntelliJ 版本 < 2017.3
我正在 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;
}
}
然后是索引.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的问题还是我的代码的问题?