Java Spring启动验证批注@Valid无效
下面给出的是我的主控制器,我从中调用submitWatchlistItemForm方法Java Spring启动验证批注@Valid无效,java,spring-boot,Java,Spring Boot,下面给出的是我的主控制器,我从中调用submitWatchlistItemForm方法 package com.openclassrooms.watchlist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.validation.Valid; import org.springframework.stereoty
package com.openclassrooms.watchlist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
@Controller
public class WatchlistController {
private List<WatchlistItem> watchlistItems = new ArrayList<WatchlistItem>();
private static int index = 1;
@PostMapping("/watchlistItemForm")
public ModelAndView submitWatchlistItemForm(@Valid WatchlistItem watchlistItem, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return new ModelAndView("watchlistItemForm");
}
WatchlistItem existingItem = findWatchlistItem(watchlistItem.getId());
下面是绑定到命令对象WatchlistItem的my表单
<form action="#" method="post" th:action="@{/watchlistItemForm}" th:object="${watchlistItem}" >
<h2 class = "mt-4">Submit an item</h2>
<hr/>
<!-- Form levels error messages here -->
<div class = "form-group row ">
<div class="col-sm-8">
<span class="text-danger" > </span>
</div>
</div>
<div class = "form-group row ">
<label for = "title" class = "col-sm-2 col-form-label">Title</label>
<div class = "col-sm-4">
<input th:field="*{title}" type = "text" class = "form-control" placeholder = "Mandatory">
</div>
<div class="col-sm-4">
<span class="text-danger" th:errors="*{title}"> </span>
</div>
</div>
<div class = "form-group row ">
<label for = "rating" class = "col-sm-2 col-form-label mr-0">Rating</label>
<div class = "col-sm-4">
<input th:field="*{rating}" type = "text" class = "form-control" placeholder = "5.0 < Number < 10.0">
</div>
<div class="col-sm-4">
<span class="text-danger"> </span>
</div>
</div>
<div class = "form-group row ">
<label for = "priority" class = "col-sm-2 col-form-label mr-0">Priority</label>
<div class = "col-sm-4">
<input th:field="*{priority}" type = "text" class = "form-control" placeholder = "Low|Medium|High">
</div>
<div class="col-sm-4">
<span class="error-message" th:errors="*{priority}"></span>
</div>
</div>
<div class = "form-group row">
<label for = "comments" class = "col-sm-2 col-form-label">Comments</label>
<div class = "col-sm-4">
<textarea th:field="*{comment}" class = "form-control" rows = "3" placeholder = "Max. 50 chars"></textarea>
</div>
<div class="col-sm-4">
<span class="text-danger" th:errors="*{comment}"> </span>
</div>
</div>
<div class = "form-group row">
<div class = "col-sm-10">
<input th:field="*{id}" type="hidden"></input>
<button type = "submit" class = "btn btn-primary">Submit</button>
</div>
</div>
</form>
提交项目
标题
评级
优先
评论
提交
。
@有效的注释从不检测错误
但是,验证似乎不起作用,并且没有抛出任何验证错误。控制器bindingResult.hasErrors()的第27行始终为false。我错过了什么?感谢您的帮助
从spring boot 2.3开始,您需要添加spring boot starter验证以使用@Valid 将此依赖项添加到pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
嘿,你应该读书。不要发布代码的图像。发布实际代码。谢谢@Shawrup。我将依赖项添加到pom.xml中,这是我的工作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
public ModelAndView submitWatchlistItemForm(@Valid @RequestBody WatchlistItem watchlistItem, BindingResult bindingResult) {
...
}