0

I am trying to mimic the PrimeFaces Dialog example. For some reason that I am not able to find, my PrimeFaces button does not seem to call the required managed bean method:

            <h:form>
                <p:commandButton value="Open" icon="ui-icon-extlink" actionListener="#{myController.createDialog()}" />
            </h:form>

Managed bean:

@Named(value = "myController")
@ViewScoped
public class MyController implements Serializable {

    public void createDialog() {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
...

The print statement is never executed, like if the Listener was not working. When I click the button, no response is given. No backend error, no JS error, nothing. I only see that some request is done because I log when a user passes the authorization layer. So something happens but seems to fail silently.

What I have tried:

  • Move the button to other places in the page
  • Use an id:

    <p:commandButton id="ex" value="Open" icon="ui-icon-extlink" actionListener="#{myController.createDialog()}" />
    <h:message for="ex" />
    
  • Remove the ViewScoped

  • Require a javax.faces.event.ActionEvent in the method

    public void createDialog(ActionEvent event) {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
    
  • Change the method signature

    action="#{myController.createDialog(5)}"
    

    and

    public void createDialog(int s) {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
    
  • I even tried to create a WEB-INF/faces-config.xml (which I would prefer not to, and according to PrimeFaces documentation I do not need it) with:

      <?xml version='1.0' encoding='UTF-8'?>
       <faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    
              http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <action-listener>org.primefaces.application.DialogActionListener</action-listener>
        <navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
        <view-handler>org.primefaces.application.DialogViewHandler</view-handler>
    </application>
    
    <lifecycle>
        <phase-listener>org.primefaces.component.lifecycle.LifecyclePhaseListener</phase-listener>
    </lifecycle>
    

Other answers I have checked are: 1, 2, 3, 4, 5, 6


Apparently the use of actionListeners in PrimeFaces is not as correct as it should be... Is there another way to use PrimeFaces components?

user1156544
  • 1,532
  • 1
  • 17
  • 40
  • I'm not sure, but I always add an ActionEvent parameter to my ActionListeners... could it be the problem? – Oscar Pérez Mar 23 '18 at 10:16
  • 1
    I think this is not necessary in PrimeFaces... Anyway, I tried adding the javax.faces.event.ActionEvent and it doesn't work either – user1156544 Mar 23 '18 at 12:03

2 Answers2

0

This Dialog Framework code works for me:

index.xhtml

<p:commandButton id="openDialogButton" value="open dialog" action="#{myBean.openDialog('origin')}">
    <!-- dialogReturn event: data could be passed, see page 587 in PF 6.1 manual. -->
    <p:ajax event="dialogReturn" listener="#{myBean.doSthOnDialogReturn}"/>
</p:commandButton>

myDialogPage.xhtml

<p:commandButton id="closeButtonDialog" value="close dialog"
   action="#{myBean.closeDialog('false')}"/>

<p:commandButton id="closeButtonDialog2" value="close dialog 2"
    action="#{myBean.closeDialog('true')}"
    ajax="false" validateClient="true"/>

Note: Example shows how to pass parameter from dialog. You can also notice, that the first button does just return while the second one does validation at first. I think these things can be useful.

myBean.java

public void openDialog(String origin) {
    RequestContext.getCurrentInstance().openDialog("myDialogPage",
    options, null);
}

public void closeDialog(Boolean param) {
    RequestContext.getCurrentInstance().closeDialog(param);
}

public void doSthOnDialogReturn(SelectEvent event) {
    if ((Boolean) event.getObject()) { // retrieve param value
        doSth();
    }
}

WEB-INF/faces-config.xml

<application>
    <action-listener>
        org.primefaces.application.DialogActionListener
    </action-listener>

    <navigation-handler>
        org.primefaces.application.DialogNavigationHandler
    </navigation-handler>

    <view-handler>
        org.primefaces.application.DialogViewHandler
    </view-handler>
</application>
Jan Novy
  • 11
  • 1
  • 1
  • 6
  • For some reason this doesn't work for me. My `faces-config.xml` is the same as yours. I placed the `p:commandButton` in your index inside a `h:form`(I have tried outside too) and `myBean.openDialog()` is never called... There must be something else that is needed – user1156544 Mar 23 '18 at 12:11
  • If you used my example, there was a mistake, missing parameter so instead of `myBean.openDialog()` call `myBean.openDialog('origin')` is correct. Actually, I think you have noticed this...which version of PrimeFaces do you use? I use 6.1. – Jan Novy Mar 23 '18 at 15:54
  • I tried with String and with int as parameter (I edited before). It is 6,2, the lastest one. – user1156544 Mar 23 '18 at 16:17
0

Finally I got to see some error:

SEVERE - /page.xhtml @159,146 actionListener="#{myController.createDialog()}": Target Unreachable, identifier 'myController' resolved to null

The trick to enable the display of error messages was to add the following in faces-config.xml:

<factory>
    <exception-handler-factory>org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory</exception-handler-factory>
</factory>

After checking this, I realised that my beans.xml file had been somehow deleted (??)

user1156544
  • 1,532
  • 1
  • 17
  • 40