Code simplification

Asked

Viewed 42 times

2

I have the following piece of code:

<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 are 2 <p:menuitem> where the only difference is that <f:param i define value differently. Is there any way to simplify and leave everything in a block for example ?

1 answer

0

To do this you will need two things:

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

So to include the list in the bean you can do so:

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 the iteration variables, thus: - Obs1: replace the variable "name_do_seu_bean" with the name of your bean; - Obs2: Do not forget to import the namespace "c" 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 Primefaces documentation, to create dynamic items within menu components you should do directly by Bean, creating the components dynamically, but because you are including several passThroughAttribute I preferred not to use this approach.

Browser other questions tagged

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