2011-12-20 24 views
1

我已經(終於)設法使問題形成我希望它的工作方式的代碼。最後一部分仍然缺失:問題所屬的類別。我已經創建了一個問題處理程序類來管理問題的持續性,我想知道是否應該將它與該類中的其他方法放在一起?我應該在哪裏把提取的所有類別,讓他們查看

有10個類別(只是一個ID和一個描述)。我認爲我缺乏經驗和知識來確切知道我應該如何組織以及屬於支持豆類。

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方法,但在這裏並不重要。

回答

1

如果邏輯與您的相關,那麼CategoryServiceBean是從中獲取類別的正確位置。

+0

是它通常有一個servicebean公關領域模型? (我正在學習,所以這可能是一個愚蠢的問題) – LuckyLuke 2011-12-20 14:10:55

+0

這絕對沒問題。 – yatskevich 2011-12-20 14:41:56

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); 
} 

werein你只是#{bean.question.answers}#{bean.question.categories}代替#{bean.answers}#{bean.categories}引用視圖中的嵌套屬性。

至於具體問題,我認爲你是在談論「可用類別」,這是在<f:selectItems>和使用什麼樣的?是的,你可以把它放在一個單獨的(application scoped?)bean中。但如果涉及到「選擇的類別」,只是將其綁定到Question#{bean.question.categories}像以前建議。例如。

<h:selectManyListbox value="#{bean.question.categories}" converter="#{categoryConverter}"> 
    <f:selectItems value="#{data.categories}" /> 
</h:selectManyListbox> 

請注意,該轉換器由EL引用,因爲我認爲這是一個@ManagedBean@Named只要你想使用它裏面的@EJB(根據您的其他quesiton)。

+0

但是這是用戶提出新問題的時候。在此之前沒有答案或類別的問題。那時我想不出另一種方式。 – LuckyLuke 2011-12-20 16:14:04

+0

我不明白這怎麼可能會造成問題。你所要做的就是做'new Question()'並確保嵌套列表屬性不是'null'。 – BalusC 2011-12-20 16:16:15

+0

類別顯示在選擇菜單中。在此之前,沒有任何類別與新問題有關。我不確定我是否理解它。我應該創建一個新的Question對象,然後使用兩個數組列表並將新創建的問題對象綁定爲答案/類別? – LuckyLuke 2011-12-20 16:20:37

相關問題