为什么javax验证在DTO类中不起作用?
我在学弹簧靴。而且,我被表单验证过程卡住了。我按照指示遵循了流程 这是我的控制器类为什么javax验证在DTO类中不起作用?,java,spring,spring-boot,validation,Java,Spring,Spring Boot,Validation,我在学弹簧靴。而且,我被表单验证过程卡住了。我按照指示遵循了流程 这是我的控制器类 import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springfra
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+版本,请确保您具有以下依赖项
org.springframework.boot
弹簧启动启动器验证
请添加一个验证无效的示例。谢谢。你救了我一天。