Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF quizz application navigation
    text
    copied!<p>I created a simple JSF app, that presents a question for the user, and a set of possible answers as radio buttons. </p> <p>When the user chooses the answer and submit, my bean updates the question and returns the same page, so that the new question is displayed. If the last question is reached, a finish page is returned with the results.</p> <p>This is working fine, but the problem happens when the user clicks the browser back button and re-submits the form...this increments bean.currentQuestion and breaks my logic.</p> <p>I tried f:ajax to update the question without page flip, but now i dont know how to present the finish page...</p> <p>//index.xhtml</p> <pre><code> &lt;h:form&gt; &lt;div class="questionNumberDiv" id ="questionNumberDiv"&gt; &lt;h:outputLabel value="Question #{test.currentQuestion + 1}" for="questionLabel"/&gt; &lt;/div&gt; &lt;br/&gt; &lt;div class="questionDiv" id="questionDiv"&gt; &lt;h:outputLabel id ="questionLabel" value="#{test.questions[test.currentQuestion].question}"/&gt; &lt;/div&gt; &lt;br/&gt; &lt;div class="questionDiv" id="possibleAnswersDiv"&gt; &lt;h:selectOneRadio requiredMessage="Please, select one answer!" id="radio" layout="pageDirection" value="#{test.currentAnswer}" required="true"&gt; &lt;f:selectItems value="#{test.questions[test.currentQuestion].possibleAnswers}" var="y" itemLabel="#{y}" itemValue="#{y}" /&gt; &lt;/h:selectOneRadio&gt; &lt;/div&gt; &lt;br/&gt; &lt;h:panelGrid columns="2" styleClass="requiredMessage"&gt; &lt;h:commandButton value="next question" action ="#{test.processAnswer}" /&gt; &lt;h:message for="radio"/&gt; &lt;/h:panelGrid&gt; &lt;/h:form&gt; &lt;/h:body&gt; </code></pre> <p>Bean method called when user hits 'next Question':</p> <pre><code>public String processAnswer() { Question q = questions.get(currentQuestion); currentQuestion++; userAnswers.add(currentAnswer); if (currentQuestion == questions.size()) { this.processResults(); return "finish"; } else { return "index"; } } </code></pre> <p>How can i solve this ? </p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload