Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: How do I forward an entire model from one controller to another?

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

like image 544
sparkyspider Avatar asked Nov 19 '12 15:11

sparkyspider


2 Answers

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

like image 187
sparkyspider Avatar answered Oct 22 '22 02:10

sparkyspider


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

like image 27
Max Avatar answered Oct 22 '22 01:10

Max