【问题标题】:Primefaces p:ajax and remoteCommand does not update growl messagesPrimefaces p:ajax 和 remoteCommand 不更新咆哮消息
【发布时间】:2016-07-22 23:22:13
【问题描述】:

在以下示例中,我使用 p:ajax 修改数据表,然后使用 remoteCommand 更新表。这样可行。但是,如果发生错误或成功,我也想更新咆哮消息。这行不通。

我从示例中看到如果一个按钮调用一个动作,然后调用 remoteCommand,咆哮消息更新。但是,这不会给我从数据表中的 ajax cellEdit 中需要的东西。

如何从 p:ajax 命令更新咆哮消息。

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

<h:head/>

<h:form id="formId">

    <p:growl id="msgs" showDetail="true" />
    <h:body>
        <h:panelGrid>
            <!-- Updates wordsId table but not msgs -->
            <p:remoteCommand name="onCellEditRemote" update="wordsId msgs"/>

            <p:dataTable id="wordsId" var="word" value="#{remoteMessageBean.words}" editable="true" editMode="cell">
                <!-- does not update msgs -->
                <p:ajax event="cellEdit" listener="#{remoteMessageBean.modifyWordOnCellEdit}"  oncomplete="onCellEditRemote()" update="formId:msgs"/>

                <p:column headerText="Modify" >
                    <p:outputLabel value="#{word}" />
                </p:column>
                <p:column headerText="Modify" >
                    <p:cellEditor>
                        <f:facet name="output"><h:outputText value=""/></f:facet>
                        <f:facet name="input">
                            <p:inputText id="modelInput" value="#{remoteMessageBean.modifyWord}"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>
            </p:dataTable>
        </h:panelGrid>

        <p:remoteCommand name="rc" update="msgs" />
        <p:commandButton type="button" action="#{remoteMessageBean.buttonAction}" value="Doesnt Work" icon="ui-icon-refresh" update="msgs" />

        <p:remoteCommand name="rc2" update="msgs" action="#{remoteMessageBean.buttonAction}" />
        <p:commandButton type="button" onclick="rc2()" value="Works" icon="ui-icon-refresh" />


    </h:body>
</h:form>
</html>



@ManagedBean
@SessionScoped
public class RemoteMessageBean {
    private static Logger logger = Logger.getLogger(RemoteMessageBean.class);
    private String modifyWord;
    private List<String> words;

    public RemoteMessageBean() {
        words = new ArrayList<>();
        words.add("word1");
        words.add("word2");
        words.add("word3");
    }

    public void modifyWordOnCellEdit(CellEditEvent event) {
        logger.debug(event);
        getWords().add(getModifyWord());
        logger.debug("");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "adding " + getModifyWord(), null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        setModifyWord(null);
    }

    public void buttonAction() {
        logger.debug("");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "buttonAction", null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
      }

    public String getModifyWord() {
        return modifyWord;
    }

    public void setModifyWord(String modifyWord) {
        this.modifyWord = modifyWord;
    }

    public List<String> getWords() {
        return words;
    }

}

【问题讨论】:

  • 您的onCellEditRemote 远程命令不会调用任何东西,因此很可能不会更新任何东西。检查网络流量

标签: primefaces growl


【解决方案1】:

我通过在 modifyWordOnCellEdit 中创建和保存消息然后从 remoteCommand 调用 displayMessageAction 来显示它来解决这个问题。不完全是我所希望的,但它确实有效。

Jsf 页面

 <p:remoteCommand name="onCellEditRemote" update="wordsId" action="#{gameBean.displayMessageAction}"/>

控制器

   public void displayMessageAction() {
        if (getMessage() != null) {
            FacesContext.getCurrentInstance().addMessage("growl", getMessage());
            setMessage(null);
        }
    }

    public void modifyWordOnCellEdit(CellEditEvent event) {
    ...
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, exception.getMessage(), null);
        setMessage(msg);
    }

【讨论】:

    【解决方案2】:

    它可以在 p:ajax 中添加部分提交

    【讨论】:

    • 错误的纯空格(无逗号)被允许在纯 jsf 中使用,即使是唯一有效的值
    • 试试这个:
    • partialSubmit 是一个布尔值 true/false 不是 js 函数
    • 又错了......请不要从根本上改变你原来的答案。那么所有的 cmets 都脱离了上下文。然后创建一个新的答案。并请了解您为其创建答案的技术
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多