10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Configuring Beans<br />

438<br />

a. Start typing the name of the class you are looking for in the Class Name field. While you<br />

are typing, the dialog will show the matching classes.<br />

b. Select the class from the Matching Classes box.<br />

c. Click OK.<br />

8. In the Add Managed Bean dialog:<br />

a. Select the bean’s scope from the Scope menu.<br />

b. Click Add.<br />

9. In the Projects tab, right-click the bookstore1 project, and select Undeploy and Deploy.<br />

<strong>The</strong> preceding steps will add the managed-bean element and three elements inside of it: a<br />

managed-bean-name element, a managed-bean-class element and a managed-bean-scope<br />

element. You will need to edit the XML of the configuration file directly to further configure this<br />

managed bean.<br />

<strong>The</strong> managed-bean-name element defines the key under which the bean will be stored in a scope.<br />

For a component’s value to map to this bean, the component tag’s value attribute must match<br />

the managed-bean-name up to the first period. For example, this value expression maps to the<br />

shape property of the ImageArea instance, NA:<br />

value="#{NA.shape}"<br />

<strong>The</strong> part before the period (.) matches the managed-bean-name of ImageArea. “Adding UI<br />

Components to a Page Using the HTML Component Tags” on page 325 has more examples of<br />

using the value attribute to bind components to bean properties.<br />

<strong>The</strong> managed-bean-class element defines the fully qualified name of the <strong>Java</strong>Beans component<br />

class used to instantiate the bean. It is the application developer’s responsibility to ensure that<br />

the class complies with the configuration of the bean in the application configuration resource<br />

file. This includes making sure that the types of the properties in the bean match those<br />

configured for the bean in the configuration file.<br />

<strong>The</strong> managed-bean-scope element defines the scope in which the bean will be stored. <strong>The</strong> four<br />

acceptable scopes are none, request, session,orapplication. If you define the bean with a<br />

none scope, the bean is instantiated anew each time it is referenced, and so it does not get saved<br />

in any scope. One reason to use a scope of none is that a managed bean references another<br />

managed bean. <strong>The</strong> second bean should be in none scope if it is supposed to be created only<br />

when it is referenced. See “Initializing Managed Bean Properties” on page 443 for an example of<br />

initializing a managed bean property.<br />

If you are configuring a backing bean that is referenced by a component tag’s binding attribute,<br />

you should define the bean with a request scope. If you placed the bean in session or application<br />

scope instead, the bean would need to take precautions to ensure thread safety because<br />

UIComponent instances depend on running inside of a single thread.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!