7. Adding Other Web Components [Optional]

In this step, you will import the Managed Bean ( LoginBean ) and view ( login.jsp ) to the web application. You will also define navigation rules for the login.jsp page and run the application.

7.1. Importing managed bean and view

  1. Copy the LoginBean.java file from the folder workshop-jsf-tutorial/web/Resources to the beans package.
  2. Open and review the code of the LoginBean class. The validate( ) method verifies the userId and password of the user and returns the logical outcome login to navigate to the home.jsp page. If, the userId or password is incorrect, it returns null to request re-login and shows the error message.
  3. Configure LoginBean as Managed Bean in workshop-jsf-tutorial/web/WEB-INF/config/faces-config.xml file, as shown in the image below. (Open faces-config.xml and click the Faces Config tab in the editor, if necessary. )
  4. The scope of LoginBean is request. Save the faces-config.xml file.
  5. Add the following key-value pairs to the workshop-jsf-tutorial/web/WEB-INF/src/java/resources/application.properties file. You can also copy it from the file workshop-jsf-tutorial/web/Typing_Aid/application_properties_3.txt
  6. # -- login page --
    loginPageTitle=Login to Workshop...
    loginFormHeader=Member Login
    login_NewUser=New User
    login_SignUp=Sign Up
    invalidPassword=Please provide correct password for userId: {0}
    invalidUserId=Please provide correct userId
  7. Save the application.properties file.
  8. Copy the login.jsp.txt file from the workshop-jsf-tutorial/web/Resources folder to the pages folder and rename it to login.jsp.

7.2. Defining the Navigation Rule for login.jsp

Note that the LoginBean.validate method returns login to navigate to home.jsp page.

  1. Using the Navigation view of faces-config.xml file define the navigation rule for the login.jsp page.
  2. Define new navigation case for the navigation rule /pages/login.jsp page, as shown in the images below.

  3. Save the faces-config.xml file.

7.3. Running the application

  1. Right click on the login.jsp file in the AppXplorer view. Select Run As > Run on Server.
  2. If necessary, in the web browser enter the URL http://localhost:7001/pages/login.jsf.
  3. Click the Submit link and login with the userId james.
  4. Try logging in without a password and observe the error message.
  5. Login with invalid password value 1006 for userId james and observe the error message.
  6. When you login with the correct userId and password (james / 1007), you will be navigated to home.jsp page.
  7. Stop the server.

Click one of the following arrows to navigate through the tutorial:


Still need help? Post a question on the Workshop newsgroup.