我已经找到了一些与此相关的线程,但大多数错误似乎来自不正确的命名,但我相信我通过使用@ModelAttribute正确地做到了这一点。除了消息显示之外,验证也被识别并且一切正常。Thymeleaf将不会呈现验证消息
这里是我的控制器:
@GetMapping("/search")
public String searchPage(Model model, @ModelAttribute("searchFormBacking") SearchParamModel search) {
if (!model.containsAttribute("searchFormBacking")) {
model.addAttribute("searchFormBacking", new SearchParamModel());
} else {
model.addAttribute("searchFormBacking", search);
}
return "search";
}
@PostMapping("/results")
@SuppressWarnings("unchecked")
public String resultSubmit(@ModelAttribute("searchFormBacking") @Valid SearchParamModel search, BindingResult result, final RedirectAttributes redirectAttributes) throws Exception{
if (result.hasErrors()) {
//flash errors bound to "searchFormBacking"
redirectAttributes.addFlashAttribute("searchFormBacking",search);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.searchFormBacking",result);
return "redirect:/search";
}
List<Object[]> queryList = GlobalMethods.baseQuery();
//input into model&view
List<CrimeModel> crimeList = GlobalMethods.analyzeQuery(search.getSearchAddress(),search.getSearchDistance(),search.getSearchTime(), queryList);
List<CrimeRank> rankedList = GlobalMethods.distinctAsList(GlobalMethods.rankedMap(GlobalMethods.distinctCountMap(crimeList)));
redirectAttributes.addFlashAttribute("searchFormBacking",search);
redirectAttributes.addFlashAttribute("crimeModel", crimeList);
redirectAttributes.addFlashAttribute("rankedModel", rankedList);
return "redirect:/results";
}
下面的形式:
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Crime Tracker | Search</title>
</head>
<body>
<form th:action="@{/results}" th:object="${searchFormBacking}" method="post">
<input type="text" th:field="*{searchAddress}" placeholder="Enter address."/>
<div class="error-message" th:if="${#fields.hasErrors('searchAddress')}" th:errors="*{searchAddress}"></div>
<br/>
<input type="text" th:field="*{searchDistance}" placeholder="Enter the distance to search."/>
<div class="error-message" th:if="${#fields.hasErrors('searchDistance')}" th:errors="*{searchDistance}"></div>
<br/>
<input type="text" th:field="*{searchTime}" placeholder="Time-span."/>
<div class="error-message" th:if="${#fields.hasErrors('searchTime')}" th:errors="*{searchTime}"></div>
<br/>
<input type="submit" value="Submit"/>
<br/>
<input type="reset" value="Reset"/>
</form>
</body>
</html>
最后形式,支持类:
public class SearchParamModel {
@NotNull
@Size(min = 6, max = 40)
private String searchAddress;
@NotNull
private String searchDistance;
@NotNull
private String searchTime;
public String getSearchAddress() {
return searchAddress;
}
public void setSearchAddress(String searchAddress) {
this.searchAddress = searchAddress;
}
public String getSearchDistance() {
return searchDistance;
}
public void setSearchDistance(String searchDistance) {
this.searchDistance = searchDistance;
}
public String getSearchTime() {
return searchTime;
}
public void setSearchTime(String searchTime) {
this.searchTime = searchTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SearchParamModel that = (SearchParamModel) o;
if (searchAddress != null ? !searchAddress.equals(that.searchAddress) : that.searchAddress != null)
return false;
if (searchDistance != null ? !searchDistance.equals(that.searchDistance) : that.searchDistance != null)
return false;
return searchTime != null ? searchTime.equals(that.searchTime) : that.searchTime == null;
}
@Override
public int hashCode() {
int result = searchAddress != null ? searchAddress.hashCode() : 0;
result = 31 * result + (searchDistance != null ? searchDistance.hashCode() : 0);
result = 31 * result + (searchTime != null ? searchTime.hashCode() : 0);
return result;
}
}
主要的错误,人们似乎有的是,当他们不使用@ModelAttribute时,默认的名字变成了searchParamModel。此外,我已经处理/ search获取映射的重定向,只创建一个新的SearchParamModel,如果还没有的话。这些似乎是失去验证信息的两个最常见的原因,所以我想知道我做错了什么。
任何建议表示赞赏。我看过Spring和thymeleaf的教程,我真的不知道自己做错了什么,因为我的代码几乎和我看到的其他人一样。 –
我觉得威尔史密斯在“我是传奇”中。有人可以帮助吗? –