Я создал простое приложение JSF, которое представляет вопрос пользователю и набор возможных ответов в виде переключателей.
Когда пользователь выбирает ответ и отправляет запрос, мой компонент обновляет вопрос и возвращает ту же страницу, так что отображается новый вопрос. Если последний вопрос достигнут, возвращается конечная страница с результатами.
Это работает нормально, но проблема возникает, когда пользователь нажимает кнопку браузера назад и повторно отправляет форму ... это увеличивает bean.currentQuestion и нарушает мою логику.
Я попытался f: ajax обновить вопрос без переворачивания страницы, но теперь я не знаю, как представить окончательную страницу ...
// index.xhtml
<h:form>
<div class="questionNumberDiv" id ="questionNumberDiv">
<h:outputLabel value="Question #{test.currentQuestion + 1}" for="questionLabel"/>
</div>
<br/>
<div class="questionDiv" id="questionDiv">
<h:outputLabel id ="questionLabel" value="#{test.questions[test.currentQuestion].question}"/>
</div>
<br/>
<div class="questionDiv" id="possibleAnswersDiv">
<h:selectOneRadio requiredMessage="Please, select one answer!" id="radio" layout="pageDirection" value="#{test.currentAnswer}" required="true">
<f:selectItems value="#{test.questions[test.currentQuestion].possibleAnswers}" var="y"
itemLabel="#{y}" itemValue="#{y}" />
</h:selectOneRadio>
</div>
<br/>
<h:panelGrid columns="2" styleClass="requiredMessage">
<h:commandButton value="next question" action ="#{test.processAnswer}" />
<h:message for="radio"/>
</h:panelGrid>
</h:form>
</h:body>
Метод бина вызывается, когда пользователь нажимает «следующий вопрос»:
public String processAnswer()
{
Question q = questions.get(currentQuestion);
currentQuestion++;
userAnswers.add(currentAnswer);
if (currentQuestion == questions.size())
{
this.processResults();
return "finish";
}
else
{
return "index";
}
}
Как я могу решить это?
Спасибо!