我已經找到了一些與此相關的線程,但大多數錯誤似乎來自不正確的命名,但我相信我通過使用@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的教程,我真的不知道自己做錯了什麼,因爲我的代碼幾乎和我看到的其他人一樣。 –
我覺得威爾史密斯在「我是傳奇」中。有人可以幫助嗎? –