UI:重复H:SelectManyCheckBox I可以在列表中获取值



我正在从事JSF 2.2项目,JBOSS 8.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>

选择的颗粒是实体列表。

我在网络上看到SelectManyycheckbox的值可以指向字符串[]或列表。使用此代码,选定的片段不包含所有检查值,只有最新的检查组。

我该怎么做才能获得所有检查值?

@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

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

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

和我的结果页面,其中我将在其中显示检查selectbox的结果

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

好吧,所以这是您必须做的:

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

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();'',迭代并结合所有回答的问题。

希望有帮助

相关内容

  • 没有找到相关文章

最新更新