【问题标题】:p:remoteCommand does not invoke the declared action methodp:remoteCommand 不调用声明的操作方法
【发布时间】:2020-03-05 05:07:34
【问题描述】:

网页:

<html xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui">

<h:body>

    <ui:composition template="../../Template/template.xhtml">

        <ui:define name="content">

            <h:form>

                <h:outputText id="montest" value="#{ProviderLogin.i}"/>

                <button class="ZWButtonActionIntervention pfButtonWhite" type="button" onclick="testpageRC()"/>
                    <span>TESTING</span>
                </button>

                <p:remoteCommand name="testpageRC" process="@this" update="montest" action="#{ProviderLogin.TESTING()}"/>

                <p:commandButton styleClass="ZWButtonActionIntervention pfButtonOrange" value="#{GestionIntervention.m_typeNonTraitee}" action="#{ProviderLogin.TESTING()}"/>

            </h:form>

        </ui:define>    
    </ui:composition>

</h:body>

我的 java 类(Bean)

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@SessionScoped
@ManagedBean(name="ProviderLogin")
public class ProviderLogin implements Serializable
{ 
    private int i;

    public int getI(){return i;}

    public void TESTING(){i++;}
}

我在函数“TESTING()”中有一个断点

当我按下 'p:commandButton' 时,到达断点

当我按下“按钮”(调用p:remoteCommand)时,没有到达断点

真正奇怪的是,'p:remoteCommand' 适用于此:update="montest" 但是,bean 中的方法没有被触发。

顺便说一句,当我开始写 #{... 时,我可以访问我的 bean(变量和方法)

我使用 Primefaces 6.2

【问题讨论】:

  • &lt;button&gt; 不应该是 primefaces 的一部分。试试&lt;p:button&gt;
  • @XtremeBaumer 我试过了,但是当页面加载(onclick)时,一个 p:button 正在触发。这就是为什么我要使用 RemoteCommand 来调用 bean 方法。我使用一个简单的按钮元素,因为我需要一些特定的自定义
  • 目前,问题更多的是“为什么我的 bean 方法没有调用 p:remoteCommand”,因为“update”属性有效,为什么 action 属性无效

标签: jsf primefaces remotecommand


【解决方案1】:

发现问题。

每个网页都定义了一个模板的内容。 在此模板中,“内容”部分包含 2 &lt;h:form&gt;。 一种是用户未登录时的渲染器。 另一个是在用户正确登录时呈现的。

在重新定义内容的每个页面中(当用户登录时),我都有第二部分 &lt;h:form&gt;。似乎&lt;p:remoteCommand&gt; 中的action 不能很好地管理这一点,而&lt;p:button&gt; 可以。我刚刚删除了每个页面上的那个表格,我的问题就解决了。

我的模板(内容部分)

<div id="content" style="padding: 10px">
    <h:form rendered="#{!ProviderLogin.m_User.m_bLogged}">
        <h:button value="Connexion" outcome="/Gestion/Connexion.xhtml"/>
    </h:form>
    <h:form id="formContent" rendered="#{ProviderLogin.m_User.m_bLogged}">
        <ui:insert name="content" >
            <ui:include src="content.xhtml" />
        </ui:insert>
    </h:form>
</div>

一个页面重新定义了内容部分(第二个表单给了我错误)

<h:body>
    <ui:composition template="../Template/template.xhtml">
        <ui:define name="content">
            <h:form id="formInterventions">
                PAGE CONTENT
            </h:form>
        </ui:define>    
    </ui:composition>
</h:body>

重新定义内容部分的同一页面(没有第二种形式) 然后,我的action 中的&lt;p:remoteCommand&gt; 工作!

 <h:body>
     <ui:composition template="../Template/template.xhtml">
         <ui:define name="content">
             PAGE CONTENT
         </ui:define>   
     </ui:composition>
 </h:body>

我也给你这些信息:

我使用update 的一些控件,例如这个展示:

https://www.primefaces.org/showcase/ui/ajax/poll.xhtml

你需要把你想控制的idupdate

当您使用&lt;h:form&gt; 时,有时您还需要像这样指定formid

update="formID:controlID"

由于在我的模板中定义了&lt;h:form&gt;(我忘记了),它没有工作,因为我根本没想到,我没有给那个表单一个 ID,所以基本上,一个默认 ID (j_idt27) 已给出。

这意味着我的update 功能找不到指定的控件ID

【讨论】:

  • 下次根据stackoverflow.com/tags/jsf/info 中的说明发布minimal reproducible example,我们可以在一分钟内指出这种基于模板的错误,或者您可能已经发现全部由你自己;)
  • @BalusC 这更好吗? :)
  • 我的意思是,在原来的问题中 :) 你的问题根本不是minimal reproducible example,没有人知道&lt;ui:composition template="../../Template/template.xhtml"&gt; 后面的文件是什么样的,所以它不能回答很长时间。此外,stackoverflow.com/tags/jsf/info 链接清楚地说明了尽可能摆脱模板并将所有内容放在一个文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2015-05-06
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2015-07-25
相关资源
最近更新 更多