ui:repeat h:selectManyCheckbox我无法获取列表内的值 [英] ui:repeat h:selectManyCheckbox i can t get values inside a list

查看:61
本文介绍了ui:repeat h:selectManyCheckbox我无法获取列表内的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发JSF 2.2项目jboss 8.x

I'm working on a JSF 2.2 project , jboss 8.x

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

I created a dynamic form only with 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是实体列表.

selectedArticles is list of entities.

我在网上看到selectManyCheckbox的值可以指向String []或List.使用此代码,selectedArticles不会包含所有选中的值,而仅包含最新选中的组.

I see on the web the value of a selectManyCheckbox can point to a String[] or List. With this code the selectedArticles don't contains all the checked value, only the latest checked group.

如何获取所有检查值?

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

我的类图是这样的: 我有一堂课禁止使用问题清单(onetomany) 我的班级问题包含答复列表(一对多) 我的班级回应是一个简单的类别,其中包含回应为字串 我正在使用jpa

my class diagram is like that : i ve got a class of trainings that contrains list of question (onetomany) my class question contains list of reponses (one to many ) and my class responses is a simple class that contains the response as string im using 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

}

我的列表测验expl .xhtml

my list quiz 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>

推荐答案

好的,所以这是您要做的:

Ok, so here is what you have to do:

a)通过删除列表并将其替换为Map,以将问题与答案列表配对来修改Backing Bean:

a) Modify the Backing Bean by removing the List and replacing it with a Map which will pair up questions to the list of answers:

@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)您修改selectManyCheckBox以对每个问题使用单独的列表:

b) You modify the selectManyCheckBox to use a separate List for each of the Questions:

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

由于这一点,每组复选框将具有其自己的List对象,并且不会受到其他组的干扰. 您只需更改提取每个问题的结果的方式的逻辑,或使用"questionAnswers.values();"来迭代并合并问题的所有答案即可.

Thanks to that, each group of checkboxes will have its own List object and will not be interfered by other groups. You just have to change your logic on the way you extract the results for each of the question or use 'questionAnswers.values();', iterate and combine all the responses regardles of the question.

希望有帮助

这篇关于ui:repeat h:selectManyCheckbox我无法获取列表内的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆