Question

How do I pass an entire model from one controller to another without using a redirect?

Was it helpful?

Solution

I struggled with this for quite a while, so decided to answer my own question...

It's quite possible with the forward method. Unlike the chain method, the documentation doesn't mention the model attribute, but [in grails 2.1.1 at least] it is actually supported.

In Controller One:

    def model = [
        firstname:           params.firstname, 
        lastname:            params.lastname
    ]

    forward(controller:"controllerName",action:"index", model:model)

In Controller Two:

    render (view: "/page.gsp") 

In page.gsp

    Welcome ${firstname} ${lastname},
    ...

Simple as that...

OTHER TIPS

If you want to pass the whole object (which contains their own methods), refer to my answer on Best way to pass objects between controller actions in grails

The key is 1) using "chain action" in source action, 2) using chainModel in target action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top