2016-11-26 10 views
0

私はJSF 2.2のプロジェクトに取り組んでいる、JBossの8.xのUI:私はトン私だけselectManyCheckboxでダイナミックなフォームを作成し、リスト内の値

を得ることができますselectManyCheckbox:Hを繰り返します。

私は次のコードを持っている:

<h:form> 



        <table border="5"> 


<ui:repeat var="question" value="#{beanQuiz.traninigee.questions}"> 
     <tr> #{question.question} </tr> 

     <tr> 
     <h:selectManyCheckbox value="#{beanQuiz.questionAnswers[question]}" 
             converter="javax.faces.Integer"> 
        <f:selectItems var="response" 
            value="#{question.responses}" 
            itemLabel="#{response.reponse}" 
            itemValue="#{response.responseId}" /> 
       </h:selectManyCheckbox> 
      </tr> 
     </ui:repeat> 

       </table> 
<h:commandButton value="Submit" action="result" styleClass="btn btn-success btn-cons m-b-10" /> 
     <h:commandButton value="Reset" type="reset" styleClass="btn btn-warninig btn-cons m-b-10"/> 

     </h:form> 

selectedArticlesは、エンティティのリストです。

selectManyCheckboxの値はString []またはListを指すことができます。このコードでは、selectedArticlesにはチェックされたすべての値が含まれず、最新のチェックされたグループのみが含まれます。

すべてのチェック値を取得するにはどうすればよいですか?

@ManagedBean 
@SessionScoped 
public class beanQuiz implements Serializable{ 


    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @EJB 
    private trainingServiceLocal local; 

    private List<Integer> selectedreponses; 

    private List<Training> trainings; 

    private Training traninigee=new Training(); 



public String redirectquiz(int idt){ 

    traninigee=local.findtrainingbyId(idt); 



    return "quiz"; 
} 


    public List<Integer> getSelectedreponses() { 
     return selectedreponses; 
    } 

    public void setSelectedreponses(List<Integer> selectedreponses) { 
     this.selectedreponses = selectedreponses; 
    } 

    public int getInc() { 
     return inc; 
    } 

    public void setInc(int inc) { 
     this.inc = inc; 
    } 


    private int inc; 

    public Training getTraninigee() { 
     return traninigee; 
    } 

    public void setTraninigee(Training traninigee) { 
     this.traninigee = traninigee; 
    } 

    @PostConstruct 
    public void init() { 
     inc=0; 
     trainings = local.findAlltrainings(); 


     //traninigee=local.findtrainingbyId(1); 
     //System.out.println("-----||||||||||||----**---"+traninigee); 




    } 


// private static Map<String,Object> color2Value; 
// static{ 
//  color2Value = new LinkedHashMap<String,Object>(); 
//  for() 
//  color2Value.put("Color2 - Red", "Red"); //label, value 
//  
// } 





    public List<Training> getTrainings() { 
     return trainings; 
    } 




    public void setTrainings(List<Training> trainings) { 
     this.trainings = trainings; 
    } 

私のクラス図は、そのようなものです:私は質問のリストをcontrainsトレーニングのクラス(onetomany) 私のクラスの質問は、(多くの1つ)reponsesのリストが含まれているを持っまし と私のクラスの応答があります

<ui:repeat var="cat" value="#{beanQuiz.trainings}"> 




<h:commandButton action="#{beanQuiz.redirectquiz(cat.trainingId)}" value="#{cat.name} " styleClass="btn btn-block btn-success"> 




</h:commandButton> 





<br></br> 

</ui:repeat> 
.xhtml JPA

Classe Training 
{ 
    @OneToMany(fetch = FetchType.EAGER,mappedBy="training") 
    private List<Question> questions; 
} 



classe Question 

{ 

@OneToMany(mappedBy="question",cascade=CascadeType.ALL,fetch=FetchType.LAZY) 
    private List<Respons> responses; 

} 




classe response { 
@Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private int responseId; 
    private Boolean isValid; 

    //bi-directional many-to-one association to Question 

} 

私のリストのクイズが正しくありませんを使用して文字列 イムとして応答を含む単純なクラス0

と私の結果ページので、ここでは、

 <h:form> 

      <p>selected responses: </p> 

      <br/> 

      <p>#{}</p> 

<c:forEach items="#{beanQuiz.questionAnswers.values()}" var="res"> 


       <p> #{res}</p> 
<p>------------</p> 
       <br></br> 

</c:forEach> 
     </h:form> 
+0

あなたのお名前をバッキングすることができますか?少なくとも関連する部分は写真を手に入れやすくなります –

+0

@MaciejKowalski私の投稿を更新して申し訳ありません – tero17

答えて

2

[OK]をチェックしselectboxesの結果を表示しようとしてイムは、あなたがしなければならないものです:

a)に除去することにより、バッキングBeanを変更リストと回答の一覧に質問をペアリングします地図でそれを置き換える:あなたは質問ごとに別々のリストを使用するようにselectManyCheckBoxを変更)

@ManagedBean 
@SessionScoped 
public class BackingBean implements Serializable{ 


@EJB 
private wagentServiceLocal local; 

private Training training=new Training(); 

private Map<Question, List<Integer>> questionAnswers 
     = new HashMap<Question, List<Integer>>(); 

// When setting up the training to be used in the multiChebox tag 
// set up the map of question to list of chosen responses. 
public String redirectquiz(int idt){ 

    training=local.findtrainingbyId(idt); 

    for(Question question: traninigee.getQuestions()){ 
      questionAnswers.put(question, new ArrayList<Integer>());    
    } 

    return "quiz"; 
} 

public Map<Question, List<Integer>> getQuestionAnswers() { 
    return questionAnswers; 
} 

public void setQuestionAnswers(Map<Question 
       , List<Integer>>  questionAnswers) { 
    this.questionAnswers = questionAnswers; 
} 

B:

おかげで、チェックボックスの各グループは独自のリストオブジェクトを持ち、他のグループに干渉されません。 それぞれの質問の結果を抽出する方法でロジックを変更するか、 'questionAnswers.values();'を使用して、質問のすべての回答を反復して組み合わせるだけです。

希望すること

+0

すべての部分を今追加しました。私はちょっと他の人のために物事をより明確にするために名前を変更しました –

+0

こんにちはmaciej私は私の実際のケースにそれを調整することができます私の問題にあなたのソリューションを適用することはできません – tero17

+0

私は更新ポストと私はマップを使用して質問を選択された回答のリストにペア設定する新しい、より良いアプローチを思いついた。見てみな。もう一度名前を少し変更しました。 –

関連する問題