Search code examples
primefacesspring-webflow

Spring Webflow and primefaces ActionListners


How can I invoke a Spring Webflow transition from a Primefaces actionlistner?

I have a Primefaces component SelectOneMenu. Whenever the value of the Component changes, I want to invoke the transition in Spring Webflow to display the description on the SelectItem in a outPutText.

I am new to the Spring Webflow and Primefaces.

Please let me know if the following is incorrect.

JSF Code

<h:outputText value="Script " style="font-weight: bold" />
    <p:selectOneMenu value="#{selectedDN.cdrScript}" >
      <p:ajax event="change" listener="scriptChange" update="scriptDesc" />
      <f:selectItems value="#{sctiptOptions}" />
    </p:selectOneMenu>
<h:outputText value="#{scriptDesc}" id="scriptDesc" />

Web flow Config for the View

<view-state id="Edit">
<on-entry>
<evaluate expression="scriptService.getOptions()"
              result="flowScope.sctiptOptions"/>
  <set name="flowScope.scriptDesc" value="selectedDN.cdrScript.scriptDesc"/>
    </on-entry>
<transition on="scriptChange">
  <set name="flowScope.scriptDesc"
       value="flowScope.selectedDN.cdrScript.scriptName"/>
  <render fragments="form:scriptDesc" />
</transition>

<transition on="Update" to="Edit">
</transition>

<transition on="Cancel" to="View">
</transition>
</view-state>

Solution

  • You dont need SWF transitions or listeners for this. Something like the following will work:

    <h:outputText value="Script " style="font-weight: bold" />
        <p:selectOneMenu value="#{selectedDN.cdrScript}" >
          <p:ajax event="change" update="scriptDesc" />
          <f:selectItems value="#{scriptService.getOptions()}" var="script" itemLabel="#{script.scriptName}" itemValue="#{script.id}"/>
        </p:selectOneMenu>
    <h:outputText value="#{selectedDN.cdrScript.scriptName}" id="scriptDesc" />
    

    And add 'selectedDN' variable definition if not already there:

    <view-state id="Edit">
    <on-entry>
      <set name="flowScope.selectedDN" value="scriptService.getCurrentSelectedDN()"/>
    </on-entry>
    

    Note the addition of itemLabel and itemValue attributes. You will likely also need a converter specified on selectOneMenu.

    SWF does not play well out of the box with PF partial rendering. Add the following to MVC servlet config under tag:

            <webflow:flow-execution-attributes>
                <webflow:redirect-in-same-state value="false"/>
            </webflow:flow-execution-attributes>