简体   繁体   English

绑定结果不工作弹簧靴

[英]Binding result is not working spring boot

i am using spring boot and validation.我正在使用 Spring Boot 和验证。 when the value of the name is not present the Whitelabel Error Page is shown.当名称的值不存在时,将显示白标签错误页面。 I want to pass it to the index page with the custom error like the name is missing.我想将它传递到带有自定义错误的索引页面,例如缺少名称。

controller class is:控制器类是:

    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
        }

    }
}

To send the errors to the view you just need to add a RedirectAttributes as a parameter to the method and use to send the errors to the view.要将错误发送到视图,您只需要添加一个 RedirectAttributes 作为方法的参数并用于将错误发送到视图。

This way:这边走:

public String result( @ModelAttribute @Valid Employee employee, BindingResult bindingResult, RedirectAttributes redirectAttributes){

And:和:

if(bindingResult.hasErrors()) {
  redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.employee", bindingResult);
        redirectAttributes.addFlashAttribute("employee", employee);
        return "index";
    }

On the view (maybe it is JSP), you should import de spring taglib and use:在视图上(可能是 JSP),您应该导入 de spring taglib 并使用:

<spring:hasBindErrors name="employee">
   <form:errors path="*"  />
</spring:hasBindErrors>

This will print all the error messages related with the object.这将打印与对象相关的所有错误消息。

Observation: import spring tags with观察:导入 spring 标签

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM