我正在使用 primefaces 3.5、Glassfish 3.1.2 和 Java EE。我正在尝试创建一个在表格中创建“问题”的页面。我有一个数据表,其中包含供用户在尝试回答问题时选择的“答案”。
作为一项多项选择琐事测验,他们需要有一个或多个答案是正确的,其余的都是正确的。我允许每个问题有 2-6 个答案,并且最多有 4 个正确。所以我有 ap:dataTable 元素来保存答案,我有 3 个命令按钮来添加、删除或编辑表中的答案,每个按钮都有一个在单击时运行的服务器端方法。但是,没有任何方法运行。没有错误也没有异常,只是没有任何作用。
这是我的页面(我将整个页面包括在内,以防万一我在其他地方出现错误):
<h:form id="createQForm">
<h1>Create A Question</h1>
<p:messages id="msgs"/>
<p:selectOneMenu
value="#{createQuestionBean.selectedCategory}"
id="lstCategory" required="true" >
<f:selectItems
value="#{createQuestionBean.categories}" />
<p:ajax listener="#{createQuestionBean.onSelectCategory}"
update="lstSubCategory" />
</p:selectOneMenu>
<p:selectOneMenu
value="#{createQuestionBean.selectedSubCategory}"
id="lstSubCategory" required="false">
<f:selectItem itemLabel="No Sub-Category"
noSelectionOption="true" />
<f:selectItems
value="#{createQuestionBean.subCategories}"/>
</p:selectOneMenu>
<h:panelGrid columns="3">
<h:outputLabel for="txtQuestion" id="lblQuestion" value="Question: *" />
<p:inputText value="#{createQuestionBean.entry.question}"
id="txtQuestion" required="true"
requiredMessage="You must provide a question"
validatorMessage="#{createQuestionBean.QUESTION_VALIDATION_MESSAGE}">
<f:validateRegex pattern=".*[\s\d\w]" />
<p:ajax update="msgQuestion" event="keyup" />
</p:inputText>
<p:message for="txtQuestion" id="msgQuestion" display="icon"/>
</h:panelGrid>
<h:outputText value="Horizontal: " />
<p:selectManyCheckbox value="#{createQuestionBean.selectedOptions}">
<f:selectItem itemLabel="Randomizable?" itemValue="randomizable" />
<f:selectItem itemLabel="Multiple Choice?" itemValue="multichoice" />
</p:selectManyCheckbox>
<p:fileUpload
value="#{createQuestionBean.questionImage}" mode="simple"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
label="Select Images (jpg or png)"
sizeLimit="2097152"
invalidFileMessage="Sorry, only files of type .jpg, .png or .gif are allowed"
invalidSizeMessage="Sorry, files are limited in size to 2 Mb"/>
<p:dataTable
id="answersTable" var="answer" style="width: 250px"
value="#{createQuestionBean.datamodel}"
selection="#{createQuestionBean.selectedAnswer}"
editMode="cell" editable="true"
selectionMode="single">
<p:column style="width:75%">
<p:cellEditor >
<f:facet name="output">
<h:outputText
value="#{answer.answer.testAnswer}" />
</f:facet>
<f:facet name="input">
<p:inputText
id="modelInput"
style="width: 97%;height: 97%"
value="#{answer.answer.testAnswer}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:25%">
#{answer.correct ? "Correct" : "Wrong" }
</p:column>
</p:dataTable>
<h:panelGrid columns="3">
<p:commandButton
update="answersTable" ajax="true"
actionListener="#{createQuestionBean.removeSelect}"
value="Remove Selected -"/>
<p:commandButton
update="answersTable" ajax="true"
actionListener="#{createQuestionBean.setAsCorrect}"
value="Set Selected As Right"/>
<p:commandButton
update="answersTable" ajax="true"
value="Add +"
actionListener="#{createQuestionBean.addAnswer}"/>
</h:panelGrid>
<p:commandButton
action="#{createQuestionBean.saveAndStoreEntry()}" />
</h:form>
以下是调用的方法:
public void addAnswer() {
System.out.println("***** CALLED ADD ANSWER");
answers.add(new QuestionAnswers());
answers.get(answers.size() - 1).setCorrect(false);
setIDs();
//set the data models
datamodel = new QuestionAnswersDataModel(new ArrayList<QuestionAnswers>(answers));
}
public void removeSelect() {
System.out.println("***** CALLED REMOVE SELECT");
if (selectedAnswer == null && !answers.contains(selectedAnswer)) {
FacesUtils.addErrorMessage(NO_SELECTION_ERROR);
} else {
answers.remove(selectedAnswer);
}
setIDs();
//set the data models
datamodel = new QuestionAnswersDataModel(new ArrayList<QuestionAnswers>(answers));
}
public void setAsCorrect() {
System.out.println("***** CALLED SET AS CORRECT");
if (selectedAnswer != null) {
selectedAnswer.setCorrect(true);
if (!entry.getMultichoice()) {
for (QuestionAnswers questionAnswers : answers) {
if (questionAnswers != selectedAnswer) {
questionAnswers.setCorrect(false);
}
}
}
} else {
FacesUtils.addErrorMessage(NO_SELECTION_ERROR);
}
setIDs();
//set the data models
datamodel = new QuestionAnswersDataModel(new ArrayList<QuestionAnswers>(answers));
}
我在每个方法中添加了断点,以查看它是否被调用,以及系统输出。
我究竟做错了什么?我虽然可能是因为我在方法调用中没有 AjaxBehaviourEvent,但这并没有改变。