0
votes

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>
1
Found another question that's essentially the same: stackoverflow.com/questions/1002170/grails-web-flow. This question can be closed. - jlpp

1 Answers

2
votes

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 :)