Purpose: To use managed beans in JSF applications. In the previous section, we used the JSF component to create a simple web application. However, our web applications cannot do anything meaningful. To enrich the functions of our JSF application, we can establish association between the backing bean and the UI component. The auxiliary bean, also known as the JSF management bean, is a common JavaBean Component, but its attributes and methods can be accessed by the JSF component. In our example, we will create a userbean management bean with two attributes: name and birthday. 1. Right-click the project, select new, and then select File/folder. In the Web category, select the JSF managed bean template and click Next. 2. Name the bean Userbean , Put in Astrologer. User Other settings and default values do not need to be modified, and then click Finish . IDE Will be opened in the source code editing area Userbean. Java , And Faces-config.xml Add the following Bean Statement:
<managed-bean>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>astrologer.user.UserBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
3. Add the following domain declaration in userbean. java.
public class UserBean {
String name;
String birthday;
4. Generate the getter and setter methods for the domain. Right-click anywhere in the file and select encapsulate fields in refactor. Select next in the dialog box and refactor it in the refactoring window. IDE changes the access control of the domain to private, and then creates the getter and setter methods. 5. In Greeting. jsp (In bold ). Note: Userbeans. Java You can use the code completor.
<f:view>
<p>Enter your name:
<p>Enter your birthday:
</f:view>
6. In
Success. jsp
Add
JSF
Tag library declaration, which can be viewed from
Greeting. jsp
.7. Click the JSF form button in the control window and drag it to the H1 mark in the original file editor.
Success. jsp
Add an empty
JSF
Form.8. Modify success. jsp as follows:
<f:view>
<p>You've successfully registered with jAstrologer.</p>
<p>Your name is
<p>Your birthday is
</f:view>
9. Right-click the project and select Run project. After the project is deployed, you will see the same welcome page. Enter the value and click Submit, success. JSP will display the value you entered, as shown in: for auxiliary beans, refer to chapter 198 auxiliary bean book on page 1: Java ee 5 practical tutorial