我需要构建一个包含问题的测验应用程序(每个问题有2个可能的答案)我在数据库中存储了一些问题。选择答案并单击“下一步”后,您将看到下一个问题。 我有一个代表每个问题的bean,它叫做Item,并且具有以下属性: 问题,ansA,ansB; 我有一个豆子:
@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>
问题是,当我运行项目时,它不会显示单选按钮,问题或答案。
谢谢!!
答案 0 :(得分: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>
你可能想要添加一个“评估”按钮,当你没有任何问题或者某些东西时你会显示它(因为你需要做某些问题列表)。< / p>