Simplifying code

2

I have the following code snippet:

<p:menuButton value="#{messages['relatorio']}">
                    <p:menuitem value="PDF" update="painelRelatorioResumoExpedicaoDesembarque" oncomplete="gerarRelatorio();" id="commandButtonGerarRelatorio" action="#{executarServicoController.executarServicoHelper}" ajax="true">
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeResponsavelCarga}" value="#{appHelper.idUnidadeResponsavelCorrente}" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeOperacaoCarga}" value="#{appHelper.idUnidadeOperacaoCorrente}" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceBean}" value="modalService" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceMethod}" value="obterDadosRelatoriosMovimentacaoImportacao" />
                    <f:param name="tipoRelatorio" value="pdf" />
                    </p:menuitem>
                    <p:menuitem value="XLS" update="painelRelatorioResumoExpedicaoDesembarque" oncomplete="gerarRelatorio();" id="commandButtonGerarRelatorios" action="#{executarServicoController.executarServicoHelper}" ajax="true">
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeResponsavelCarga}" value="#{appHelper.idUnidadeResponsavelCorrente}" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeOperacaoCarga}" value="#{appHelper.idUnidadeOperacaoCorrente}" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceBean}" value="modalService" />
                    <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceMethod}" value="obterDadosRelatoriosMovimentacaoImportacao" />
                    <f:param name="tipoRelatorio" value="xls" />
                    </p:menuitem>
                </p:menuButton>

Notice that there is 2 <p:menuitem> where the only difference is that <f:param I set the different value. Is there any way to simplify and leave everything in a block for example?

    
asked by anonymous 12.05.2017 / 15:18

1 answer

0

To do this you will need two things:

  • A list to iterate inside the Bean containing the necessary options;
  • A c: forEach (ui: repeat will not work due to the limitations of the component p: menuButton) within xhtml to iterate through the previously included list.

So, to include the list in the bean you can do this:

public List<String> getTiposRelatorio() {
    List<String> tipos = new ArrayList<>();
    tipos.add("PDF");
    tipos.add("XLS");
    return tipos;
}

Then, iterate this list inside the p: menuButton element including iteration variables, like this: - Obs1: replace the variable "name_of_your_bean" with the name of your bean; - Obs2: Do not forget to import the "c" namespace at the top of xhtml (xmlns: c="http://xmlns.jcp.org/jsp/jstl/core").

<p:menuButton value="#{messages['relatorio']}">
    <c:forEach list="#{nome_do_seu_bean.tiposRelatorio}" var="tipo">
        <p:menuitem value="#{tipo}" update="painelRelatorioResumoExpedicaoDesembarque" oncomplete="gerarRelatorio();" id="commandButtonGerarRelatorio" action="#{executarServicoController.executarServicoHelper}" ajax="true">
            <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeResponsavelCarga}" value="#{appHelper.idUnidadeResponsavelCorrente}" />
            <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.parametros.paramsHelper.idUnidadeOperacaoCarga}" value="#{appHelper.idUnidadeOperacaoCorrente}" />
            <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceBean}" value="modalService" />
            <f:setPropertyActionListener target="#{executarServicoPreenchendoParametrosExecutarForm.serviceMethod}" value="obterDadosRelatoriosMovimentacaoImportacao" />
            <f:param name="tipoRelatorio" value="#{tipo.toLowerCase()}" />
        </p:menuitem>
    </c:forEach>
</p:menuButton>

I imagine this approach will solve your problem, good luck!

Note: This solution is not indicated in these cases. According to the documentation of PrimeFaces, to create dynamic items within menu components you should do directly through the Bean, creating the components dynamically, but because you are including multiple passThroughAttribute I did not use this approach.

    
19.05.2017 / 16:00