简体   繁体   中英

update from p:commandButton doesn't work

I search for someone who can help me out with this "small" problem, I searched for hours. The code below is "broken down", so the real application is huge, I can't move technology at the moment.

So I have primefaces 5.0.10 and MyFaces 2.0.3 and I have to deal with that.

Here's an 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>

And here's my 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;
    }

}

What I would like to to: Update the outputText on pressing the commandButton. QUIET SO SIMPLE!

In the real application the use-case is different, but for start figuring out why it doesn't work in real application, I decided to break it down to this simple think and then make it (step by step) more like the real application.

Whatever: This doesn't work. The outputText just refresh after page-load. I tryed a lot till now, nothing worked. Just switching from MyFaces to original JSF, but that is (as I wrote) not possible for me at the moment.

Hopefully someone can help me out here.

update 1

Here's the Ouput of console:

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

Very interessting: After pressing the commandButton once, the Beans will not be called again...

Also I modifyed my bean, it now starts with

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

AND also I modifyed my "update" to use the whole id:

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

All that doesn't change a thing :/

FireFox throws one JS error on pressing the button:

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

Thanks for any reply

update 2 OK breaking it down to this XHTML, it work:

<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>

Now I can move forward step by step. Thanks for all :)

Have you tried to remove @ManagedBean annotation and import javax.enterprise.context.SessionScoped instead of javax.faces.bean.SessionScoped?

It seems to me that at this moment you are mixing CDI beans with Backing beans. Details and differences are provided at this link

I tried to reproduce your Problem:

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");
    }
}

View

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

But in my case it is working :/

  • Don't you get any javascript or java errors?
  • Did you try to use the absolute id to update the component?

You can find the absolute id if you inspect the page with your Browser:

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

In this case the absolute id would be :form:testId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM