2011-11-12 36 views
-1

我創建了一個簡單的JSF應用程序,呈現給用戶一個問題,和一組可能的答案單選按鈕。JSF QUIZZ應用程序導航

當用戶選擇的答案並提交,我的豆更新的問題,並返回相同的頁面,以顯示新的問題。如果達到最後一個問題,返回結果頁面。

這是工作正常,但是當用戶點擊瀏覽器的後退按鈕,並重新提交表單的問題發生......這增加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> 

Bean方法稱爲當用戶點擊'下一個問題'時:

public String processAnswer() 
{   
    Question q = questions.get(currentQuestion); 

    currentQuestion++; 
    userAnswers.add(currentAnswer); 

    if (currentQuestion == questions.size()) 
    { 
     this.processResults(); 
     return "finish"; 
    } 
    else 
    { 
     return "index";    
    } 
} 

我該如何解決這個問題?

謝謝!

+0

什麼是您所使用的代碼?你用f:ajax做了什麼?你的邏輯如何破壞? – Deco

+0

你能發表一些代碼嗎? – Robin

+0

只是爲了更清楚:應用程序工作正常,問題是,如果用戶再次點擊瀏覽器的後退按鈕並重新回答(重新提交),同樣的問題會發生什麼......爲了解決這個問題,我試圖F:阿賈克斯加載新的問題,和它的作品,但後來我不知道如何來回報「finish'page ...謝謝! – Fernando

回答

1

儘管我不知道很多關於你的邏輯,我會嘗試這樣的事情選項(考慮接收與處理的所有接聽電話的posibility):

public String processAnswer() 
{   
    String outcome = null; 
    if (currentQuestion <= questions.size()) { 
     Question q = questions.get(currentQuestion); 

     currentQuestion++; 
     userAnswers.add(currentAnswer); 
     if (currentQuestion < questions.size()) { 
      outcome = "index"; 
     } else { 
      this.processResults(); 
      outcome = "finish"; 
     } 
    } else { 
     outcome = "finish"; 
    } 
    return outcome; 
} 

但它不」 t創建一個可靠的代碼,因爲代碼在使用後退按鈕後會忽略所有提交。

我真的在ah中有當前的問題值:inputHidden,所以如果你使用後退按鈕,你提交你將提交currentquestion的值,並且值將匹配用戶回答的問題,所以你將能夠更新答案,並儘管已經使用了後退按鈕n次,仍然繼續下一個問題。

+0

謝謝,我會試試。但理想的情況是絕不允許用戶回去。 – Fernando