Skip to content

Grails’ chainModel

A question came up on the Grails mailing list recently regarding how to pass model data from one action to another.

The solution is to make use of chain() and the chainModel map. As the doco says:

This dynamic property only exists in actions following the call to the chain method…

Here’s a trivial Grails controller class that illustrates How To Do It:

package bob

class TestController {
  def index = {
    chain(action:'doIt', model:[data:new Data(when:new Date(), what:"Hello, world")])
  }

  def doIt = {
    render chainModel.data.what
  }
}

class Data {
  Date when
  String what
}

I’ve used chain() several times before, but it’s been good to refresh my understanding of chainModel.

The doco is here and here.

Tags:

Java Enterprise Edition, JEE, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE