4.12. Executing view transitions

Define one or more transition elements to handle user events that may occur on the view. A transition may take the user to another view, or it may simply execute an action and re-render the current view. A transition may also request the rendering of parts of a view called "fragments" when handling an Ajax event. Finally, "global" transitions that are shared across all views may also be defined.

Implementing view transitions is illustrated in the following sections.

Transition actions

A view-state transition can execute one or more actions before executing. These actions may return an error result to prevent the transition from exiting the current view-state. If an error result occurs, the view will re-render and should display an appropriate message to the user.

If the transition action invokes a plain Java method, the invoked method may return false to prevent the transition from executing. This technique can be used to handle exceptions thrown by service-layer methods. The example below invokes an action that calls a service and handles an exceptional situation:

<transition on="submit" to="bookingConfirmed">
    <evaluate expression="bookingAction.makeBooking(booking, messageContext)" />
</transition>
			
public class BookingAction {
   public boolean makeBooking(Booking booking, MessageContext context) {
       try {
           bookingService.make(booking);
           return true;
       } catch (RoomNotAvailableException e) {
           context.addMessage(new MessageBuilder().error().
               .defaultText("No room is available at this hotel").build());
           return false;
       }
   }
}
			
[Note]Note

When there is more than one action defined on a transition, if one returns an error result the remaining actions in the set will not be executed. If you need to ensure one transition action's result cannot impact the execution of another, define a single transition action that invokes a method that encapsulates all the action logic.

Global transitions

Use the flow's global-transitions element to create transitions that apply across all views. Global-transitions are often used to handle global menu links that are part of the layout.

<global-transitions>
    <transition on="login" to="login" />
    <transition on="logout" to="logout" />
</global-transitions>
			

Event handlers

From a view-state, transitions without targets can also be defined. Such transitions are called "event handlers":

<transition on="event">
    <!-- Handle event -->
</transition>
			

These event handlers do not change the state of the flow. They simply execute their actions and re-render the current view or one or more fragments of the current view.

Rendering fragments

Use the render element within a transition to request partial re-rendering of the current view after handling the event:

<transition on="next">
    <evaluate expression="searchCriteria.nextPage()" />
    <render fragments="searchResultsFragment" />            
</transition>
			

The fragments attribute should reference the id(s) of the view element(s) you wish to re-render. Specify multiple elements to re-render by separating them with a comma delimiter.

Such partial rendering is often used with events signaled by Ajax to update a specific zone of the view.