Rendering views Introduction This chapter shows you how to use the view-state element to render views within a flow. Defining view states Use the view-state element to define a step of the flow that renders a view and waits for a user event to resume: ]]> By convention, a view-state maps its id to a view template in the directory where the flow is located. For example, the state above might render /WEB-INF/hotels/booking/enterBookingDetails.xhtml if the flow itself was located in the /WEB-INF/hotels/booking directory. Below is a sample directory structure showing views and other resources like message bundles co-located with their flow definition: Flow Packaging Specifying view identifiers Use the view attribute to explictly specify the id of the view to render. Flow relative view ids The view id may be a relative path to view resource in the flow's working directory: ]]> Absolute view ids The view id may be a absolute path to a view resource in the webapp root directory: ]]> Logical view ids With some view frameworks, such as Spring MVC's view framework, the view id may also be a logical identifier resolved by the framework: ]]> See the Spring MVC integration section for more information on how to integrate with the MVC ViewResolver infrastructure. View scope A view-state allocates a new viewScope when it enters. This scope may be referenced within the view-state to assign variables that should live for the duration of the state. This scope is useful for manipulating objects over a series of requests from the same view, often Ajax requests. A view-state destroys its viewScope when it exits. Allocating view variables Use the var tag to declare a view variable. Like a flow variable, any @Autowired references are automatically restored when the view state resumes. ]]> Assigning a viewScope variable Use the on-render tag to assign a variable from an action result before the view renders: ]]> Manipulating objects in view scope Objects in view scope are often manipulated over a series of requests from the same view. The following example pages through a search results list. The list is updated in view scope before each render. Asynchronous event handlers modify the current data page, then request re-rendering of the search results fragment. ]]> Executing render actions Use the on-render element to execute one or more actions before view rendering. Render actions are executed on the initial render as well as any subsequent refreshes, including any partial re-renderings of the view. ]]> Binding to a model Use the model attribute to declare a model object the view binds to. This attribute is typically used in conjunction with views that render data controls, such as forms. It enables form data binding and validation behaviors to be driven from metadata on your model object. The following example declares an enterBookingDetails state manipulates the booking model: ]]> The model may an object in any accessible scope, such as flowScope or viewScope. Specifying a model triggers the following behavior when a view event occurs: View-to-model binding. On view postback, user input values are bound to model object properties for you. Model validation. After binding, if the model object requires validation that validation logic will be invoked. For a flow event to be generated that can drive a view state transition, model binding must complete successfully. If model binding fails, the view is re-rendered to allow the user to revise their edits. Suppressing binding Use the bind attribute to suppress model binding and validation for particular view events. The following example suppresses binding when the cancel event occurs: ]]> Specifying bindings explicitly Use the binder element to configure the exact set of model bindings usable by the view. This is particularly useful in a Spring MVC environment for restricting the set of "allowed fields" per view. ]]> If the binder element is not specified, all public properties of the model are eligible for binding by the view. With the binder element specified, only the explicitly configured bindings are allowed. Each binding may also apply a converter to format the model property value for display in a custom manner. If no converter is specified, the default converter for the model property's type will be used. ]]> In the example above, the shortDate converter is bound to the checkinDate and checkoutDate properties. Custom converters may be registered with the application's ConversionService. Each binding may also apply a required check that will generate a validation error if the user provided value is null on form postback: ]]> In the example above, all of the bindings are required. If one or more blank input values are bound, validation errors will be generated and the view will re-render with those errors. Validating a model Model validation is driven by constraints specified against the model object. These constraints may be specified declaratively, or enforced using a programmatic validation routine or external Validator. Programmatic validation There are two ways to perform model validation programatically. Implementing a model validate method The first way is to define a validate method on the model object class. To do this, create a public method with the name validate${state}, where state is the id of your view-state. The method must declare a MessageContext parameter for recording validation error messages. For example: Implementing a Validator The second way is to define a separate object, called a Validator, which validates your model object. To do this, create a class that defines a public method with the name validate${state}, where state is the id of your view-state. The method must declare a parameter to accept your model object, and a MessageContext parameter for recording validation error messages. For example: A Validator can also accept a Spring MVC Errors object, which is required for invoking existing Spring Validators. Validators must be registered as Spring beans employing the naming convention ${model}Validator to be detected and invoked automatically. In the example above, Spring 2.5 classpath-scanning would detect the @Component and automatically register it as a bean with the name bookingValidator. Then, anytime the booking model needs to be validated, this bookingValidator instance would be invoked for you. Declarative validation Spring Web Flow does not yet ship integration with a declarative validation framework such as Hibernate Validator. It is expected that integration will be provided in a future Web Flow release. This integration will allow declarative validation constraints to be defined against model properties. Suppressing validation Use the validate attribute to suppress model validation for particular view events: ]]> In this example, data binding will still occur on back but validation will be suppressed. 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 itself from executing. If an error result occurs, the view will re-render and should typically display an appropriate error 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: ]]> Handling events From a view-state, transitions without targets can also be defined. Such transitions are called "event handlers": <-- Handle event --> ]]> 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 partials Use the render element to request partial re-rendering of a view after handling an event: ]]> 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. Handling global events Use the flow's global-transitions element to create event handlers that apply across all views. Global-transitions are often used to handle global menu links that are part of the layout. ]]> Working with messages Spring Web Flow's MessageContext is an API for recording messages during the course of flow executions. Plain text messages can be added to the context, as well as internationalized messages resolved by a Spring MessageSource. Messages are renderable by views and automatically survive flow execution redirects. Three distinct message severities are provided: info, warning, and error. In addition, a convenient MessageBuilder exists for fluently constructing messages. Adding plain text messages Adding internationalized messages Using message bundles Internationalized messages are defined in message bundles accessed by a Spring MessageSource. To create a flow-specific message bundle, simply define messages.properties file(s) in your flow's directory. Create a default messages.properties file and a .properties file for each additional Locale you need to support. From within a view or a flow, you may also access message resources using the resourceBundle EL variable: ]]> Displaying popups Use the popup attribute to render a view in a modal popup dialog: ]]> When using Web Flow with the Spring Javascript, no client side code is necessary for the popup to display. Web Flow will send a response to the client requesting a redirect to the view from a popup, and the client will honor the request. View backtracking By default, when you exit a view state and transition to a new view state, you can go back to the previous state using the browser back button. These view state history policies are configurable on a per-transition basis by using the history attribute. Discarding history Set the history attribute to discard to prevent backtracking to a view: ]]> Invalidating history Set the history attribute to invalidate to prevent backtracking to a view as well all previously displayed views: ]]>