A Trivia Quiz — 20 points

The trivia quiz web application lets the user play a trivia game with questions from several categories. As the game is played, results from the previous question and a running score are displayed. After the user finishes a quiz from one category she is given an opportunity to try another category.

The requirements for this web application are described in terms of the JSF pages, the navigation, and its use of string management. You have some latitude in the definition of the application beans.

The trivia quiz application should use four JSF pages:

Display for index.xhtml

The JSF page index.xhtml presents a choice of quiz categories. It displays

Display for first.xhtml

The JSF page first.xhtml presents the first question for a quiz. It displays

After Correct Response After Incorrect Response
Display of rest.xhtml after a correct answer Display of rest.xhtml after an incorrect answer

The JSF page rest.xhtml presents all quiz questions after the first one. It displays

After the user has entered an incorrect answer the results from the previous question should include the correct answer. The results text can be generated using a conditional value expression.

A conditional value expression can specify text that appears in a JSF page or it can specify the the value of a tag attribute. It has the following form.

#{condition ? true-value : false-value}

The condition should be a boolean bean property. The true-value and false-value should be strings. They need not be string literals. They can be specified using any kinds of value expression.

After Correct Response After Incorrect Response
Display of done.xhtml after a correct answer Display of done.xhtml after an incorrect answer

The JSF page done.xhtml is displayed after the user has answered the last question. It displays

After the user has entered an incorrect answer the results from the previous question should include the correct answer.

This web application needs a JSF bean to represent the current state of the quiz, a JSF bean to stand in for a future database, and objects for representing quiz problems . The CoreJSF numberquiz example has QuizBean and ProblemBean objects that serve similar functions as the quiz bean and problem objects for this assignment.

The quiz bean needs properties to keep track of any quiz information displayed in the JSF pages other than index.xhtml.

Some of this information can be obtained indirectly through other application beans.

In addition to properties, the quiz bean needs to provide two action methods that are invoked from the category selection command buttons in index.xhtml. As in the CoreJSF numberquiz example, the problems for the quizzes can be defined in an array in the class for the quiz bean. When the bean is created it needs to process the problems into a java.util.Map of categories to support lookup by the action method. You may want to design supporting classes for doing this.

Problem objects are not managed directly by the JSF framework. Instead they are accessed indirectly through the quiz bean. Each should have

The Problem class constructor should have the following form:

  public Problem(String categoryName, String question,
      String[] choices, String correctChoice) {
      // code to initialize properties

The database bean is application scoped. It is stand-in for a future database that will hold all information about quiz categories and problems. Its starting code is provided for you here. You will have to adjust its package name and add some quiz problems. The comments in the file indicate how these changes should be made.

The database bean has a method getProblems(String categoryName) for retrieving the problems for a category. You will need to use this method in this assignment.

The database bean also has a method getCategoryNames() that returns a list of category names. It will be needed in a later enhancement of the trivia quiz.

An action method is a bean method that is usually invoked from a command button in a JSF page. It returns a string which is used as an outcome to direct the navigation to the next page. An action method is needed when either or both of the following are true.

For this assignment the action method is needed for the second reason.

Use in a JSF Page

A JSF page that uses action methods typically contains a tag like the following.

<h:commandButton value="button-label" action="bean-name.method-name(argument-list)"/>

If literal string arguments are used they must be enclosed in single quotes.

Bean Method Declaration

The appropriate bean class for bean-name should contain the following declaration.

public String method-name(parameter-declarations) {
  return "outcome";

The method-name in this declaration should be identical to the method-name in the JSF page. The outcome should be the name of the next JSF page. The ".xhtml" suffix can be omitted.

For some web applications the following variations can be useful.

All strings displayed in the JSF Pages should be obtained from a resource bundle except for the category names and the quiz questions and their answers.

When you have completed the assignment submit it as directed by your TA.