How to view Questions recovered from a DAO on the same JSF page

Asked

Viewed 72 times

1

good afternoon.

I have a page called simulated.xhtml in which I receive some parameters (Filter) to make a query in the database according to the filter. Everything is already working, what I wanted was to take this query and display the questions on the same page, IE, Simulado.xhtml

Follow the source code:

Filter for consultation:

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

Gera Simulado Bean:

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

Pagina Simulado.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 view the questions on the console, my goal is to display these questions on the same simulated page.xhtml

Thanks in advance.

  • Do you just want to show what’s already filtered? @Felipeportela

  • that’s right @Wellingtonavelino. Only that I wanted to display on the same screen simulated.xhtml because it is she who makes the filter. Is there any problem?

  • Dude, I don’t see a problem, you can render this below your filter... You already have the objects to display?

1 answer

2


I don’t know how you want to display these questions on the screen, could be on Grid, Lista and N ways. You could leave a validation in your .xhtml:

Inside this Voce 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>

  • Friend @Wellingtonavelino, I haven’t been able to display that way yet. My intention was to display it in a more organized way possible. What do you recommend? A datatable? At the moment I have a problem, I tried to display using a standard p:Panelgrid <p:panelGrid id="test"> <h:outputText value="#{generationSimuladoBean.questoes}"> , and received the following result: [com.sisEnade.tcc.modelo.Questao@20, com.sisEnade.tcc.modelo.Questao@21, com.sisEnade.tcc.modelo.Questao@22] , the object addresses. How do I resolve?

  • These questions is what a list is? if it is you need to iterate it... and generates the class toString.

  • Thanks @wellingtonAvelino. Solved brother!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.