0

I'm learning Grails so forgive me if I'm missing something basic.

I'm trying to create a wizard/web flow using the Grails Web Flow plugin. I'd like for the first step of the flow to render some variables. From what I've read about normal controllers, typically this would be done by passing those variables from the controller to the view using a map. In the WebFlow model though, I don't know where to initialize these variables and how to pass them to the first step. I tried creating an initialize "action" and putting the variable into the flash scope, knowing that it should pass through one redirect, but it doesn't render on the gsp.

How is this done?

Here's a snip of the controller, which prints "4" in the console:

class ServicesController {

def index() {
    redirect(action: "initialize")
}

def initialize() {
    flash.assessmentTypes = AssessmentType.list()
    println flash.assessmentTypes.size
    redirect(action: "request")
}

def requestFlow = {
    selectAssessments {
        on("next") {
            // capture assessments
        }.to("productInfo")
        on("cancel").to("finish")
    }

...

And a snip of the gsp, which throws a nullpointer when rendering the size:

${flash.assessmentTypes.size}
<g:each var="assessmentType" in="${flash.assessmentTypes}">
  <li><g:checkbox name="assessmentType" value="${assessmentType.id}" />${assessmentType.name}</li>
</g:each>
jlpp
  • 1,564
  • 5
  • 23
  • 36
  • Similar question asked here: http://grails.1312388.n4.nabble.com/web-flow-view-state-td1380749.html – jlpp Mar 16 '12 at 00:50
  • Found another question that's essentially the same: http://stackoverflow.com/questions/1002170/grails-web-flow. This question can be closed. – jlpp Mar 16 '12 at 01:19
  • Wow, another one: http://stackoverflow.com/questions/7158042/grails-web-flow-first-state – jlpp Mar 16 '12 at 01:24

1 Answers1

2

No problem...

Use a flow initializer to act as the first step in the flow and then move it to the first step on success of the initFlow.

def wizardFlow = {

    initFlow {

      flow.assessmentTypes = AssessmentType.list();  //<-- put them in the flow so you can access it in step1.gsp

    }
    on('success').to('step1') 
    on(Exception).to('handleFlowError')

    step1{
        on('next'){ 
            flow.asessmentType = AssessmentType.get(params.assessmentType.id);
            println("They picked ${flow.asessmentType}.");
        }.to('step2')
        on('exit').to('exit')
    }
    step2{
        on('next'){ /* do stuff */ }.to('finish')
        on('previous').to('step1')
        on('exit').to('exit')
    }

   exit( /* exit flow before finish */ )
   finish( /* finish up */ )
   handleFlowError( */ not good :( */)

}

step1 GSP....

<g:select name="assessmentType.id" from="${assessmentTypes}" optionKey="id" value="${assessmentType?.id}" />

This is untested but it should work just fine. Enjoy :)

Michael J. Lee
  • 12,278
  • 3
  • 23
  • 39
  • Thanks Michael. This answer may work but after more searching I found http://stackoverflow.com/questions/7158042/grails-web-flow-first-state and a few other references which suggest that an "action" block is needed in the first state. – jlpp Mar 17 '12 at 12:37