我正在使用继承的 JSF 应用程序。对它还是很陌生。
这是我正在使用的页面的屏幕截图:http: //goo.gl/GidpCD。这是一些考试软件,您正在查看的是问题管理。在此屏幕上,用户可以编写问题、引用问题来源、编写答案、关联到类别和关联图像。
当我向底部添加/删除答案时,我遇到了困难。
我怀疑我的问题出在 ViewState 上。
当我单击添加或删除答案时。页面重新加载,我丢失了 JSF dataTable 组件中的文件关联。ViewState 维护在页面上的所有其他表单元素中,只是该 dataTable 中的那些文件,我似乎无法理解。这是 JSF dataTable 组件(我将在下面完整列出代码):
<h:dataTable id="table0" value="#{adminQuestions.filesList}" var="file" columnClasses="id,title,selected last" rendered="true" >
<h:column>
<f:facet name="header">File</f:facet>
<h:graphicImage value="#{file.path2}" alt="" />
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
<h:outputText value="#{file.title}" />
</h:column>
<h:column>
<f:facet name="header">Select files</f:facet>
<h:selectBooleanCheckbox value="#{file.selected}" />
</h:column>
</h:dataTable>
这是触发添加添加答案方法的 JSF 按钮组件:
<h:commandButton value="#{msgs.addButton}" action="#{adminQuestions.addQuestionToAnswerRow}" />
这是那个add answer
方法:
public String addQuestionToAnswerRow() {
if(this.questionToAnswerList.size() < 8) {
// set the scroll
if(FacesContext.getCurrentInstance().getMessageList().size() > 0) {
this.scroll = false;
}
else {
this.scroll = true;
}
//Create a new answer, set the current question, and add it to the collection
Answers answer_local = new Answers();
QuestionToAnswer qta_local = new QuestionToAnswer();
answer_local.setTitle(this.inputTextAnswer);
answer_local.setDescription(this.inputTextAnswer);
answer_local.setCorrect(this.correctAnswer);
qta_local.setAnswers(answer_local);
qta_local.setAnswer(this.questionToAnswerList.size() + 1);
qta_local.setQuestions(this.question);
this.questionToAnswerList.add(qta_local);
// reset the input fields
this.inputTextAnswer = "";
this.correctAnswer = false;
List<File1> thisFilesList = getFilesList();
QuestionsToFiles qtf;
List<QuestionsToFiles> qtfc = new ArrayList<QuestionsToFiles>();
if(this.filesList != null) {
for(Iterator<File1> entries = this.filesList.iterator(); entries.hasNext();) {
File1 file_temp = entries.next();
if(file_temp.isSelected()) {
qtf = new QuestionsToFiles();
qtf.setQuestions(this.question);
qtf.setFile1(file_temp);
qtfc.add(qtf);
}
}
}
return null;
}
else {
JSFUtils.addErrorMessage("No more than eight answers are allowed: ", "The question cannot have more than eight answers");
return null;
}
}
这是触发删除方法的 JSF 按钮组件
<h:commandButton value="#{msgs.deleteButton}" action="#{adminQuestions.deleteQuestionToAnswerRow(a)}" />
和删除方法:
public String deleteQuestionToAnswerRow(QuestionToAnswer qta_local) {
// set the scroll
if(FacesContext.getCurrentInstance().getMessageList().size() > 0) {
this.scroll = false;
}
else {
this.scroll = true;
}
//Iterate through the category exam collection and delete the associated category so it's reflected on the page
for (Iterator<QuestionToAnswer> itr = this.questionToAnswerList.iterator(); itr.hasNext();) {
QuestionToAnswer qta_temp = itr.next();
if(qta_temp == qta_local) {
qta_temp.setQuestions(null);
qta_temp.setAnswers(null);
itr.remove();
return null;
}
}
return null;
}
谢谢参观。