0

Someone help please... I am trying to pass a list as parameter to a p:commandButton inside a p:dataTable. It is always null.

What is wrong with this? Thanks in advance!

The Screen components:

<h:form id="form-upload" enctype="multipart/form-data">
    <div class="">
        <h:outputText value="Foram encontradas as seguintes restrições:" />
        <br /> <br />

        <p:dataTable var="restricao" id="listaRestricoes"
                     value="#{decompDecisaoRestricoesController.listView}">
            <p:inputText value="#{restricao.id}" type="hidden" />
                            
            <p:column headerText="Restrição">
                <h:outputText value="#{restricao.nome}" />
                                
                <p:selectOneRadio id="comentar"
                                  value="#{restricao.comentar}" layout="custom">
                    <f:selectItem itemValue="sim" />
                    <f:selectItem itemValue="nao" />
                </p:selectOneRadio>
            </p:column>

            <p:column headerText="Comentar">
                <p:radioButton for="comentar" itemIndex="0" />
            </p:column>

            <p:column headerText="Descomentar">
                <p:radioButton for="comentar" itemIndex="1" />
            </p:column>

        </p:dataTable>
    </div>
    <div class="button-group">
        <p:commandButton icon="ui-icon-circle-triangle-e"
                         value="Executar" widgetVar="uploadButton" 
                         action="#{decompDecisaoRestricoesController.chamaService(listaRestricoes)}" />
    </div>
</h:form>

The Controller:

@Named
@RequestScoped
public class DecompDecisaoRestricoesController implements Serializable {

    private static final long serialVersionUID = -6252355181831338279L;

    @Inject
    private DecompDecisaoRestricoesService service;

    private List<RestricaoDecomp> listView;

    public List<RestricaoDecomp> getListView() {
        return listView;
    }

    public void setListView(List<RestricaoDecomp> listView) {
        this.listView = listView;
    }

    public void onLoad() {
        listView = service.listaRestricoesBanco();
    }

    public void chamaService(List<RestricaoDecomp> lista) {
        service.executar(lista);
    }

}

And the Service:

@Stateless
public class DecompDecisaoRestricoesService {

    @Inject
    RestricaoDecompDAO restricaoDecompDAO;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public void executar(List<RestricaoDecomp> lista) {
        for (RestricaoDecomp restricao : lista) {
            logger.info("NOME: " + restricao.getNome());
            logger.info("COMENTAR:  " + restricao.getComentar());
        }
    }

    public List<RestricaoDecomp> listaRestricoesBanco() {
        List<RestricaoDecomp> lista = new ArrayList<RestricaoDecomp>();
        lista = restricaoDecompDAO.listAll();
        return lista;
    }

}

When rendering, the list is displayed in the View. But on submit, it just sends null.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
gbossa
  • 377
  • 1
  • 3
  • 18
  • Does this answer your question? [How to choose the right bean scope?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Jasper de Vries Apr 04 '22 at 08:10

1 Answers1

0

Did it! The bean was RequestScoped, so, after the request, it just throwed away the list values.

Changed it to ViewScoped and voilá! The list value was there after submission, with the changes the fields.

After that, I just cleaned the view after submit. Like this:

<p:commandButton icon="ui-icon-circle-triangle-e" onsuccess="window.location.reload(); ..."

=)

gbossa
  • 377
  • 1
  • 3
  • 18