簡體   English   中英

從p:commandButton更新不起作用

[英]update from p:commandButton doesn't work

我搜尋了可以幫助我解決這個“小”問題的人,我搜尋了幾個小時。 下面的代碼“已分解”,因此實際應用程序非常龐大,目前我無法轉移技術。

所以我有素數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之后,將不會再次調用Beans。

我也修改了我的bean,它現在開始於

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

並且我還修改了“更新”以使用整個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可以將其分解為這個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批注並導入javax.enterprise.context.SessionScoped而不是javax.faces.bean.SessionScoped?

在我看來,此刻您正在將CDI bean與Backing bean混合在一起。 此鏈接提供詳細信息和不同之

我試圖重現您的問題:

豆角,扁豆

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

但就我而言,它正在工作:/

  • 您沒有收到任何JavaScript或Java錯誤?
  • 您是否嘗試使用絕對ID來更新組件?

如果使用瀏覽器檢查頁面,則可以找到絕對ID:

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

在這種情況下,絕對ID為:form:testId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM