Wednesday, December 11, 2013

Faces Flow is not necessary in JSF

Spring has the nice feature of WebFlow. JSF 2.2 comes with Faces Flow. But I think Faces Flow is not needed in JSF.
First, Let us try to understand why WebFlow is necessary in Spring MVC. Spring MVC is a generic MVC. Itself does not provides any rendering technology. Instead it provides a set of nice hook to make it easy to use other rendering technology such as validation,  data binding, request dispatcher, web flow. But JSF is complete MVC framework with all the features from Spring MVC. For example, JSF takes advantage of EL for bidirectional data-binding. It has clearly defined validation and conversion plugin.  I will compare the WebFlow feature from Spring with similar feature in JSF.

FlowScope

JSF needs to run in CDI context. CDI defines ConversationScope. This is a long-lived versatile scope and be used for anything.  It can be definitely used for scope in flow.

View

A view in JSF can simply be a facelet page. The page will has all the data-binding, validation, rendering from JSF.

Action

In Spring WebFlow, you can execute action, basically method call, in various stages such as beginning or end of the flow, beginning or end of a view, or before rendering a view.
These actions can be implemented in JSF in various ways. First, JSF has PhaseListener, SystemEventListener, etc. You can hook up your event listened to JSF lifecycle, view or a particular component. The possibility is infinite. ActionListener  and Action can also be attached to UICommand. Any business logic can be performed here. We can also perform initialization and cleanup logic for flow using CDI PostConstruct and PreDestroy with ConversationScope bean.

View Branching

Suppose you are going to do transition from one view. The next view is selected based on some outcome result. This just corresponds to JSF navigation case. You can use face-config.xml or embed the logic in your action method.

I can not see any WebFlow features can not be implemented with JSF 2.1.

Note: by the way, Flow from JSF 2.2 is buggy at current stage. Simple static flow management is ok, but a complex method call will give you headache. I suggest you use Conversation instead of Flow to manage a flow.

2 comments:

  1. Hi Jason,

    Thanks for taking the time to write up this blog entry. I just wanted to mention that Faces Flows will greatly benefit JSF portlet developers: https://www.liferay.com/web/neil.griffin/blog/-/blogs/three-cheers-for-jsf-2-2-faces-flows

    Spring Web Flow uses its own internal portlet integration and does not implement the JSR 329 portlet bridge standard. Unfortunately it is incompatible with JSF 2.x portlet bridges that extend JSR 329: http://docs.spring.io/spring-webflow/docs/2.3.x/reference/html/ch14s06.html

    Kind Regards,

    Neil

    ReplyDelete
  2. You could be right. I have no experience in portlet. So far, I only developed servlet-based web application.

    ReplyDelete