如何在 POST 之后通过重定向保留 spring 中的验证错误?

How to preserve validation errors in spring with redirect after a POST?

我正在尝试弄清楚如何 "preserve" Post 之后的 BindingResult 值 我想进行重定向但保留错误值,并在 [=] 中向用户显示21=] 没有重定向它工作得很好,但是当我做 redict 时我失去了我的代码的错误值

@RequestMapping(value="/ajouter",method=RequestMethod.POST)
    public String ajouterEnqueteur(HttpServletRequest request,Model model,
            @ModelAttribute("enqueteur")@Valid Enqueteur enqueteur ,BindingResult binding,RedirectAttributes attr,HttpSession session)
    {
        if (binding.hasErrors())
        {
            logger.debug(binding.toString());
            attr.addAttribute("enqueteur",enqueteur);
            attr.addAttribute("org.springframework.validation.BindingResult.enqueteur",binding);

        }
        else{
            enqueteurService.save(enqueteur);
            logger.debug("Enqueteur enregistré. adesse IP {}"+request.getRemoteAddr());
        }
        return "redirect:/enqueteurs/init.do";

    }

当我提交表单时收到此错误消息:

message Failed to convert value of type 'com.glsid.gnrqst.entities.Enqueteur' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.glsid.gnrqst.entities.Enqueteur] to required type [java.lang.String]: no matching editors or conversion strategy found

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

exception

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'com.glsid.gnrqst.entities.Enqueteur' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.glsid.gnrqst.entities.Enqueteur] to required type [java.lang.String]: no matching editors or conversion strategy found
    org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
    org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:40)
    org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:657)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.formatValue(RedirectAttributesModelMap.java:79)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:71)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:34)
    com.glsid.gnrqst.controllers.EnqueteurController.ajouterEnqueteur(EnqueteurController.java:46)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère

java.lang.IllegalStateException: Cannot convert value of type [com.glsid.gnrqst.entities.Enqueteur] to required type [java.lang.String]: no matching editors or conversion strategy found
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:108)
    org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
    org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:40)
    org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:657)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.formatValue(RedirectAttributesModelMap.java:79)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:71)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:34)
    com.glsid.gnrqst.controllers.EnqueteurController.ajouterEnqueteur(EnqueteurController.java:46)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

有人可以帮我 Merci d'avance !

假设您有 2 个 controllers.If 您从一个控制器重定向到另一个控制器,模型对象中的值在另一个控制器中将不可用。因此,如果您想共享模型对象值,那么您必须在第一个控制器中说

您犯的错误是您应该将 attr.addAttribute 替换为 attr.addFlashAttribute

attr.addFlashAttribute("org.springframework.validation.BindingResult.enqueteur", binding);
 attr.addFlashAttribute("enqueteur", enqueteur);