2011-11-12 11 views
-1

私は単純なJSFアプリケーションを作成しました。これはユーザーのための質問と、可能な回答のセットをラジオボタンとして提示します。JSF quizzアプリケーションのナビゲーション

ユーザーが回答を選択して送信すると、Beanは質問を更新して同じページを返し、新しい質問が表示されます。最後の質問に達すると、結果とともに終了ページが返されます。

これはうまくいきますが、ユーザーがブラウザの戻るボタンをクリックしてフォームを再送信すると問題が発生します。これはbean.currentQuestionを増やし、ロジックを破壊します。

//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> 

ビーン方法...というページフリップのない質問を更新しアヤックスが、今はフィニッシュページを提示する方法を知らない:

私はFを試してみましたユーザーが次の質問にヒットしたとき:

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:ajaxに新しい質問を読み込み、それは動作しますが、私は 'finish'ページを返す方法を知らない...ありがとう! – Fernando

答えて

1

、私は、このオプションのようなものを試してみます(アカウントに加工し、すべての答えとコール受信働か取る):

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; 
} 

をしかし、それはdoesnの戻るボタンを使用した後にコードがすべての送信を無視するため、ソリッドコードを作成しないでください。

私は本当にcurrenthestionの値をah:inputHiddenにしています。そのため、戻るボタンを使用して送信するとcurrentquestionの値を送信し、その値がユーザーの回答と一致します戻るボタンをn回使用したにもかかわらず、回答を更新して次の質問に進むことができます。

+0

ありがとう、私はそれを試してみましょう。しかし、理想的な状況は、ユーザーが戻ってくることを決して許さないことです。 – Fernando

関連する問題