Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, December 10, 2009

Spring Web Flow 10000 ft Overview

Spring Web Flow (SWF) is a workflow engine designed specifically for Web page navigation. SWF flow definition language is a Domain-Specific Language (DSL) written in XML. Developers use this language to define State Machines.


Here is a visualized main flow from the swf-booking-faces sample project:


There are quite a few benefits of using workflow-based UI process frameworks such as Spring Web Flow. Among them are:
  1. UI flow are visible and easier to understand
  2. Complex UI interaction is abstracted and modularized into flows and can be reused

Other comparable technologies is Seam + jBMP.

When using Spring Web Flow, you basically replace "controllers" in MVC with flows, because flows capture controlling logic. Creating and setting models has become the responsibility of the flows.


The most commonly used state is ViewState. ViewState displays a view to user. User's action on that view, such as clicking a button, generates an event that is handled by the view state. Typically event handling results in transition to other state. Each view state has a corresponding view template. By default, view template is located at the same folder where the flow is located. For example: enterSearchCriteria view state has a view template enterSearchCriteria .xhtml.

<view-state id="enterSearchCriteria">
<on-render>
<evaluate expression="bookingService.findBookings(currentUser.name)" result="viewScope.bookings" result-type="dataModel" />
</on-render>
<transition on="search" to="reviewHotels">
<evaluate expression="searchCriteria.resetPage()" />
</transition>
<transition on="cancelBooking">
<evaluate expression="bookingService.cancelBooking(bookings.selectedRow)" />
<render fragments="bookingsFragment" />
</transition>
</view-state>


In this case, transition happens when user perform "search" action, which is defined in view template file as:

<sf:commandButton id="findHotels" value="Find Hotels" processIds="*" action="search" />

<on-render>is used to prepare the a ViewState for rendering. You can define actions in EL within a State to perform additional tasks such as invoking business service. Actions can be inserted into different places within a State: on state entry, on state exit, on view render, on transition etc.


Flow can have inputs (using <input>) and outputs (<output> elements in EndState).


Flow can also declare flow instance variables by using <var>.


Other type of states include:

  • ActionState: used to perform an action and transition to another state based on the outcome.
  • DecisionState: is similar to ActionState, with a different if-else syntax.
  • SubflowState: SubflowState invokes a subflow. Caller flow can pass in input parameters to subflow and check the output of the subflow to determine the transition.
  • EndState: flow is terminated when it enters in an EndState.


Friday, December 4, 2009

Spring @MVC Explained

SpringTravel project is used here as an example to illustrate how Spring MVC works.

The following diagram depicts the processing flow of Spring MVC.




Spring provides a Dispatcher Servlet called DispatcherServlet, which maps to an URI path pattern, in this case it is /app/*. You configure the servlet in web.xml.


    <servlet>















<servlet-name>















Spring MVC Dispatcher Servlet















</servlet-name>















<servlet-class>















org.springframework.web.servlet.DispatcherServlet















</servlet-class>















<init-param>















<param-name>contextConfigLocation</param-name>















<param-value>/WEB-INF/web-application-config.xml</param-value>















</init-param>















<load-on-startup>1</load-on-startup>















</servlet>































<servlet-mapping>















<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>















<url-pattern>/app/*</url-pattern>















</servlet-mapping>

















Controller

Controllers are annotated with @Controller. The Spring MVC Dispatcher Servlet scans for all the conroller classes. There is no need to define the controllers as beans in the conext xml file, Spring automatically does that for us. All we have to do is adding one line in the context file
web-application-config.xml, which is passed in as an initialization paramter to the Spring MVC Dispatcher Servlet.


    <context:component-scan base-package="com.springsource.springtravel.hotels" />

















Here is an example of a controller. URI path /app/hotels/* is mapped to this controller. Each handler method further narrows it down by additional URI path pattern and http method. For example search() method maps to /app/hotels/search and GET method. Handler methods have flexible signature and return type. In this example, they return three types:


  • void: view is implied by RequestToViewNameTranslator

  • string: view name

  • Model object: view is implied by RequestToViewNameTranslator

@Controller















@RequestMapping("/hotels/*")















public class HotelsController {































...































@RequestMapping(value = "index", method = RequestMethod.GET)















public void index(HotelSearchCriteria searchCriteria) {















}















































@RequestMapping(value = "search", method = RequestMethod.GET)















public String search(HotelSearchCriteria searchCriteria,















BindingResult bindResult, Model model) {















...















if (bindResult.hasErrors()) {















return "hotels/index";















} else {















model.addAttribute("hotels", searchService.search(searchCriteria));















return "hotels/search";















}















}































@RequestMapping(value = "details", method = RequestMethod.GET)















@ModelAttribute("hotel")















public Hotel details(@RequestParam("id") Long id) {















return searchService.getHotel(id);















}































}

















Model

Model in Spring MVC is actually a map, which contains beans (map values) and bean names (map keys). Model is completely separated from view and view rendering technology.

A special type of model is Form Model. Form model is specified in the form tag as following:

<form:form modelAttribute="hotelSearchCriteria" action="search" method="get">
In the handler method, form model object can be passed in as method parameter, or returned as return value. By convention-over-configuration, Spring figures out form model object name by its class name. You can always use @ModelAttribute if the names does not match.

Spring MVC vs JSF
Spring MVC is truly a MVC framework. However developers still need to deal with low level HTTP details such as URI path, form parameters to a certain degree. In the sense, Spring MVC lacks UI component model that can be found in JSF.