0

I'm trying to create a ModalPanel from a dataTable (RichFaces 4.5.1, MyFaces 2.2.6, Spring 3.1.1, Tomcat 7.0.27) but I can't.

The modalPanel has values ​​to be displayed based on the selected row in the table. When I click in a commandLink, trigger a actionListener should feed these values (localidadeMB.carregarColecoes()), but it does't work, because the reference to attributes of bean is null (has not been set by the f:setPropertyActionListener located in commandLink).

What was missing?

*page.xhtml

(...)
<f:view>
<h:form id="formConsulta">
    <rich:dataTable id="tabela"
        value="#{localidadeMB.getLocalidadesPorPalavra()}" var="loc"
        rowKeyVar="row" rows="20" width="800px" render="scroller">
        <rich:column>
            (...)
        </rich:column>
        <rich:column>
            <f:facet name="header">
                <h:outputText value=""/>
            </f:facet>
            <a4j:commandLink id="editlink" ajaxSingle="true" 
                oncomplete="#rich:component('modalEditarLocalidade')}.show()"
                actionListener="#{localidadeMB.carregarColecoes}" 
                render="modalEditarLocalidade">
            <h:graphicImage value="/img/edit.png"/>
            <f:setPropertyActionListener value="#{loc}" target="#{localidadeMB.localidade}" />
            </a4j:commandLink>
        </rich:column>
    </rich:dataTable>
</h:form>

<ui:include src="modalPanel.xhtml" />

*modalPanel.xhtml

<ui:composition>
<rich:popupPanel id="modalEditarLocalidade" autosized="true"
    modal="false" resizable="false">
    <f:facet name="header">
        <h:outputText value="Alterar localidade" />
    </f:facet>
    <f:facet name="controls">
        <h:graphicImage id="modalClosePNG2" value="/img/close1.png"
            style="cursor:default;"
            onclick="Richfaces.hideModalPanel('modalEditarLocalidade')" />
    </f:facet>
    <h:form>
        <h:outputLabel value="Nome da localidade:" for="nomeLocalidade" />
        <h:inputText id="nomeLocalidade"
            value="#{localidadeMB.localidade.nome}" required="true"
            immediate="true"/>
        </h:inputText>
    </h:form>
</rich:popupPanel>
</ui:composition>

*ManagedBean

private Localidade localidade;

public void setLocalidade(Localidade l){
    this.localidade = l;
}

public void carregarColecoes(ActionEvent action){
    System.out.println(localidade.getNome());

        ***** print NULL *****
}
Rogério Arantes
  • 712
  • 1
  • 8
  • 29

2 Answers2

1

If you want to set up a variable first and then execute a method you have to use @action for that method, actionListeners are executed first but since both of your methods are actionListeners the localidadeMB.carregarColecoes is executed before the variable is set. (And btw. h:commandLink has no "ajaxSingle" or "oncomplete" attributes.)

Makhiel
  • 3,874
  • 1
  • 15
  • 21
  • Thanks for your feedback, Makhiel. Sorry, the ajaxSingle is because I was testing a4j:commandLink, and just copying the wrong code. I've updated. – Rogério Arantes Jun 23 '16 at 11:16
  • You can better explain the use of @Action? Which method should I use it: setLocalidade( ) or carregarColecoes( )? When trying to insert, Eclipse tried to import javax.xml.ws.Action. This is not, right? What is the import should I do? – Rogério Arantes Jun 23 '16 at 11:27
  • "@action" means "attribute named action", not a Java annotation. ;) – Makhiel Jun 23 '16 at 13:56
0

With the suggestion of Makhiel and this link I decided.

The xhtml looked like this:

    <a4j:commandLink id="editlink" ajaxSingle="true"
        oncomplete="#{rich:component('modalEditarLocalidade')}.show()"
        action="#{localidadeMB.carregarColecoes}" 
        render="modalEditarLocalidade">
        <h:graphicImage value="/img/edit.png"/>
        <f:setPropertyActionListener value="#{loc}"
            target="#{localidadeMB.localidade}" />
    </a4j:commandLink>

and ManagedBean like this:

    public String carregarColecoes(){
        (....)
    }
Community
  • 1
  • 1
Rogério Arantes
  • 712
  • 1
  • 8
  • 29