Expert Spring MVC and Web Flow

(Dana P.) #1

Exception Handling


Exceptions within Spring Web Flow are managed the same way that exceptions are handled
within the rest of the Spring Framework. Exceptions from which you cannot recover are
treated as unchecked exceptions.
Spring MVC provides org.springframework.web.servlet.HandlerExceptionResolverto
handle exceptions, but they are common for the entire web application. Given that web flows
are self-contained units of work, it would be inappropriate to allow web flow-specific excep-
tions to leak out of the web flow if the flow knows how to handle them.
Spring Web Flow defines a single interface, org.springframework.webflow.
StateExceptionHandler(Listing 12-29) for handling exceptions thrown by the execution
of a state.


Listing 12-29.org.springframework.webflow.StateExceptionHandler


public interface StateExceptionHandler {
boolean handles(StateException exception);


ViewSelection handle(
StateException exception,
FlowExecutionControlContext context);
}


■NoteThe exception handling within Spring Web Flow is for exceptions thrown by states within flows.


To register your own ExceptionHandler, simply define it within your web flow definition


.
Upon encountering an exception thrown by a state, Spring Web Flow will traverse all reg-
istered implementations of org.springframework.webflow.StateExceptionHandler, and if one
can handle the exception, it will hand it off for processing.

■CautionThis means that the order in which ExceptionHandlers are defined is important, Spring Web
Flow will stop as soon as it finds a StateExceptionHandlerthat can handle the specified exception.


Table 12-13 lists the common exceptions that might be thrown by Spring Web Flow during
normal runtime execution.


CHAPTER 12 ■ADVANCED SPRING WEB FLOW 367
Free download pdf