英文:
Why javax validation not working in DTO class?
问题
以下是翻译好的内容:
我正在尝试学习 Spring Boot。但是,我在表单验证过程中遇到了问题。我已经按照这里的说明进行了操作[1]。
这是我的控制器类:
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="不能为空")
@Size(min=10, max=30)
private String name ;
@NotBlank(message="不能为空")
private String status ;
@NotBlank(message="不能为空")
private String approved ;
@NotBlank(message="不能为空")
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">名称</label>
<input th:field="*{name}" type="text" class="form-control form-control-sm" id="name" placeholder="类别名称" />
<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">排序顺序</label>
<input type="text" class="form-control form-control-sm" id="sort_order" placeholder="例如:1" />
</div>
</div>
<div class="col-2">
<div class="form-group">
<label for="status">状态</label>
<select name="status" id="status" class="form-control form-control-sm">
<option selected>选择...</option>
<option value="1">活动</option>
<option value="0">停用</option>
</select>
</div>
</div>
<div class="col-2">
<div class="form-group">
<label for="approved">已批准</label>
<select name="approved" id="approved" class="form-control form-control-sm">
<option selected>选择...</option>
<option value="1">是</option>
<option value="0">否</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<button name="create" class="btn btn-sm btn-primary">创建</button>
</div>
</div>
</form>
请注意,这是翻译后的代码部分,只包括代码内容,不包含额外的回答。如有其他问题,请随时提问。
英文:
I am trying to learn the spring boot. And, I am stuck on the form validation process. I have followed the process as instructed here.
Here is my controller class
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 class :
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 ;
}
View :
<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>
When a form is submitted with all the fields empty, the request is not redirect to the form(prints validation not working in console).
答案1
得分: 1
如果您使用的是Spring版本2.3+,请确保您具有以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
英文:
if you are using spring version 2.3+ , make sure you have following dependency
<!-- language: lang-xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论