Data Binding with Page Flow

 <  Day Day Up  >  

In the casino application, you might have noticed that no data has been stored yet. This section explains the different attributes where data can be stored:

  • pageFlow

  • globalApp

  • actionForm

  • request

  • url

  • session

  • container

  • pageContext

  • application

The pageFlow Attribute

Every Page Flow application can have attributes associated with it. They are added to the class definition of the Controller created in Workshop. These attributes are available to all the actions and any other methods (business methods or exception handlers) you might have. They are used to pass information back and forth from actions or hold results that might have occurred from within an action. For instance, you could have a Page Flow variable that holds an array of data. In the casino example used so far, the amount of chips purchased is simply retrieved from the HTTPRequest object in the confirmation.jsp . It's likely that other actions or even other JSPs will want to use this value. Adding a Page Flow attribute would look like this:

 public class Controller extends PageFlowController {         public int amt;         . . . } 

The variable amt is now available to all the Controller code and other JSPs. To display the attribute from a JSP, use a < netui :label> tag, as shown in the following code:

 <netui:label value="{pageFlow.amt}"></netui:label> 

Within the value attribute of the <netui:label> tag is an XScript expression to access the variable. XScript expressions consist of two parts : where the attribute is found and the attribute itself. The different attribute areas are the data binding areas you're looking at in this section. The example uses {pageFlow.amt} , which means it's an attribute of the Controller and the attribute's name is amt . XScript expressions are used throughout this book to access different attributes.

The globalApp Attribute

The globalApp attribute is available to all Page Flows. For example, you might want to store the username for all Page Flows. Declare an attribute of your Controller class to reference the global application:

 protected global.Global globalApp 

This attribute can now be used in the Controller or accessed via XScript in a JSP, as shown here:

 {globalApp.globalVariableName} 

The actionForm Attribute

The actionForm attribute is used to access form values from a Form bean. This attribute is used similarly to other attributes; it's accessed via XScript as shown here:

 {actionForm.formObject} 

The "Using Form Beans" section later in this chapter covers creating and using Form beans and includes more examples of using the actionForm attribute.

The request Attribute

The request attribute represents the HTTPRequest object. It can be used to access Form bean values and request attributes. The following code adds an attribute to the request from within the Controller:

 getRequest().setAttribute("chipAmt",amount); 

You might want highRoller.jsp to display the amount of chips someone has purchased, as the confirmation page does. Again, you use the <netui:label> tag to display this value, but this time you use the following XScript expression as the value attribute:

 {request.chipAmt} 

The request attribute should be used whenever the value you want to store needs to exist only temporarily. The request values exist only as long as the HTTPRequest object does; after HTTPResponse has been returned to the client, the request attribute no longer exists.

The url Attribute

The url attribute is used to access query string parameters. If you have a URL such as http://www.mycompany.com?testVal=7 , you could access the testVal query string parameter by using this XScript expression:

 {url.testVal} 

The session Attribute

The session attribute is used to store attributes in the HTTPSession object. Attributes stored in HTTPSession exist as long as the sessionID is valid. The value for the server's session timeout determines the validity of the sessionID . To add an attribute to the session from the Controller, use the following:

 getSession().setAttribute("key",object); 

To access the session attribute with an XScript expression, use the following statement:

 {session.key} 

The container Attribute

The container attribute is used to access data in a complex form. Typically, it is data retrieved from a database and represented as a RowSet .

The pageContext Attribute

The pageContext attribute represents the JSP Page Context, which can be used to access some of the other attributes covered in this section, such as actionForm or request . Attributes created in the Page Context are available only to that JSP. You can use the pageContext attribute if you have a static value that needs to be repeated several times in a page. If you ever need to change the value, you would have to change it in only one place as opposed to searching for all the places the static value is used.

The application Attribute

The application attribute is used to access values in the ServletContext . The values in the ServletContext are read-only values; therefore, there's no way to change them. These values are used for environment variables and other deployment configuration properties. Table 4.1 lists the data binding options and their scope.

Table 4.1. Scoping of the Data Binding Attributes

DATA BINDING ATTRIBUTE

SCOPE

pageFlow

Data stored here is available throughout user interaction with a Page Flow. If a Page Flow is exited, such as a nested Page Flow, any data is destroyed .

One caveat for pageFlow attributes is using large data sets. Some information is stored indirectly in the session, which is replicated when clustered.

globalApp

Data stored with this scope is available from the first time a user accesses a page until that user is no longer using the application.

Be careful when using this scope with large data sets; it is replicated across a cluster, and large amounts of data increase network traffic.

actionForm

When a form is submitted from a page, it calls an action. The action has a populated Form bean to use. When the action is finished and the next page is determined, the actionForm attribute is then created and represents the Form bean.

request

Data stored with this scope is available from the time a page is submitted until the response is returned to the browser.

url

This is a read-only scope. Its lifetime is the same as the request.

session

Data stored in session is just like the globalApp scope, with the same warning about large data sets.

container

The container scope contains the current item from a large data set. It's often used when looping through a data set.

pageContext

Attributes defined with the pageContext scope are available only to the JSP that defines them.

application

These are read-only attributes that are available after the Web application is loaded the first time.

SHOP TALK : CHOOSING THE RIGHT DATA BINDING OPTION

You might be wondering which option to use for binding data values. The key decision is determining the variable's lifetime.

There are definitely some similar choices among the different types, such as choosing between the session and Page Flow scopes. The advantage of Page Flow variables is that it's easier to use them within the Page Flow framework. Page Flow variables are an easy way to pass information back and forth in the Controller.

The session scope isn't harder to use, but it requires you to write more lines of code. In each method where you want to use the session value, you have to use the setAttribute() method. Another important factor is that Page Flow variables last only as long as the Page Flow is active. If your site is composed of several Page Flows, the Page Flow variables aren't accessible in the Page Flows they're not declared in. In this situation, storing the variables as session attributes is the only way to go. You can think of the Page Flow scope as being the middle ground between the request and session scopes.


 <  Day Day Up  >  


BEA WebLogic Workshop 8.1 Kick Start
BEA WebLogic Workshop 8.1 Kick Start: Simplifying Java Web Applications and J2EE
ISBN: 0672326221
EAN: 2147483647
Year: 2004
Pages: 138

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net