springspring-webflowspring-webflow-2

Triggering event on input/select value change with Spring web flow


I wanna trigger flow change when user change the value in select box:

<select>
  <options>
    <option>1</option>
    <option>2</option>
  </options>
</select>

After reading this docs: https://docs.spring.io/spring-webflow/docs/current/reference/html/spring-mvc.html#spring-mvc-resuming-on-event I did not find the answer. Please can any one help? Any advice appreciated.

The change I wanna achieve is to set button to be activated, when value has been chosen from select box.


Solution

  • there are 2 ways to do this:

    First, using strictly javascript (all in the UI), by using onchange and/or onclick on your select element to find the button and activate/deactivate it

    Second, using Webflow:

    Assuming you want to go with webflow here, you can use this:

    JSP

    <form id="myFormId" action="${flowExecutionUrl}" method="post">
        <select id="mySelectId" onchange="Spring.remoting.submitForm('mySelectId', 'myFormId', {fragments:'body', _eventId: 'myChangeEvent'}); return false;">
            ...
        </select>
    </form>
    

    Flow

    <view-state id="myViewStateId">
        <transition on="myChangeEvent" validate="false" bind="true">
            <!-- change some property to enable your button -->
        </transition>
    </view-state>
    

    This will re-render the view and the property/attribute you are using to enable/disable your button will be updated. It is important to use validate="false" otherwise validation error(s) could prevent your transition from succeeding