input component inside ui:repeat, how to save submitted values

前端 未结 1 1310
天命终不由人
天命终不由人 2020-12-03 20:35

I\'m displaying a list of questions from database and for each question I have to display a list of options, in this case radio buttons.



        
相关标签:
1条回答
  • 2020-12-03 21:04

    You need to associate the input value with the repeated variable var in some way. Right now you're not doing that anywhere and basically binding all input values to one and same bean property. So, when the form gets submitted, every iteration will override the bean property everytime with the value of the current iteration round until you end up getting the value of the last iteration round. This is definitely not right.

    The simplest way would be to directly associate it with the object represented by var:

    <p:selectOneRadio value="#{question.selectedOption}">
    

    In your specific case, this only tight-couples the "question" model with the "answer" model. It's reasonable to keep them separated. A more proper solution in your specific case is to map it with currently iterated #{question} as key (provided that it has a proper equals() and hashCode() implementation, obviously):

    <p:selectOneRadio value="#{formData.selectedOptions[question]}">
    

    With:

    private Map<Question, String> selectedOptions = new HashMap<>();
    

    Regardless of the approach, in the action method, just iterate over it to collect them all.

    0 讨论(0)
提交回复
热议问题