How to display Questions retrieved from a DAO on the same JSF page

1

Good afternoon.

I have a page called simulate.xhtml where I get some parameters (Filter) to query the database according to the filter. It's all working, what I wanted was to get this query and display the questions on the same page, that is, Simulated.xhtml

Follow the source code:

Query filter:

@Inject
    private EntityManager manager;

    public List<String> geraSimuladoPorFiltro(Long codigoCurso,
            Integer complexidade, Integer numeroDeQuestoes) {
        String query = "select pergunta from Questao WHERE curso_codigo = ?1 AND complexidade = ?2";
        List<String> questoes = manager.createQuery(query, String.class)
                .setParameter(1, codigoCurso).setParameter(2, complexidade)
                .setMaxResults(numeroDeQuestoes).getResultList();
        for (String q : questoes) {
            System.out.println(q);
        }
        return questoes;
    }

Simulated Bean Builds:

@Named
@ViewScoped
public class GerarSimuladoBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    SimuladoFiltroDAO simuladoFiltroDAO;

    private Curso cursoSelecionado;
    private Integer complexidadeSelecionada;
    private Integer numeroDeQuestoesSimulado;

    @Transactional
    public void gerarSimulado() {
        this.simuladoFiltroDAO.geraSimuladoPorFiltro(cursoSelecionado.getCodigo(),
                this.complexidadeSelecionada, this.numeroDeQuestoesSimulado);
    }

    public Curso getCursoSelecionado() {
        return cursoSelecionado;
    }

    public void setCursoSelecionado(Curso cursoSelecionado) {
        this.cursoSelecionado = cursoSelecionado;
    }

    public Integer getComplexidadeSelecionada() {
        return complexidadeSelecionada;
    }

    public void setComplexidadeSelecionada(Integer complexidadeSelecionada) {
        this.complexidadeSelecionada = complexidadeSelecionada;
    }

    public Integer getNumeroDeQuestoesSimulado() {
        return numeroDeQuestoesSimulado;
    }

    public void setNumeroDeQuestoesSimulado(Integer numeroDeQuestoesSimulado) {
        this.numeroDeQuestoesSimulado = numeroDeQuestoesSimulado;
    }
}

Simulated Page.xhtml

<ui:define name="titulo">Gerar Simulado</ui:define>

    <ui:define name="corpo">

        <h1>Filtro para gerar Simulado</h1>
        <h:form id="frmCadastro">
        <br></br>

        <h:panelGrid columns="2">

            <p:outputLabel value="Curso" for="curso" style="font-weight:bold" />
            <p:selectOneMenu id="curso"
                value="#{gerarSimuladoBean.cursoSelecionado}"
                converter="cursoConverter" required="true"
                requiredMessage="Preencha o curso">
                <f:selectItem itemLabel="Selecione..." />
                <f:attribute name="collectionType" value="java.util.ArrayList" />
                <f:selectItems value="#{cadastroQuestaoBean.cursos}" var="curso"
                    itemLabel="#{curso.nome}" itemValue="#{curso}" />
            </p:selectOneMenu>
        </h:panelGrid>


        <h:panelGrid id="gridComplexidade" columns="2">
            <p:outputLabel value="Complexidade da Questão" for="complexidade"
                style="font-weight:bold" />
            <p:selectOneMenu id="complexidade"
                value="#{gerarSimuladoBean.complexidadeSelecionada}" required="true"
                requiredMessage="Por favor, preencha a complexidade.">
                <f:selectItem itemLabel="Selecione..." />
                <f:selectItem itemLabel="1" itemValue="1" />
                <f:selectItem itemLabel="2" itemValue="2" />
                <f:selectItem itemLabel="3" itemValue="3" />
                <f:selectItem itemLabel="4" itemValue="4" />
                <f:selectItem itemLabel="5" itemValue="5" />
            </p:selectOneMenu>
            </h:panelGrid>

            <h:panelGrid id="gridNumeroQuestoes" columns="2">
            <p:outputLabel value="Número de questoes" for="numeroDeQuestoes"
                style="font-weight:bold" />
            <p:selectOneMenu id="numeroDeQuestoes"
                value="#{gerarSimuladoBean.numeroDeQuestoesSimulado}" required="true"
                requiredMessage="Por favor, preencha o numero de questoes.">
                <f:selectItem itemLabel="Selecione..." />
                <f:selectItem itemLabel="3" itemValue="3" />
                <f:selectItem itemLabel="5" itemValue="5" />
                <f:selectItem itemLabel="10" itemValue="10" />
                <f:selectItem itemLabel="20" itemValue="20" />
                <f:selectItem itemLabel="40" itemValue="40" />
            </p:selectOneMenu>
            </h:panelGrid>

            <p:commandButton value="Gerar Simulado" action="#{gerarSimuladoBean.gerarSimulado}"
                icon="ui-icon-search" iconPos="right" update="frmCadastro">
            </p:commandButton>

    </h:form>
    </ui:define>

I can already see the questions in the console, my goal is to display these questions on the same simulated page.xhtml

Thank you in advance.

    
asked by anonymous 13.10.2015 / 19:42

1 answer

2

I do not know how you want to display these questions on the screen, it could be in Grid , Lista and N manners. You could leave a validation on your .xhtml :

Within this form you can retrieve your questions by iterating your list and rendering only if you have content in it.

 <h:form rendered="#{not empty meuMB.listaPerguntas}" >
   <ui:repeat var="perguntas" value="#{meuMB.listaPerguntas}" varStatus="status">
    <h:outputText value="#{perguntas.conteudo}" /> //exibe perguntas
</ui:repeat>

    
13.10.2015 / 20:59