2

I have a bean called Question with a lot of alternatives -->

 Question have 1:N Alternative. 

I have a Page with a lot of Question, like:

 Page have 1:N Question

I need create a XHTML page showing the amount of questions and your alternatives, like this:

<c:forEach var="questaoCurso"
           items="#{cursandoMB.paginaAtualProva.questoesCurso}">
    <h:outputText value="#{questaoCurso.questao.texto}" />
    <br />

    <h:selectOneRadio style="font-weight: normal"
                      value="#{cursandoMB.alternativasEscolhida}" layout="pageDirection">

        <f:selectItems value="#{questaoCurso.questao.alternativasPreenchidas}"
                       var="c"
                       itemValue="#{c}"
                       itemLabel="#{c.texto}" />
    </h:selectOneRadio>
    <br />
    <br />
</c:forEach>

The problem is that I don't know how I can set each choose alternative in a List in my ManagedBean. I can't create one variable for each Question, because this is dynamic and I don't know the amount of questions in design-time.

SOLVED:

I used a Map in RadioButton, see:

<h:selectOneRadio
            value="#{cursandoMB.alternativasEscolhidas[questaoCurso]}" converter="entityConverter" layout="pageDirection">
            <f:selectItems
                value="#{questaoCurso.questao.alternativasPreenchidas}" var="c"
                itemValue="#{c}" itemLabel="#{c.texto}" />
        </h:selectOneRadio>
Shelly
  • 1,035
  • 5
  • 27
  • 51

1 Answers1

0

First, You must not mix render-time (e.g. f:selectItems, h:inputText ...) and build-time tags (JSTL tags, f:actionListener, ui:include ... ), for more understanding of the view build time and render time concepts and how that works please take a look to this BalusC's answer: JSTL in JSF2 Facelets... makes sense?

So IMO the only thing to replace is the c:forEach tag with ui:repeat like this:

<ui:repeat var="questaoCurso"
           value="#{cursandoMB.paginaAtualProva.questoesCurso}">
    <h:outputText value="#{questaoCurso.questao.texto}" />
    <br />

    <h:selectOneRadio style="font-weight: normal"
                      value="#{cursandoMB.alternativasEscolhida}" layout="pageDirection">

        <f:selectItems value="#{questaoCurso.questao.alternativasPreenchidas}"
                       var="c"
                       itemValue="#{c}"
                       itemLabel="#{c.texto}" />
    </h:selectOneRadio>
    <br />
    <br />
</ui:repeat>

The value attribute of ui:repeat Tag is:

The name of a collection of items that this tag iterates over. The collection may be a List, array, java.sql.ResultSet, or an individual java Object. If the collection is null, this tag does nothing.

So you can use a List of Object in that value which in your case will be a List of Question objects, and in your Question bean you can include a List of answers (while Answer may also be a bean).

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • But the problem is that value attribute of SelectOneMenuRadio should be a list: value="#{cursandoMB.alternativasEscolhida}". How can i do it ? – Shelly Apr 29 '15 at 23:23
  • @Shelly what do you mean by "how can i do it" ? your variable `alternativasEscolhida` must be a List of `Question` objects ? what is the problem here ?! – Tarik Apr 30 '15 at 08:03