Java 绑定结果是spring boot无法工作
我正在使用spring引导和验证。当名称的值不存在时,将显示白标签错误页面。我想通过自定义错误将其传递到索引页,例如名称丢失 控制器类为:Java 绑定结果是spring boot无法工作,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我正在使用spring引导和验证。当名称的值不存在时,将显示白标签错误页面。我想通过自定义错误将其传递到索引页,例如名称丢失 控制器类为: package com.springs.springs.com.springs.springs.controller; import com.springs.springs.hibernate.Employee; import com.springs.springs.hibernate.EmployeeServiceImpl; import org
package com.springs.springs.com.springs.springs.controller;
import com.springs.springs.hibernate.Employee;
import com.springs.springs.hibernate.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import javax.validation.Valid;
import java.util.List;
@Controller
@Validated
public class URLController {
@Autowired
EmployeeServiceImpl empService;
@GetMapping({"/", "/index"})
public String index1(Model model){
model.addAttribute("employee",new Employee());
return "index";
}
@PostMapping("/result")
public String result( @ModelAttribute @Valid Employee employee, BindingResult bindingResult){
List<FieldError> errors = bindingResult.getFieldErrors();
for (FieldError error : errors ) {
System.out.println (error.getObjectName() + " - " +error.getDefaultMessage());
}
System.out.print(employee.getName()== null); //use a logger instead
if(bindingResult.hasErrors()){
return "index";
}
else {
empService.save(employee);
return "result"; //may want to return a different page name for clarity
}
}
}
包com.springs.springs.com.springs.springs.controller;
导入com.springs.springs.hibernate.Employee;
导入com.springs.springs.hibernate.EmployeeServiceImpl;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.validation.BindingResult;
导入org.springframework.validation.FieldError;
导入org.springframework.validation.annotation.Validated;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.PostMapping;
导入javax.validation.Valid;
导入java.util.List;
@控制器
@验证
公共类URL控制器{
@自动连线
雇员服务雇员服务;
@GetMapping({”/“,“/index”})
公共字符串索引1(模型){
model.addAttribute(“employee”,new employee());
返回“索引”;
}
@后映射(“/result”)
公共字符串结果(@modeldattribute@Valid Employee,BindingResult BindingResult){
List errors=bindingResult.getFieldErrors();
for(FieldError错误:错误){
System.out.println(error.getObjectName()+“-”+error.getDefaultMessage());
}
System.out.print(employee.getName()==null);//改用记录器
if(bindingResult.hasErrors()){
返回“索引”;
}
否则{
empService.save(雇员);
return“result”;//为了清晰起见,可能需要返回不同的页面名称
}
}
}
要将错误发送到视图,只需将重定向属性作为参数添加到方法中,并使用将错误发送到视图中即可
这样:
public String result( @ModelAttribute @Valid Employee employee, BindingResult bindingResult, RedirectAttributes redirectAttributes){
以及:
在视图上(可能是JSP),您应该导入de-spring标记库并使用:
<spring:hasBindErrors name="employee">
<form:errors path="*" />
</spring:hasBindErrors>
这将打印与对象相关的所有错误消息
观察:使用导入弹簧标记
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
您如何验证员工类?我使用@notempty注释进行验证@notempty
没有考虑null
介绍。您需要额外的@NotNull
。同时从类中删除@Validated
,您不需要它。我得到了错误javax.validation.ConstraintViolationException:result.employee.name:不能为空。它工作正常,但我想在html中添加自定义错误。如何才能做到这一点
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>