站内搜索: 请输入搜索关键词
当前页面: 图书首页 > JavaServer Pages, Second Edition

JavaServer Pages, Second Edition

[ directory ] Previous Section Next Section

5.5 The Quiz Result Page

Chapter 3 introduced the daily quiz that Java News Today will use to liven up its site. The quiz consists of a serialized bean that holds the questions and correct answer, along with a form from which the user can guess, as shown in Listing 3.13. At that point, there was no way to check whether the user was correct, but that's easily remedied now that we have the standard tag library at our disposal. The quiz result page, shown in Listing 5.6, it is very straightforward.

Listing 5.6 The quiz result page
<%@ taglib prefix="c"
    uri="http://java.sun.com/jstl/core" %>

<jsp:useBean
  id="quiz"
  beanName="todaysQuiz3"
  type="com.awl.jspbook.ch03.QuizBean"/>

<jsp:setProperty name="quiz" property="*"/>

<jsp:include page="top.jsp" flush="true">
  <jsp:param name="title" value="Quiz result"/>
</jsp:include>

<c:choose>
  <c:when test="${quiz.userGuess == quiz.correctAnswer}">
    That's right!<p>
  </c:when>
  <c:otherwise>
    Sorry, that's incorrect; the right answer is
    <c:out value="${quiz.correctAnswer}"/><p>
  </c:otherwise>
</c:choose>

<jsp:include page="bottom.jsp" flush="true"/>

This is another example of setting bean properties from a form and then checking a condition with a c:choose tag.

    [ directory ] Previous Section Next Section