PrimeFaces命令按钮不会在提交时更新表单

PrimeFaces命令按钮不会在提交时更新表单

问题描述:

这是我的Sample backing bean.

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

}

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

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

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

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

当我点击commandButton时,

方案01 :工作正常.我可以从两个inputText字段中看到新的日期值,而无需手动刷新页面.

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

场景02 :刷新页面.无法看到任何ajax行为.有新值.

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

场景03 :没有任何反应.没有刷新.值没有变化.要查看值,我必须手动刷新页面.

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

方案04 :类似于方案01

请有人给我一些解释,说明这些组件在这里发生了什么. 我使用过ICEfaces,但没有看到类似的内容.

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.

谢谢!

IceFaces能够确定在调用ajax操作时应刷新哪些组件.使用标准的JSF + Primefaces,您不会获得这样的自动化,因此您必须自己指定执行和更新的组件.

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.

  • 我忽略了方案1,因为您提供的代码与方案2中的相同
  • 方案2完全没有ajax,因此整个页面都可以提交
  • 方案3使用默认的ajax行为p:commandButton,该行为可以执行操作但不呈现其他组件-因此,您仅在刷新后才能看到更改
  • 方案4使用ajax执行动作并重新呈现整个表单-因此更改在处理完成后可见
  • 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" :)