background image

Developing the Beans

<< Configuring Error Messages | User Interface Component Model >>
<< Configuring Error Messages | User Interface Component Model >>

Developing the Beans

Developing the Beans
Developing beans is one responsibility of the application developer. A typical JavaServer Faces
application couples a backing bean with each page in the application. The backing bean defines
properties and methods that are associated with the UI components used on the page.
The page author binds a component's value to a bean property using the component tag's value
attribute to refer to the property. Recall that the userNo component on the greeting.jsp page
references the userNumber property of UserNumberBean:
<h:inputText id=
"userNo" label="User Number"
value=
"#{UserNumberBean.userNumber}">
...
</h:inputText>
Here is the userNumber backing bean property that maps to the data for the userNo component:
Integer userNumber = null;
...
public void setUserNumber(Integer user_number) {
userNumber = user_number;
}
public Integer getUserNumber() {
return userNumber;
}
public String getResponse() {
if(userNumber != null &&
userNumber.compareTo(randomInt) == 0) {
return
"Yay! You got it!";
} else {
return
"Sorry, "+userNumber+" is incorrect.";
}
}
See
"Backing Beans" on page 309
for more information on creating backing beans.
Adding Managed Bean Declarations
After developing the backing beans to be used in the application, you need to configure them in
the application configuration resource file so that the JavaServer Faces implementation can
automatically create new instances of the beans whenever they are needed.
The task of adding managed bean declarations to the application configuration resource file is
the application architect's responsibility. Here is a managed bean declaration for
UserNumberBean
:
A Simple JavaServer Faces Application
The Java EE 5 Tutorial · September 2007
298