PrimeFaces actionListener没有调用bean

时间:2018-03-22 19:46:23

标签: jsf primefaces

我想模仿PrimeFaces Dialog example。由于某些我无法找到的原因,我的PrimeFaces按钮似乎没有调用所需的托管bean方法:

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

管理bean:

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

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

从不执行print语句,就像监听器不工作一样。单击按钮时,没有给出响应。没有后端错误,没有JS错误,没有。我只看到某些请求已完成,因为我在用户通过授权层时进行了记录。所以发生了一些事情,但似乎无声地失败了。

我尝试过:

  • 将按钮移至页面中的其他位置
  • 使用ID:

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

  • 在方法中需要javax.faces.event.ActionEvent

    public void createDialog(ActionEvent event) {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
    
  • 更改方法签名

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

    public void createDialog(int s) {
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
    
  • 我甚至试图创建一个WEB-INF/faces-config.xml(根据PrimeFaces documentation我不愿意,而且我不需要它):

      <?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>
    

我检查过的其他答案有:123456

显然在PrimeFaces is not as correct as it should be中使用actionListener ...有没有其他方法可以使用PrimeFaces组件?

2 个答案:

答案 0 :(得分:0)

此对话框架代码适用于我:

<强>的index.xhtml

map

<强> myDialogPage.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>

注意:示例显示如何从对话框传递参数。您还可以注意到,第一个按钮刚刚返回,而第二个按钮首先进行验证。我认为这些事情很有用。

<强> myBean.java

<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"/>

<强> WEB-INF / faces-config.xml中

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();
    }
}

答案 1 :(得分:0)

最后我看到了一些错误:

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

启用错误消息显示的技巧是在faces-config.xml中添加以下内容:

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

检查this后,我意识到我的beans.xml文件已被删除(??)