我正在使用IntelliJ上的spring啓動和thymeleaf編寫一個簡短的web表單應用程序,但似乎在html文件中模型中的所有字段都無法解析。這裏是我的代碼:IntelliJ中的春季啓動+百里香:無法解決變數
控制器類:
@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>
總是有下紅線「帖子「,」標題「和」內容「,但我不知道如何解決它。這是IntelliJ的問題還是隻是我的代碼問題?
這應該在IntelliJ 2013.3中解決,請參閱我編輯的答案。 –