November 22, 2017
Hot Topics:

The First Time Ever I Saw Your Face: Getting Started with JavaServer Faces

  • March 31, 2008
  • By David Thurmond
  • Send Email »
  • More Articles »

Listing 4

<HEAD> <title>Hangman</title> </HEAD>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="com.dlt.developer.hangman.Messages"
<body bgcolor="white">

<h:form id="youLoseForm">

<h:graphicImage id="hangmanThumbnail"
                alt="Small image of hangman"/>


<h:outputFormat id="youLoseMessage"
<f:param value="#{GameBean.playerName}"/>
<f:param value="#{GameBean.answer}"/>


<h:commandLink id="newGame"



In Listings 3 and 4, you see another new tag, the <h:commandLink> tag. This tag is identical in function to the <h:commandButton> tag, but renders a link rather than a submit button on the form. This link is a bit different than the command buttons you saw before because the action attribute has a hard-coded value, "success", rather than linking directly to a business logic function. This is because the navigation from this page is handled automatically by the JSF framework. You'll see how this is done in the next section.

Determining Page Navigation

So, now that you have a user interface built, you can move on to the next development step: defining page navigation.

You should revisit the flow of your word game application. First, you begin by starting a new game on the newGame.jsp page. When the user clicks the Start Game button, you move on to the showPuzzle.jsp page, and remain there until you have either solved the puzzle or guessed too many times. From the showPuzzle.jsp page, you can 1) go back to the showPuzzle.jsp page for another guess, 2) solve the puzzle correctly and go to the youWin.jsp page, or 3) guess too many times and go to the youLose.jsp page. From both youWin.jsp and youLose.jsp, you can start a new game by going back to newGame.jsp, at which point the cycle begins again.

To define how navigation is performed in the application, you need to define some navigation rules in the faces-config.xml file. The rule for the newGame.jsp page is shown below:

      Send the user to the puzzle page after the new game
      is set up
         Go to the puzzle page if everything on the new game
         page validated OK

The <from-view-id> tag tells JSF which page you are defining possible page navigations for. Then, you define the possible outcomes for that page by using the <navigation-case> tags. For newGame.jsp, the only outcome is "success" from the user clicking the "Start Game" button on the form. When this happens, you want to go to showPuzzle.jsp, so you indicate this by setting the <from-outcome> tag to "success" and the <to-page-view> tag to showPuzzle.jsp. This tells JSF that, after page validation is performed on newGame.jsp, you want to go to showPuzzle.jsp.

Now, look at the more complicated case of showPuzzle.jsp. Remember that here you have three possible outcomes, not just one, as shown below:

      Defines behavior once the user is on the puzzle page
         If the user is on the puzzle page and hasn't solved
         the puzzle, have them guess again
         If the user is on the puzzle page and solves
         the puzzle, go to the win page
         If the user is on the puzzle page and guessed too
         many times, go to the you lose page

Page 4 of 8

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date