为什么javax验证在DTO类中不起作用



我正在努力学习弹簧靴。而且,我被困在表格验证过程中。我按照这里的指示进行了操作。

这是我的控制器类

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TalentCategoryController {
@GetMapping("talent-category")
public ModelAndView create(CreateTalentCategoryRequest talentCategory) {
ModelAndView model = new ModelAndView();
model.setViewName("talent-category/create");
model.addObject("talentCategory", talentCategory);
return model ; 
}
@Autowired
TalentCategoryService talentCategoryService ; 

@RequestMapping(path="talent-category", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ModelAndView store(@Valid @ModelAttribute CreateTalentCategoryRequest talentCategory, BindingResult result) {
// result.hasErrors is false
if(result.hasErrors()) { 
System.out.println("Validation working");
ModelAndView model = new ModelAndView();            
model.setViewName("talent-category/create");
return model; 
}
System.out.println("Validation not working");
talentCategoryService.store();
return null ; 
}

}

DTO等级:

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Data;
@Data
public class CreateTalentCategoryRequest {

@NotBlank(message="Cannot be empty")
@Size(min=10, max=30)
private String name ; 

@NotBlank(message="Cannot be empty")
private String status  ; 
@NotBlank(message="Cannot be empty")
private String approved ; 

@NotBlank(message="Cannot be empty")
private String sort_order ;
}

视图:

<form th:object="${talentCategory}" name="create-talent-category" method="POST" th:action="@{/talent-category}">
<div class="row">
<div class="col-4">
<div class="form-group">
<label for="name">Name</label>
<input th:field="*{name}" type="text"  class="form-control form-control-sm" id="name" placeholder="Category Name" />
<p class="alert alert-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></p>

</div>
</div>

<div class="col-2">
<div class="form-group">
<label for="sort_order">Sort Order</label>
<input type="text" class="form-control form-control-sm" id="sort_order" placeholder="Eg : 1" />
</div>
</div>


<div class="col-2">
<div class="form-group">
<label for="status">Status</label>
<select name="status" id="status" class="form-control form-control-sm">
<option selected>Choose...</option>
<option value="1">Active</option>
<option value="0">Passive</option>
</select>
</div>
</div>

<div class="col-2">
<div class="form-group">
<label for="approved">Approved</label>
<select name="approved" id="approved" class="form-control form-control-sm">
<option selected>Choose...</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>

<div class="row">
<div class="col-12">
<button name="create" class="btn btn-sm btn-primary">Create</button>
</div>
</div>
</form>

当提交的表单中所有字段都为空时,请求不会重定向到该表单(打印控制台中不起作用的验证(。

如果您使用的是spring版本2.3+,请确保您有以下依赖项

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

最新更新