How to pass model attributes from one Spring MVC controller to another controller?

后端 未结 10 1290
暗喜
暗喜 2020-11-27 13:19

I am redirecting from a controller to another controller. But I also need to pass model attributes to the second controller.

I don\'t want to put the model in sessi

相关标签:
10条回答
  • 2020-11-27 13:44

    I think that the most elegant way to do it is to implement custom Flash Scope in Spring MVC.

    the main idea for the flash scope is to store data from one controller till next redirect in second controller

    Please refer to my answer on the custom scope question:

    Spring MVC custom scope bean

    The only thing that is missing in this code is the following xml configuration:

    <bean id="flashScopeInterceptor" class="com.vanilla.springMVC.scope.FlashScopeInterceptor" />
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
      <property name="interceptors">
        <list><ref bean="flashScopeInterceptor"/></list>
      </property>
    </bean>
    
    0 讨论(0)
  • 2020-11-27 13:46

    I used @ControllerAdvice , please check is available in Spring 3.X; I am using it in Spring 4.0.

    @ControllerAdvice 
    public class CommonController extends ControllerBase{
    @Autowired
    MyService myServiceInstance;
    
        @ModelAttribute("userList")
        public List<User> getUsersList()
        {
           //some code
           return ...
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:47

    I use spring 3.2.3 and here is how I solved similar problem.
    1) Added RedirectAttributes redirectAttributes to the method parameter list in controller 1.

    public String controlMapping1(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model, 
            final RedirectAttributes redirectAttributes)
    

    2) Inside the method added code to add flash attribute to redirectAttributes redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);

    3) Then, in the second contoller use method parameter annotated with @ModelAttribute to access redirect Attributes

    @ModelAttribute("mapping1Form") final Object mapping1FormObject
    

    Here is the sample code from Controller 1:

    @RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
    public String controlMapping1(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model, 
            final RedirectAttributes redirectAttributes) {
    
        redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
    
        return "redirect:mapping2";
    }   
    

    From Contoller 2:

    @RequestMapping(value = "/mapping2", method = RequestMethod.GET)
    public String controlMapping2(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model) {
    
        model.addAttribute("transformationForm", mapping1FormObject);
    
        return "new/view";  
    }
    
    0 讨论(0)
  • 2020-11-27 13:48

    If you want just pass all attributes to redirect...

    public String yourMethod( ...., HttpServletRequest request, RedirectAttributes redirectAttributes) {
        if(shouldIRedirect()) {
            redirectAttributes.addAllAttributes(request.getParameterMap());
            return "redirect:/newPage.html";
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:54

    You can resolve it by using org.springframework.web.servlet.mvc.support.RedirectAttributes.

    Here is my controller sample.

    @RequestMapping(method = RequestMethod.POST)
        public String eligibilityPost(
                @ModelAttribute("form") @Valid EligibiltyForm form,
                Model model,
                RedirectAttributes redirectAttributes) {
            if(eligibilityService.validateEligibility(form)){
                redirectAttributes.addFlashAttribute("form", form);
                return "redirect:<redirect to your page>";
            }
           return "eligibility";
        }
    

    read more on my blog at http://mayurshah.in/596/how-do-i-redirect-to-page-keeping-model-value

    0 讨论(0)
  • 2020-11-27 13:56

    Add all model attributes to the redirecting URL as query string.

    0 讨论(0)
提交回复
热议问题