简体   繁体   中英

PrimeFaces command button does not update form on submit

This is my Sample backing bean.

@ManagedBean
@SessionScoped
public class Sample {

    private String dateText;

    public Sample(){
        dateText = (new Date()).toString();
    }

    public String updateDate(){
        setDateText((new Date()).toString());
        return null;
   }

    public String getDateText() {
        return dateText;
    }

    public void setDateText(String dateText) {
       this.dateText = dateText;
   }

}

Scenario 01

<h:head></h:head>
<h:body>
    <h:form>
       <h:inputText id="txtFirst" value="#{sample.dateText}"/>
       <h:inputText id="txtSecond" value="#{sample.dateText}"/>
       <h:commandButton action="#{sample.updateDate}"/>
    </h:form>
</h:body>

Scenario 02

<h:head></h:head>
<h:body>
    <h:form>
       <h:inputText id="txtFirst" value="#{sample.dateText}"/>
       <p:inputText id="txtSecond" value="#{sample.dateText}"/>
       <h:commandButton action="#{sample.updateDate}"/>
    </h:form>
</h:body>

Scenario 03

<h:head></h:head>
<h:body>
    <h:form>
       <h:inputText id="txtFirst" value="#{sample.dateText}"/>
       <p:inputText id="txtSecond" value="#{sample.dateText}"/>
       <p:commandButton action="#{sample.updateDate}"/>
    </h:form>
</h:body>

Scenario 04

<h:head></h:head>
<h:body>
    <h:form>
       <h:inputText id="txtFirst" value="#{sample.dateText}"/>
       <p:inputText id="txtSecond" value="#{sample.dateText}"/>
       <p:commandButton action="#{sample.updateDate}" update="@form"/>
    </h:form>
</h:body>

When I click on the commandButton ,

Scenario 01 : Works fine. I can see new date values from both inputText fields without manually refreshing the page.

Scenario 02 : It refreshes the page. Cannot see any ajax behavior. New values are there.

Scenario 03 : Nothing happens. No refreshes. No value changes. To see the values I have to refresh the page manually.

Scenario 04 : Works like Scenario 01

Please someone give me a little explanation about what is happening here with these components. I have used ICEfaces , but I haven't see anything like this.

Thank you!

IceFaces are able to determine which components should be refreshed when an ajax action is invoked. With standard JSF + Primefaces you don't get such an automation so you have to specify the executed and updated components yourself.

  • I'm ignoring scenarion 1 as the code you provided is the same as in scenario 2
  • Scenario 2 has no ajax at all, so the whole page get submitted
  • Scenario 3 uses default ajax behaviour of p:commandButton which onlt executes the action but does not render other components - thus you only see the changes after a refresh
  • Scenario 4 executes the action using ajax and rendres the entire form - so the changes are visible after the processing is finished

So the behaviour isn't actually "strange" :)

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