有时,当使用<h:commandLink>, <h:commandButton>或<f:ajax>时,与标记相关的action, actionListener或listener方法根本没有被调用。或者,bean属性没有更新提交的UIInput值。

可能的原因和解决方法是什么?


当前回答

我想再提一点关于Primefaces的p:commandButton!

当你使用p:commandButton来执行需要在服务器上执行的操作时,你不能使用type="button",因为这是用于Push按钮的,用于执行自定义javascript,而不会向服务器发出ajax/非ajax请求。

为此,您可以免除type属性(默认值是"submit"),也可以显式地使用type="submit"。

希望这能帮助到一些人!

其他回答

虽然我的答案不是100%适用,但大多数搜索引擎都会把它作为第一个搜索结果,但我还是决定把它贴出来:

如果您正在使用PrimeFaces(或一些类似的API) p:commandButton或p:commandLink,那么您可能忘记显式地将process="@this"添加到命令组件中。

正如PrimeFaces用户指南在3.18节中所述,process和update的默认值都是@form,这与普通JSF f:ajax或RichFaces的默认值截然相反,后者分别是execute="@this"和render="@none"。

只是我花了很长时间才发现。(…而且我认为使用与JSF不同的默认值是相当不聪明的!)

我自己也遇到了这个问题,并发现了这个问题的另一个原因。 如果在支持bean中没有用于*.xhtml中使用的属性的setter方法,则不会调用该操作。

<ui:composition>  
  <h:form id="form1">
    <p:dialog id="dialog1">
      <p:commandButton value="Save" action="#{bean.method1}" /> <!--Working-->
    </p:dialog>
  </h:form>

  <h:form id="form2">
    <p:dialog id="dialog2">
      <p:commandButton value="Save" action="#{bean.method2}" /> <!--Not Working-->
    </p:dialog>
  </h:form>
</ui:composition>

来解决;

<ui:composition>  
  <h:form id="form1">
    <p:dialog id="dialog1">
      <p:commandButton value="Save" action="#{bean.method1}" />   <!-- Working  -->
    </p:dialog>

    <p:dialog id="dialog2">
      <p:commandButton value="Save" action="#{bean.method2}" />   <!--Working  -->
    </p:dialog>
  </h:form>
  <h:form id="form2">
    <!-- ..........  -->
  </h:form>
</ui:composition>

我修复了我的问题,放置:

<h:commandButton class="btn btn-danger" value = "Remove" action="#{deleteEmployeeBean.delete}"></h:commandButton>

In:

<h:form>
     <h:commandButton class="btn btn-danger" value = "Remove" action="#{deleteEmployeeBean.delete}"></h:commandButton>
</h:form>

如果你的h:commandLink在h:dataTable中,还有另一个原因,为什么h:commandLink可能不起作用:

绑定到h:dataTable的底层数据源也必须在单击链接时触发的第二个JSF-Lifecycle中可用。

因此,如果底层数据源是请求作用域,h:commandLink将不起作用!