1

質問を含むクイズのアプリを作成する必要があります (各質問には 2 つの回答があります)。いくつかの質問をデータベースに保存しました。回答を選択して [次へ] をクリックすると、次の問題が表示されます。各質問を表す Bean があります。これは Item と呼ばれ、次の属性があります。そして私は豆を持っています:

@ManagedBean
@ViewScoped
public class Bean {

private List<Item> items = new ArrayList<Item>();
private List<Item> helper = new ArrayList<Item>();
private String myValue;
int indexHelp = 0;

public String next() {
    items.clear();
    items.add(helper.get(indexHelp));
    indexHelp++;

    System.out.println("chose: " + myValue);

    return "ok";
}

リストヘルパーは、データベースからの質問とともに既に保存されています

jsf ファイル:

<h:form>
        <h:dataTable value="#{bean.items}" var="item">
            <h:selectOneRadio value="#{bean.myValue}">
                <h:column>
                    <f:selectItem itemLabel="#{item.question}"      />
                </h:column>
                <h:column>
                    <f:selectItem itemValue="1" itemLabel="#{item.ansA}" />
                </h:column>
                <h:column>
                    <f:selectItem itemValue="2" itemLabel="#{item.ansB}" />
                </h:column>

            </h:selectOneRadio>
        </h:dataTable>
        <h:commandButton value="Next" action="#{bean.next}" />
    </h:form>

問題は、プロジェクトを実行すると、ラジオ ボタン、質問、または回答が表示されないことです。ありがとうございました!!

4

1 に答える 1

0

あなたの説明に一致するものは次のとおりです。

public class Item {
     String question;
     String chosen;
     String ansA;
     String ansB;
}

@ManagedBean
@ViewScoped
public AnswerBean implements Serializable {
     private List<Item> alreadyAnswered = new ArrayList<>();
     private Item nextQuestion;

     @PostConstruct
     public void retrieveNextQuestion() {
         // pull "next" question from DB and assign it to nextQuestion
     }

     public void save() {
         alreadyAnswered.add(nextQuestion);
         retrieveNextQuestion();
     }
}

JSF ページは次のようになります。

<h:form>
    <h:outputText value="#{answerBean.nextQuestion.question}" />
    <h:selectOneRadio value="#{answerBean.nextQuestion.chosen}">
        <f:selectItem itemValue="1" itemLabel"#{answerBean.nextQuestion.ansA}" />
        <f:selectItem itemValue="2" itemLabel"#{answerBean.nextQuestion.ansB}" />
    </h:selectOneRadio>
    <h:commandButton action="#{answerBean.save}" value="Next" />
</h:form>

おそらく、質問が残っていない場合などに表示される「評価」ボタンを追加することをお勧めします (質問のリストで何かをする必要があるため)。alreadyAnswered

于 2014-01-14T13:19:10.060 に答える