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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A Simple <strong>Java</strong>Server Faces Application<br />

296<br />

Developing the Beans<br />

Developing beans is one responsibility of the application developer. A typical <strong>Java</strong>Server Faces<br />

application couples a backing bean with each page in the application. <strong>The</strong> backing bean defines<br />

properties and methods that are associated with the UI components used on the page.<br />

<strong>The</strong> page author binds a component’s value to a bean property using the component tag’s value<br />

attribute to refer to the property. Recall that the userNo component on the greeting.jsp page<br />

references the userNumber property of UserNumberBean:<br />

<br />

...<br />

<br />

Here is the userNumber backing bean property that maps to the data for the userNo component:<br />

Integer userNumber = null;<br />

...<br />

public void setUserNumber(Integer user_number) {<br />

userNumber = user_number;<br />

}<br />

public Integer getUserNumber() {<br />

return userNumber;<br />

}<br />

public String getResponse() {<br />

if(userNumber != null &&<br />

userNumber.compareTo(randomInt) == 0) {<br />

return "Yay! You got it!";<br />

} else {<br />

return "Sorry, "+userNumber+" is incorrect.";<br />

}<br />

}<br />

See “Backing Beans” on page 307 for more information on creating backing beans.<br />

Adding Managed Bean Declarations<br />

After developing the backing beans to be used in the application, you need to configure them in<br />

the application configuration resource file so that the <strong>Java</strong>Server Faces implementation can<br />

automatically create new instances of the beans whenever they are needed.<br />

<strong>The</strong> task of adding managed bean declarations to the application configuration resource file is<br />

the application architect’s responsibility. Here is a managed bean declaration for<br />

UserNumberBean:<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!