View Controller

The ViewController is just a concept which binds a specific bean to a web-page. In JSF this means, you have a managed bean configured which directly matches to a view-id.

If you follow this concept you'll gain some very comfortable things. They are:

Notifications:

  • initView

    This event fill be fired after the JSF RESTORE_VIEW phase.
  • preProcess

    This event fill be fired before the JSF INVOKE_APPLICATION phase.
  • preRender

    This event fill be fired before the JSF PRE_RENDER phase.
The ViewController concept is also the base for the core15 annotation @ConversationRequire which allows you to issue a redirect if a conversations timed-out in the mid of a e.g. wizzard style pageflow.

Installation

In a JSF environment Orchestra already configures the JSF system in a way which allows you to start using this concept. This has been done by installing the PhaseListener org.apache.myfaces.orchestra.viewController.jsf.ViewControllerPhaseListener.

However, there is a way which allows you to adjust the configuration as required. To make the ViewController work you need to have two things:
  • A NameMapper (org.apache.myfaces.orchestra.viewController.ViewControllerNameMapper)

    The NameMapper is responsible to build a managed-bean name out of the view-id.
  • An Executor (org.apache.myfaces.orchestra.viewController.AbstractViewControllerExecutor)

    The Executor finally will invoke some methods on this managed-bean then (if there is a bean with the mapped name).
Both things will be used by the ViewControllerManager. Now, if you are in need of a custom name mapping scheme, just create jour own ViewControllerManager by inheriting from the org.apache.myfaces.orchestra.viewController.AbstractViewControllerManager class. Two methods you have to implement allow you to provide custom implementations for the >NameMapper and the Executor.

Finally you have to configure your implementation by adding an bean definition with the name org.apache.myfaces.orchestra.viewController.ViewControllerManager. For example:
<bean
    name="org.apache.myfaces.orchestra.viewController.ViewControllerManager"
    class="your.package.YourViewController"
    scope="singleton">

</bean>
					

DefaultViewControllerNameMapper

There is a default NameMapper which will be used if you do not provide your own. The mapping will be as follows:
  • Convert each character after a "/" to upper-case
  • Remove any occurence of the "/" character
  • Stop at the first "." character and remove the rest
  • Prefix with a "_" character if the result is a reserved word or an invalid bean name
Examples:
View-Id Bean name
mainform.jsp mainform
userData/password.jsp userDataPassword
requestScope.jsp _requestScope
123set.jsp _123set

ViewControllerExecutor (AbstractViewControllerExecutor)

The ViewControllerExecutor is responsible to invoke the notification methods on your managed-bean.

Currently the Orchestra standard provides the following methods:
  • ReflectiveViewControllerExecutor (default)

    The ReflectiveViewControllerExecutor uses reflection to lookup the following public methods: initView, preRenderView, preProcess.

    All of the methods are optional.
  • InterfaceViewControllerExecutor

    The InterfaceViewControllerExecutor requires you to implement the org.apache.myfaces.orchestra.viewController.ViewController interface.
  • AnnotationsViewControllerExecutor (core15)

    The AnnotationsViewControllerExecutor might be the most powerful one. It allows you to configure a managed bean as view controller without following any naming scheme. Not for the bean name nor for the method name.

    This is a feature of the core15 module.
Again, you have to configure a custom ViewControllerManager if you do not want the default ReflectiveViewControllerExecutor.

AnnotationsViewController

To work with the AnnotationsViewController you have to use the core15 module. Once you added the jar to your classpath and configured the required import statement in your configuration file you are done.

The biggest advantage the AnnotationsViewController provides is, that you do not have to follow any naming scheme. Just annotate your managed-bean with the @ViewController annotation.

Example:
@ViewController(
	viewIds={"/annotations/Page1.jsp", "/annotations/Page2.jsp", "/annotations/Page3.jsp"})
public class MultiViewController
{
				
Which means the class MultiViewController is responsible for the three configured pages and will receive the notification to the methods annotated with the
  • @InitView
  • @PreProcess
  • @PreRenderView
annotation.