This topic explains the different kinds of scoping that Form Beans can possess in a Controller file. It also describes the different data-accumulation behaviors inherent in each sort of scoping.
There are two different scopings available for Form Beans: (1) request-scoping and (2) page flow-scoping.
Request-scoped Form Beans have the same life cycle as a request to the page flow servlet. Each time a new request is made of the servlet, a new Form Bean instance is created. When the request is destroyed, the Form Bean instance is destroyed as well.
Page Flow-scoped Form Beans have the same life cycle as the Page Flow's Controller file instance. Data stored in a Page Flow-scoped Form Bean will be maintaned until the Controller file instance is destroyed.
By default, Form Bean instances that are passed to action methods are request-scoped instances.
/** * @jpf:action * @jpf:forward name="success" path="displayData.jsp" */ protected Forward submit( MyFormBean requestScopedBean ) { return new Forward( "success" ); }
"Request-scoped" means that the Form Bean instance has the same life-cycle as the HTTP request. The Form Bean instance is created at the same time as the request and it is destroyed, along with any data within it, when the request is destroyed. Request-scoped Form Bean instances are useful when you submitting data from a single JSP page to a single action method. But they are cumbersome when your application needs to accumulate data over many different JSP pages and action methods. This is because accumulating data from many different JSP pages entails the creation and destruction of many different request objects, along with the creation and destruction of many different request-scoped Form Bean instances. To preserve the accumulated data, you will need to pass the data from the previous Form Bean instance into the new Form Bean instance at each stage in the process. As an alternative to a string of request-scoped Form Beans, you could use a single Page Flow-scoped Form Bean instance to hold the accumulated data.
Page Flow-scoped Form Bean instances have the same life-cycle as the Controller file instance. They are created and destroyed when the Controller file instance is created and destroyed. This makes Page Flow-scoped Form Beans useful for storing data that has been accumulated across many different JSP pages.
To create a Page Flow-scoped Form Bean instance, construct a public member variable of the Form Bean in the Controller file.
public class myController extends PageFlowController { public MyFormBean pageFlowScopedBean = new MyFormBean(); . . . }
Once you have created a Page Flow-scoped instance of a Form Bean, you can pass the instance to action methods by using the @action form="form_bean" annotation.
public class myController extends PageFlowController { public MyFormBean pageFlowScopedBean = new MyFormBean(); /** * @jpf:action form="pageFlowScopedBean" * @jpf:forward name="success" path="displayData.jsp" */ protected Forward submit( MyFormBean form ) { return new Forward( "success" ); } }
Each time the submit() method is invoked, it is passed the same instance of the Form Bean, namely, pageFlowScopedBean, the instance that was created when the Controller file instance was created.
The following section details what happens when one action method passes Form Bean data to another action method. In the following list, assume that A is an action method that invokes action method B.
/** * @jpf:action form="pageFlowScopedBean" * @jpf:forward name="success" path="B.do" */ protected Forward A(MyFormBean form) { return new Forward("success"); } /** * @jpf:action */ protected Forward B(MyFormBean form) { return new Forward("success"); }
/** * @jpf:action form="pageFlowScopedBean" * @jpf:forward name="success" path="B.do" */ protected Forward A(MyFormBean form) { return new Forward("success", form); } /** * @jpf:action */ protected Forward B(MyFormBean form) { return new Forward("success"); }
/** * @jpf:action * @jpf:forward name="success" path="B.do" */ protected Forward A(MyFormBean form) { return new Forward("success"); } /** * @jpf:action form="pageFlowScopedBean" */ protected Forward B(MyFormBean form) { return new Forward("success"); }
/** * @jpf:action * @jpf:forward name="success" path="B.do" */ protected Forward A(MyFormBean form) { return new Forward("success", form); } /** * @jpf:action form="pageFlowScopedBean" */ protected Forward B(MyFormBean form) { return new Forward("success"); }
/** * @jpf:action form="pageFlowScopedBean" * @jpf:forward name="success" path="B.do" */ protected Forward A(MyFormBean form) { return new Forward("success"); } /** * @jpf:action form="pageFlowScopedBean" */ protected Forward B(MyFormBean form) { return new Forward("success"); }