Sun Java Solaris Communities My SDN Account
 
 
handle_command_submissions_part2

JavaServer Faces Best Practices: How to Handle Command Submission in JavaServer Faces Application - Part 2

By Doris Chen, Ph.D.
June 2006

Using Dynamic Action Mapping

The action mapping can also be defined dynamically by specifying an action method instead of specifying the outcome.  In part 2, we will continue from Part 1, and implement a login button which will only allow users to login with specified privileges.  We will use dynamic action mapping in this part 2.

    1. First we need to develop a login page using dynamic action mapping.  The code fragment is shown below:
  • Line 4 below shows how an action outcome can be dynamically binded to a login method invoked from the managed bean CommandSubmissionsBeanThe return outcome will be the result of login invocation.  If the outcome is success, it will go to the response.jsp page as defined in the navigation.  If the outcome is not success or null, it will not advance and the error message will be shown.
  • Line 5 below shows how to display the status of the login action by binding the value of authStatus from CommandSubmissionsBean managed bean.
  1.    <b>User Name:</b> <h:inputText id="userName" value="#{CommandSubmissionsBean.userName}"/> <br><br>
  2.    <b>Password:</b> <h:inputText id="password" value="#{CommandSubmissionsBean.password}"/> <br><br>

  3.    <h:commandButton id="submit" action="#{CommandSubmissionsBean.login}" value="Login" />
  4.    <b>Login Status: <h:outputText value="#{CommandSubmissionsBean.authStatus}"/></b>

    2. Next implement the login method in CommandSubmissionsBean.java.  The login method is implemented only for authorized user with user name admin and password adminadmin.  The authorization login status and type is also updated based on different result.  The code below illustrates the detail.
  1.     public String login() {
  2.         String outcome = getUserName().equals(getAllowedUserName()) && getPassword().equals(getAllowedPassword()) ? SUCCESS : FAILURE;
  3.         if (outcome.equals(SUCCESS)) {
  4.             authStatus = "Login Succeeded";
  5.             authType = AUTHORISED_USER;
  6.         } else {
  7.             authStatus = "Login Failed";
  8.             authType = GUEST_USER;
  9.         }
  10.         return outcome;
  11.     }

    3. Configure the configuration file faces-config.xml the same as described in Part 1.  If the returning outcome is success, the next page to display is response.jsp which displays the login successful message. 

    4.  Next is to build, deploy, and test the application.  We are using NetBeans 5.5 IDE to develop the application and deployed onto Sun Java System Application Server PE 9 (Java EE 5 compliant application server).  The complete NetBeans project and source code can be downloaded here.
  • The following is the result:

  • Enter username admin and password adminadmin and then click Login button. An success outcome will return and response.jsp page will show.
  • Enter any username somebody and password somebody and then click Login button.  The login action will fail, an failure outcome will return and the associated "Login Failed" error message will show.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.