Jsf ui:重复h:selectManyCheckbox我无法在列表中获取值

Jsf ui:重复h:selectManyCheckbox我无法在列表中获取值,jsf,uirepeat,selectmanycheckbox,Jsf,Uirepeat,Selectmanycheckbox,我正在从事JSF2.2项目JBoss8.x 我仅使用selectManyCheckbox创建了一个动态表单 我有以下代码: <h:form> <table border="5"> <ui:repeat var="question" value="#{beanQuiz.traninigee.questions}"> <tr> #{question.question} </tr>

我正在从事JSF2.2项目JBoss8.x

我仅使用selectManyCheckbox创建了一个动态表单

我有以下代码:

<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的值可以指向字符串[]或列表。使用此代码,选定的项目不包含所有选中值,仅包含最新选中的组

我应该如何获取所有检查值

@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;
    }
@ManagedBean
@会议范围
公共类beanQuiz实现了可序列化{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
@EJB
私人培训服务本地;
私有列表选择的报告;
私人名单培训;
私人培训培训培训=新培训();
公共字符串重定向测验(int idt){
transinigee=local.findtrainingbyId(idt);
返回“测验”;
}
公共列表GetSelectedReportes(){
返回选定的报告;
}
公共无效集合SelectedReports(列出SelectedReports){
this.selectedrepons=selectedrepons;
}
公共信息公司{
返回公司;
}
公共空间公司(国际公司){
this.inc=inc;
}
私人国际公司;
公共培训{
返回变速箱;
}
公共无效设置传输(培训传输){
this.tranigee=tranigee;
}
@施工后
公共void init(){
inc=0;
培训=本地。findAlltrainings();
//traninigee=local.findtrainingbyId(1);
//System.out.println(“----输出-”+traninigee);
}
//私有静态映射值;
//静止的{
//color2Value=newlinkedhashmap();
//for()
//color2Value.put(“Color2-Red”,“Red”);//标签,值
//      
//  }
公共列表获取训练(){
返乡培训;
}
公共培训(列出培训){
这就是培训;
}
我的类图是这样的: 我参加了一个与问题清单相反的培训班(一个月) 我的课堂问题包含一对多的回答列表 我的类responses是一个简单的类,它以字符串的形式包含响应 我在用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

}
Classe培训
{
@OneToMany(fetch=FetchType.EAGER,mappedBy=“training”)
私人名单问题;
}
类问题
{
@OneToMany(mappedBy=“question”,cascade=CascadeType.ALL,fetch=FetchType.LAZY)
私人名单答复;
}
类响应{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人内部响应ID;
私有布尔值是有效的;
//双向多对一关联问题
}
我的列表测验expl.xhtml

<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>



和我的结果页面,我将在其中显示选中的选择框的结果

      <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>

选定的答复:


#{}

#{res}

------------




好的,下面是您必须做的:

a) 通过删除列表并将其替换为将问题与答案列表配对的映射来修改支持Bean:

@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;
}
@ManagedBean
@会议范围
公共类BackingBean实现了可序列化{
@EJB
私人投注服务本地;
私人培训=新培训();
私人地图问答
=新HashMap();
//设置要在multiChebox标签中使用的培训时
//将问题地图设置为所选答案的列表。
公共字符串重定向测验(int idt){
培训=本地.findtrainingbyId(idt);
for(问题:tranigee.getQuestions()){
问题答案。放置(问题,新ArrayList());
}
返回“测验”;
}
公共地图getQuestionAnswers(){
回答问题;
}
公共问题解答(地图问题解答){
this.questionAnswers=问题答案;
}
b) 您可以修改selectManyCheckBox,为每个问题使用单独的列表:

<ui:repeat var="question" value="#{beanQuiz.training.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.name}"
                                    itemValue="#{response.id}" />
                </h:selectManyCheckbox>
            </tr>
        </ui:repeat>

#{问题.问题}:
因此,每组复选框都有自己的列表对象,不会受到其他组的干扰。 您只需在提取每个问题的结果时更改逻辑,或使用“questionAnswers.values();”,迭代并合并该问题的所有答案


希望这有助于

好的,下面是你要做的:

a) 通过删除列表并将其替换为将问题与答案列表配对的映射来修改支持Bean:

@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;
}
@ManagedBean
@会议范围
公共类BackingBean实现了可序列化{
@EJB
私人投注服务本地;
私人培训=新培训();
私人地图问答
=新HashMap();
//设置要在multiChebox标签中使用的培训时
//将问题地图设置为所选答案的列表。
公共字符串重定向测验(int idt){
培训=本地.findtrainingbyId(idt);
for(问题:tranigee.getQuestions()){
问题答案。放置(问题,新ArrayList());
}
返回“测验”;
}
公共地图getQuestionAnswers(){
回答问题;
}
公共问题解答(地图问题解答){
this.questionAnswers=问题答案;
}
b) 您可以修改selectManyCheckBox,为每个问题使用单独的列表:

<ui:repeat var="question" value="#{beanQuiz.training.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.name}"
                                    itemValue="#{response.id}" />
                </h:selectManyCheckbox>
            </tr>
        </ui:repeat>

#{问题.问题}:
正因为如此,每组复选框都将有自己的列表对象,并且不会受到