1

I'm trying to figure out how to "preserve" the BindingResult value after a Post I want to do a redirect but keeping the errors value ,and show them to the user in jsp without redirection it's work well, but when I do redict I lose the error value this my code

@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";

    }

When I submit the form I get this error message :

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)

Can someone help me Merci d'avance !

e2rabi
  • 4,728
  • 9
  • 42
  • 69
  • Possible duplicate of http://stackoverflow.com/questions/2543797/spring-redirect-after-post-even-with-validation-errors – Jakub Ch. Mar 04 '17 at 23:10
  • NO it's Not a duplicate question ! I try the solution in this link but still have the some stack errorsexception that's why I post my code and exception that I get hope getting some help – e2rabi Mar 05 '17 at 00:25
  • 1
    The problem in another question looked pretty similar, that's why I suggested it. I remember that `RedirectAttributes` resolved similar exception problem to me. But if it didn't work for you maybe someone will come up with the proper answer. Greetings. – Jakub Ch. Mar 05 '17 at 12:12
  • 1
    why do you want to redirect? – rmalchow Mar 06 '17 at 14:38
  • After a post to the form action the url that i'm posting to it still in the url of navigator that's why I want to redirect I don't want this behavior just keep the some url on navigator before post – e2rabi Mar 06 '17 at 14:45

1 Answers1

1

Assume you have 2 controllers.If you redirect from one controller to another controller the values in model object won't be available in the other controller. So if you want to share the model object values then you have to say in first controller

the error that you make is you should replace attr.addAttribute to attr.addFlashAttribute

attr.addFlashAttribute("org.springframework.validation.BindingResult.enqueteur", binding);
 attr.addFlashAttribute("enqueteur", enqueteur);
j.Stark
  • 191
  • 2
  • 8