|
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.There are two different scopings available for Form Beans: (1) request-scoping and (2) page flow-scoping.
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 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.
<pre class="code">You can modify this code to set the size of the String array to any size
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>
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.
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.
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.
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 |
Issues with popup window and pageflows/portlets
Working with Arrays in form Bean using different FORM bean scopings
An Introduction to the Enterprise JavaBeans 3.0 (EJB 3) Specification
The EJB 3.0 specification goes a long way toward making the EJB programming experience a pleasant one by simplifying development, facilitating test-driven development, and focusing more on plain Java objects. This article takes a look at the specification, and how to build EJBs in the new model. Mar. 29, 2006
WS-Security and Java: Practical, In-Depth, Message-Based Security
This article looks at WS-Security and related technologies such as XML Signatures and XML Encryption, with a brief look at other protocols in this space. May. 26, 2004
All articles by Vimala Ranganathan »