7

Ending Conversations

The "Confirm" button is bound to the action method confirm() of HotelBookingAction.

<h:commandButton value="Confirm" action="#{hotelBooking.confirm}" class="button"/>

The confirm() method is tagged with the @End annotation, which ends the long-running conversation and results in all state associated with the conversation being destroyed at the end of the request. Actually, since the confirmed outcome triggers a redirect, the state will not be destroyed until after the redirect completes. Note that even the success message that we create using the built-in facesMessages component is transparently propagated across the redirect!

@End public String confirm() { if (booking==null || hotel==null) return "main"; em.persist(booking); if (bookingList!=null) bookingList.refresh(); facesMessages.add("Thank you, #{user.name}, your confimation number for #{hotel.name} is #{booking.id}"); return "confirmed"; } @End public String cancel() { return "main"; } @Destroy @Remove public void destroy() {} }

When the conversation state is finally destroyed, Seam calls the @Destroy method, which results in removal of the SFSB.

The HotelBookingAction bean is marked @Conversational. This means that none of its methods may be called outside of a long-running conversation. So if we try to use the back button after the end of the conversation, Seam will redirect to the main page, with an error message.