我(最终)设法以我希望它的方式提出问题。最后一部分仍然缺失:问题所属的类别。我已经创建了一个问题处理程序类来管理问题的持久性等,我想知道是否应该将它与该类中的其他方法放在一起?
共有10个类别(只是ID和说明)。我认为我缺乏经验和知识,无法确切知道我应该如何组织以及什么属于一个支持bean。
public abstract class QuestionHandler {
@Inject
protected QuestionServiceBean questionBean;
@Inject
protected Question question;
protected List<Answer> answers;
protected List<Category> categories;
protected String correctAnswer;
public void updateQuestion() {
questionBean.updateQuestion(question);
}
public String persist() {
question.setAnswers(answers);
question.setCategories(categories);
questionBean.persistQuestion(question);
}
public void persistAsUserSubmitted() {
question.setAnswers(answers);
question.setCategories(categories);
questionBean.persistAsUserSubmitted(question);
}
protected void addAnswerAlternative() {
if (answers != null) {
answers.add(new Answer());
}
}
public abstract void init();
// Removed the getters/setters for readability.
}
这个类是扩展的并且实现了init方法,但这并不重要。
答案 0 :(得分:1)
如果逻辑与您的域相关,则CategoryServiceBean
是从中获取类别的正确位置。
答案 1 :(得分:0)
这看起来不对。您正在爆炸控制器中的模型属性。
protected Question question;
protected List<Answer> answers;
protected List<Category> categories;
public String persist() {
question.setAnswers(answers);
question.setCategories(categories);
questionBean.persistQuestion(question);
}
public void persistAsUserSubmitted() {
question.setAnswers(answers);
question.setCategories(categories);
questionBean.persistAsUserSubmitted(question);
}
人们会期待更多的是
protected Question question;
public String persist() {
questionBean.persistQuestion(question);
}
public void persistAsUserSubmitted() {
questionBean.persistAsUserSubmitted(question);
}
你只是在#{bean.question.answers}
和#{bean.question.categories}
而不是#{bean.answers}
和#{bean.categories}
中引用视图中的嵌套属性。
关于具体问题,我假设您正在讨论<f:selectItems>
中使用的“可用类别”等?是的,您可以将它放在一个单独的(应用程序作用域?)bean中。但如果它涉及“选定的类别”,只需按照之前的建议将其绑定到Question
#{bean.question.categories}
。 E.g。
<h:selectManyListbox value="#{bean.question.categories}" converter="#{categoryConverter}">
<f:selectItems value="#{data.categories}" />
</h:selectManyListbox>
请注意转换器是由EL引用的,因为我认为它是@ManagedBean
或@Named
,因为你想在其中使用@EJB
(基于你的其他问题)