Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Working with Arrays in form Bean using different FORM bean scopings

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

Vimala Ranganathan's Blog | November 15, 2006  11:36 AM | Comments (2)


Abstract

This article discusses two types of form scoping and addresses the issue of  working with Array or Complex DataType as a member of the FormBeans

In this sample we have taken an array of checkbox items to demonstrate how  the two form scopings work with form beans.

The article also has samples for both Workshop 8.1 and Workshop 9.2

Form Bean Scopings

There are two different scopings available for Form Beans: (1) request-scoping and (2) page flow-scoping. 

Request-Scoped Form Beans

By default, Form Bean instances that are passed to action methods are request-scoped instances.

<pre class="code">
 /**
* @jpf:action
* @jpf:forward name="success" path="displayData.jsp"
*/
protected Forward submit( MyFormBean requestScopedBean )
{
return new Forward( "success" );
}
</pre>

"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. 

Page Flow-Scoped Form Beans

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.

<pre class="code">
public class myController extends PageFlowController
{
public MyFormBean pageFlowScopedBean = new MyFormBean();
.
.
.
}
</pre>

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.

<pre class="code">
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" );
}
}
</pre>

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.

Arrays or Complex Data Type within Request Scoped Form Bean

Default scope of formBean is Request; therefore, each http request triggers the container to instantiate a new formBean instance. That is, the formBean instance that contains large array will not be used upon subsequent form submission.

The container usually inokes the setXXX method to set the values into the form bean. But in case of arrays or complex object type the container invokes getXXX method for the array type object or complex type object before setting a value to each array element. So you need to modify the getter method and make sure the array is initialized to the correct size. By default workshop would initialize it to 1 and would generate a setter like below
<pre class="code">
public String[] getName() {
// For data binding to be able to post data back, complex types and
// arrays must be initialized to be non-null.
if(this.name == null || this.name.length == 0) {
this.name = new String[1];
}
return this.name;
}
</pre>
You can modify this code to set the size of the String array to any size

When can I not use Arrays with in Request Scoped form beans:

The problem with the above solution is that we need to know the size of the array and if the form bean has a complex object which may have arrays, then we need to know its size too and kind of hardcode the value.
This may not be possible in all situations and page flow scoped form bean will be useful in overcoming these issues.

Arrays or Complex Data Type within PageFlow Scoped Form Bean

This design approach allows a user to solve the following issues.

· a formBean contains non-static array sized array type
· a formBean contains XMLBean Document as a property and one or more elements in this XMLBean Document instance has array type
· getXXX method returns non-null array type as default and usally the array size is set to one. setXXX is used to set larger array sized object but upon submission of form only one element is submitted.

When can I not use Arrays with in Page Flow Scoped form beans:

There are certain situations like standalone portlets when you are using Arrays, you cannot use Pageflow scoped formbeans and in such case you need to use Request Scoped Form Bean.

Code Sample:

In this sample we have demonstrated how an array of a complex java bean works with both request scoped form bean and page flow scoped form bean.

We have defined a java bean "FormListBean" which has a string and a int member variable and the Form Bean has an array of  type "FormListBean"

Setup:

    1. Create a Web Project in your application
    2. Right click and import the folder sample into your web project
    3. Build and Run the Controller
    4. The Controller will bring up a page with a table of strings and checkboxes with default value. 
    5. You can edit and submit the data and the display page will display all the modified data. 
    To test again you can reset the values by clicking the Reset button on the page which will take you to index page

    Conclusion

    This article talks about the two types of form bean scopings and how they can be used when we have arrays or complex object type as member variable of  a form bean and also to be able to modify the data in the array and submit. It discusses the scenario's when a specific form bean scoping cannot be used with form beans having array data type. The article is also accompanied by samples on both 8.1 and 9.2 for both Request Scoped Form Beans and Page Flow Scoped Form Beans.

    References

    Edocs Reference - the BEA edocs page
    RequestScoped_Workshop8.1.zip - the sample on request scoped form bean with arrays on workshop 8.1
    PageFlowScoped_Workshop8.1.zip - the sample on page flow scoped form bean with arrays on workshop 8.1
    RequestScoped_Workshop9.2.zip - the sample on request scoped form bean with arrays on workshop 9.2
    PageFlowScoped_Workshop9.2.zip - the sample on page flow scoped form bean with arrays on workshop 9.2

    Comments

    Comments are listed in date ascending order (oldest first) | Post Comment

    • It is very easy and simple to understand and more over very helpful. Thanks a lot.

      Posted by: rshanmoogam on April 9, 2008 at 3:24 AM

    • Hi Vimala, Very useful and thanks for the simple working example. Thanks! Regards, Mukunr

      Posted by: mkunasek on March 6, 2008 at 7:30 PM



    Only logged in users may post comments. Login Here.

    Powered by
    Movable Type 3.31