The Java Bean–JSF Page Relationship and Page Navigation – Develop Java Web Applications to Access Databases

8.1.6.5.1   The Java Bean–JSF Page Relationship and Page Navigation

Table 8.1 lists all data access methods and action methods used in this example.

A Java bean can be connected to a JSF page by using the value attribute of a UI component represented by a JSF tag in that page. That is, a property or method defined in a Java bean class can be mapped to a value attribute of a UI component in a JSF page. This relationship can be triggered and setup when a submit button in the JSF page is clicked by the user and all form data sent to the Web server. Refer to Figure 8.30. The operational procedure of executing a request is:

1) The data access method LogInQuery() is defined in the Java bean class LogInBean and will be called by the action method LogInBean.LogInAction() defined in the JSF page LogIn.jsp when the user clicks the LogIn button. Since the action method LogInBean.LogInAction() has been bound to the LogIn command button, all form data, including the Username and Password entered by the user on the JSF page, will be submitted to the FacesServlet when the LogIn button is clicked by the user.

2) After the FacesServlet receives the form data, it will validate it and return the form back to the client if any error is encountered.

3) Otherwise, the validated form data, including the Username and Password, will be stored to the associated properties in the Java bean class. Then JSF engine will call the action method LogInBean.LogInAction() that has been bound to the LogIn button and in turn call the data access method LogInQuery() to perform the database-related query to find the matching login information.

FIGURE 8.30   The operational procedure of executing a request using JSF.

4) After a piece of matching login information has been found, the associated properties, userName and passWord, which are defined inside the Java bean class, will be updated by assigning the matching username and password to them. This update occurs on the Java bean side and will be immediately reflected to the value attributes of the Username and Password input Text fields in the JSF page, since they have been bound together. Therefore, the content of each input Text tag will also be updated.

5) The action method LogInAction() defined in the LogInBean class will also be called when the LogIn button is clicked by the user since it is bound to the LogIn button.

6) The data access method LogInQuery() will be executed to perform database-related queries and business logic.
7) Each action method returns a string called “outcome”. JSF uses a navigation handler to determine what it is supposed to do for each outcome string. If an action method returns a null, it means that the execution of that method encountered problems and the same page must be redisplayed. Otherwise, the desired next page should be displayed, depending on the returned outcome string. The default JSF navigation handler uses a set of navigation rules that are specified in the JSF application configuration file faces-config.xml, which is shown in Figure 8.31. In this example, if a piece of matching login information is found, the action method will return an outcome string “SELECTION” and the next page, Selection.jsp, should be displayed.

8) Otherwise, the query fails and no matching login user information can be found. The LogInAction() method returns a null to the JSF engine to redisplay the LogIn page.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024