【问题标题】:commandButton is not executed if component was ajax updated如果组件是 ajax 更新,则不会执行 commandButton
【发布时间】:2013-07-30 09:56:00
【问题描述】:

当我设置 ajax="false" 时,下面的简化代码正在工作。使用 ajax="true" 时,第二个 commandButton 在由 Button1 或 Button2 更新后推送时不会调用 personPM.commitEditPerson()。

有人可以帮我看看这里有什么问题吗?因为好像不太好解决,所以我添加了可以轻松复现的整个代码(JSF 2.2,Primefaces 是 3.5,GlassFish 4.0):

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
            <h:form id="f1">                    
                <p:commandButton id="Button1" update=":neuePerson" value="Neue Person" action="#{testPM.setCurrentPerson()}"/>
            </h:form>

            <h:panelGroup id="neuePerson">
                <h:form id="f2" >
                    <p:commandButton id="Button2" update=":neuePerson" value="Übernehmen" action="#{testPM.commitEditPerson()}"/>
                </h:form>
            </h:panelGroup>
    </h:body>
</html>

Session Scoped Bean TestPM.java:

package at.feimas.administration.presentation;

import java.util.logging.Level;
import java.util.logging.Logger;

public class TestPM {

    private String currentPerson;
    private String firstName;

    private static final Logger logger = Logger.getLogger(TestPM.class.getName());

    public void setCurrentPerson() {
        logger.log(Level.INFO, "New Person");
    }    
    public void commitEditPerson(){
        logger.log(Level.INFO, "Edit Person");
    }    
}

【问题讨论】:

  • 这是您的实际代码吗?第二种形式的字段是否有任何验证器?检查 JavaScript 控制台以查看是否确实有请求发送到服务器。 BTW ajax=true 是默认值。
  • 不确定这是否能解决问题,但您在使用 action 时应该使用 actionListenerstackoverflow.com/questions/3909267/…
  • @user1983983 是的,这是实际代码。没有验证器。 JavaScript 控制台告诉我一个请求将发送到服务器。但是 personPM.commitEditPerson() 没有被调用(我已经实现了一条日志消息)。
  • @Lester 我试过了,但没有任何改变:没有调用 personPM.commitEditPerson()。

标签: ajax jsf primefaces


【解决方案1】:

这个问题是由版本不匹配引起的,PF 3.5 还没有为 JSF 2.2 做好准备!我使用了 PF 快照 4.0,它工作正常。

这段代码应该显示整个问题以及如何使用 PF 3.5 和 JSD 2.2 来解决它,但我强烈建议使用一起工作的版本!:

<h:body>
    <h:form id="f1">                    
        <p:commandButton id="Button1" update=":f2:buttonpanel2" value="New Person" actionListener="#{testPM.setFirstName}"/>
    </h:form>

    <h:panelGroup id="buttonpanel1">
        <h:form id="f2">      
            <h:panelGroup id="buttonpanel2">
                <h:outputText value="#{testPM.firstName}" id="firstName"/>                
                <p:commandButton id="Button2" update=":f2:buttonpanel2" value="Apply" actionListener="#{testPM.commitEditPerson}"/>
            </h:panelGroup>
        </h:form>          
    </h:panelGroup>
</h:body>

如果按钮使用 'update=":buttonpanel1"' 这会导致我在此处发布的问题中的行为:一旦更新发生,表格 f2 将不再起作用。如果按钮使用 'update=":f2:buttonpanel2"' 一切都按预期工作。

【讨论】:

  • 不幸的是,我对此没有任何解释。所有版本都适合我。 :/ 当我用 ":buttonpanel1" 替换 Button2update-attribute 值时,它仍然可以正常工作。
  • 好的,感谢您再次浏览。那么这将成为一个谜。我希望如果其他人遇到这个问题,它仍然会有所帮助。
  • 我有解决办法:肯定是版本不匹配。 PF 3.5 还没有为 JSF 2.2 做好准备。我现在使用的是 PF 4 Snapshot,它在更新表单时也可以使用。
猜你喜欢
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2016-01-16
  • 2012-12-09
相关资源
最近更新 更多