2017-03-04 9 views
1

나는 어떻게하면 BindingResult 값을 "보존"하는지를 알기 위해 노력하고있다. 포스트가 리다이렉트를하고 싶지만 에러 값을 유지하고 리다이렉트하지 않고 JSP로 사용자에게 보여주기를 원하지만, redict 나는 오류 값이 내 코드를 잃게POST 후 리디렉션을 사용하여 봄에 유효성 검사 오류를 보존하는 방법은 무엇입니까?

@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) 

누군가가 나를 도울 수 메르 디부 AVANCE!

+0

가능한 중복에 attr.addAttribute를 교체해야합니다인지 확인 오류/questions/2543797/spring-redirect-after-post-even-with-validation-errors –

+0

아니요 중복 질문이 아닙니다! 나는이 링크에서 솔루션을 시도하지만 여전히 몇 가지 스택을 가지고있다. errorsexception은 내가 코드와 예외를 게시하는 이유이다. 어떤 도움을 얻기를 희망한다. –

+1

다른 질문의 문제는 꽤 비슷해 보였다. 그래서 나는 그것을 제안했다. 나는'RedirectAttributes'가 비슷한 예외 문제를 해결했다는 것을 기억합니다. 그러나 그것이 당신을 위해 작동하지 않는다면 아마도 누군가 적절한 답을 찾게 될 것입니다. 인사말. –

답변

1

컨트롤러가 2 대 있다고 가정합니다. 한 컨트롤러에서 다른 컨트롤러로 리디렉션하는 경우 모델 개체의 값을 다른 컨트롤러에서 사용할 수 없습니다. 그런 다음 모델 객체 값을 공유하고 싶다면 먼저 컨트롤러 말을

당신이 http://stackoverflow.com의 attr.addFlashAttribute

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