我目前正在使用JSF和PrimeFaces用Java8开发一个Web应用程序。我已经成功使用ui:repeat打印了String的第一个列表,但是我无法检索用户的答案(我想在豆FormulaireComplete的列表响应中添加该答案)。
我必须承认我不知道如何在a.xhtml中初始化对象列表。它可能来自这里,但我真的不确定。我在stackoverflow上找到了一些示例,但是该列表已经创建了,并且元素数量很多。
将包含响应的FormulaulaComplete类(我没有在此处放置getter和setter,但它们当然存在于代码中):
@Named
@RequestScoped
public class FormulaireComplete implements Serializable {
private List<Reponse> reponses;
public FormulaireComplete() {
super();
reponses = new ArrayList<>();
}
//validate form
public String complete() {
ReponsesDAO.create(reponses);
return "../admin.xhtml?faces-redirect=true";
}
}
<div id="postFormulaire" class="ui-g">
<ui:repeat id="repeater" value="#{utilisateur.questionnaire.questions}" var="question" class="ui-g-12">
<div class="ui-g-12"><b>#{question.titreQuestion}</b> #{question.question}</div>
<div class="ui-g-1"/>
<p:inputTextarea class="ui-g-9" id="reponse" value="#{formulairecomplete.reponses.commentaire}" label="f"></p:inputTextarea>
<p:selectOneMenu id="onemenu" class="ui-g-1" style="max-height:45px" value="#{formulairecomplete.reponses.note}" >
<f:selectItem itemValue = "NA" itemLabel = "NA" />
<f:selectItem itemValue = "1" itemLabel = "1" />
<f:selectItem itemValue = "2" itemLabel = "2" />
<f:selectItem itemValue = "3" itemLabel = "3" />
<f:selectItem itemValue = "4" itemLabel = "4" />
<f:selectItem itemValue = "5" itemLabel = "5" />
<f:selectItem itemValue = "6" itemLabel = "6" />
<f:selectItem itemValue = "7" itemLabel = "7" />
<f:selectItem itemValue = "8" itemLabel = "8" />
<f:selectItem itemValue = "9" itemLabel = "9" />
<f:selectItem itemValue = "10" itemLabel = "10" />
</p:selectOneMenu>
<div class="ui-g-1"/>
<div class="ui-g-2"/><p:separator id="separator" class="ui-g-8" /><div class="ui-g-2"/>
</ui:repeat>
<div class="ui-g-5"/><h:commandButton id="validerForm" class="ui-g-2" value="Valider Formulaire" action="#{formulairecomplete.complete}"></h:commandButton><div class="ui-g-5"/>
</div>
答案 0 :(得分:2)
您没有数据结构来将问题答案映射到您的问题。我建议创建一种将问题和答案包装在一起的新类型。
用于关联问题和答案的数据结构:
public class Response {
private Question question;
private Answer answer;
}
托管bean,它分别加载问题和答案并将它们映射到这种新的数据类型中:
@Named
@RequestScoped
public class FormulaireComplete implements Serializable {
private List<Response> responses;
public FormulaireComplete() {
super();
reponses = new ArrayList<>();
}
@PostConstruct
init() {
List<Question> questions = loadQuestions();
List<Answer> answers = loadAnswers();
for(Question q: questions) {
for(Answer a: answers) {
if(q.id == a.id) {
this.responses.add(new Response(q, a));
}
}
}
}
//validate form
public String complete() {
ReponsesDAO.create(reponses);
return "../admin.xhtml?faces-redirect=true";
}
}
显示数据,并初始化答案:
<ui:repeat value="#{bean.responses}" var="response">
#{response.question.description}
<p:inputTextarea value="#{response.answer.input}"></p:inputTextarea>
<p:selectOneMenu value="#{response.answer.score}">
<f:selectItems ... />
</p:selectOneMenu>
</ui:repeat>