从 p:commandButton 更新无效

update from p:commandButton doesn't work

我在寻找可以帮助我解决这个 "small" 问题的人,我搜索了几个小时。下面的代码是"broken down",所以真正的应用是巨大的,我暂时无法移动技术。

所以我有 primefaces 5.0.10 和 MyFaces 2.0.3,我必须处理它。

这是一个 XHTML:

<ui:composition 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"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="../index.xhtml">
<ui:define name="title">Keyboard Bean Test</ui:define>

<ui:define name="content">
    <h1>Keyboard Bean Test</h1>

    <h:form id="myform">
        <p:panel header="KeyboardBean">
            <p:keyboard value="#{keyboardBean.value}"></p:keyboard>
        </p:panel>

        <h:outputText id="myOutput" value="#{keyboardBean.value}"></h:outputText> <br /><br />

        <p:commandButton value="Setzen" update="myOutput"></p:commandButton>
    </h:form>
</ui:define>

这是我的 KeyboardBean.java

package de.lippoliv.test.primefaces.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Named;

@Named("keyboardBean")
@ManagedBean
@SessionScoped
public class KeyboardBean {

    private String value;

    public String getValue () {
        System.out.println("KeyboardBean::reading value: " + value);

        return value;
    }

    public void setValue (String newValue) {
        System.out.println("KeyboardBean::write value: " + newValue);

        value = newValue;
    }

}

我想做的事:在按下 commandButton 时更新 outputText。安静如​​此简单!

在实际应用中,用例是不同的,但为了开始弄清楚为什么它在实际应用中不起作用,我决定将其分解为这个简单的想法,然后实现(一步一步)更像真正的应用程序。

随便:这行不通。 outputText 仅在页面加载后刷新。到目前为止,我尝试了很多,但没有任何效果。只是从 MyFaces 切换到原始的 JSF,但这(正如我所写的)目前对我来说是不可能的。

希望有人能帮助我。

更新 1

这是控制台的输出:

26.02.2016 11:58:58 org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
INFO: Creating instance of de.rac.oliverlippert.test.primefaces.beans.MenuBean
26.02.2016 11:58:58 org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
INFO: Creating instance of de.rac.oliverlippert.test.primefaces.beans.KeyboardBean
KeyboardBean::reading value: null
KeyboardBean::reading value: null
KeyboardBean::reading value: null
KeyboardBean::reading value: null
26.02.2016 11:59:01 org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
INFO: Creating instance of de.rac.oliverlippert.test.primefaces.beans.KeyboardBean
26.02.2016 11:59:01 javax.faces.validator._ExternalSpecifications isUnifiedELAvailable
INFO: MyFaces Unified EL support enabled
KeyboardBean::reading value: null
KeyboardBean::write value: sdf

非常有趣:按一次commandButton后,Bean不会再被调用...

我也修改了我的bean,它现在以

开头
@ManagedBean
@SessionScoped
public class KeyboardBean {
    ...
}

并且我还修改了我的 "update" 以使用整个 ID:

<p:commandButton value="Setzen" update=":myform:myOutput"></p:commandButton>

所有这一切都没有改变:/

FireFox 在按下按钮时抛出一个 JS 错误:

TypeError: f is null (javax.faces.resource/primefaces.js.xhtml?ln=primfaces&v=5.0 row 2 col 6868)

感谢您的回复

更新 2 OK 把它分解成这个 XHTML,它起作用了:

<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"
xmlns:ui="http://java.sun.com/jsf/facelets"
>

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Primefaces Test</title>
</h:head>

<h:body>
    <h1>Keyboard Bean Test</h1>

    <h:form id="myform">
        <h:panelGrid id="grid" cellpadding="5" columns="2" style="margin-bottom:10px">
            <p:panel header="KeyboardBean">
                <p:keyboard value="#{keyboardBean.value}"></p:keyboard>
            </p:panel>
            <h:outputText id="myOutput" styleClass="update" value="#{keyboardBean.value}"></h:outputText>

            <h:outputText id="myOutput2" styleClass="update" value="#{keyboardBean.value}"></h:outputText>
            <h:outputText id="myOutput3" styleClass="update" value="#{keyboardBean.value}"></h:outputText>
        </h:panelGrid>

        <p:commandButton value="Setzen" update="@(.update)"></p:commandButton>
    </h:form>
</h:body>

现在我可以一步步向前了。谢谢大家:)

我试图重现你的问题:

豆子

@ManagedBean
@ViewScoped
public class TestBean {

    private String testValue = "a";

    public void setTestValue(final String testValue) {
        this.testValue = testValue;
    }

    public String getTestValue() {
        return testValue;
    }

    public void testAction() {
        setTestValue("b");
    }
}

查看

<h:outputText id="testId" value="#{roleEditController.testValue}" />
<p:commandButton update="testId" action="#{roleEditController.testAction}" />

但在我的情况下它正在工作:/

  • 您没有遇到任何 java 脚本或 java 错误吗?
  • 您是否尝试使用绝对 ID 来更新组件?

如果您使用浏览器检查页面,您可以找到绝对 ID:

<span id="form:testId">a</span>

在这种情况下,绝对 ID 将是 :form:testId

您是否尝试删除 @ManagedBean 注释并导入 javax.enterprise.context.SessionScoped 而不是 javax.faces.bean.SessionScoped?

在我看来,此刻您正在将 CDI bean 与 Backing bean 混合在一起。提供了详细信息和差异 at this link