< 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:
The pageFlow AttributeEvery 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 AttributeThe 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 AttributeThe 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 AttributeThe 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 AttributeThe 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 AttributeThe 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 AttributeThe 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 AttributeThe 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 AttributeThe 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
|
< Day Day Up > |