Search code examples
http-redirectspring-mvccontrollersmodelattribute

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


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 session.

Please help.


Solution

  • 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>