【问题标题】:ui:repeat h:selectManyCheckbox i can t get values inside a list [duplicate]ui:repeat h:selectManyCheckbox我无法获取列表中的值[重复]
【发布时间】:2016-11-26 17:49:28
【问题描述】:

我正在开发一个 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>

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

我的类图是这样的: 我有一类培训可以限制问题列表(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>

我的结果页面将显示选中的选择框的结果

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

【问题讨论】:

  • 你能附上一些支持bean的代码吗?至少是相关部分,因为这样更容易获得图片
  • @MaciejKowalski 对不起,我更新了我的帖子

标签: jsf uirepeat selectmanycheckbox


【解决方案1】:

好的,这就是你必须做的:

a) 通过删除列表并将其替换为将问题与答案列表配对的 Map 来修改 Backing 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>

因此,每组复选框都有自己的 List 对象,不会受到其他组的干扰。 您只需要更改为每个问题提取结果的方式或使用“questionAnswers.values();”的逻辑,迭代并结合问题的所有响应。

希望有帮助

【讨论】:

  • 现在添加了所有部分。我已经更改了一些命名以使其他人更清楚
  • 嗨 maciej,我已经用我的真实案例概念更新了我的帖子
  • 好的,我已经更新了帖子,并且我提出了一种新的、更好的方法,它使用 Map 将问题与所选答案列表配对。一探究竟。我再次重构了命名。
  • 那行不通我正在用回复显示问题,但是使用您的解决方案它没有显示带有回复的问题它只显示问题谢谢您的帮助任何其他建议??
  • 这个话题的底线是你必须使用 Map 而不是 List。请创建一个单独的主题,因为我猜您现在正面临其他部分逻辑的问题。
猜你喜欢
  • 2021-06-30
  • 2017-05-23
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 2013-11-27
  • 2013-04-23
  • 1970-01-01
相关资源
最近更新 更多