2013-06-12 25 views
2

我读了很多关于f:ajax与其他jsf或html标签一起工作但看起来不够用的!从未在h:commandButton(使用f:ajax)时触发JSF动作

我有一个commandButton,在我的页面中有一个(必要的)f:ajax标记,并且这个动作从不被触发。这是代码。

<html> 
    <h:head> 
    </h:head> 

    <h:body> 
    <h:form> 
     <h:commandButton action="#{bean.myFirstFunction}" value="Submit"> 
     <f:ajax render=":wrap" /> 
     </h:commandButton> 
    </h:form> 

    <h:panelGroup id="wrap"> 
     <h:panelGroup id="content" rendered="#{bean.myVariable != null}"> 

     <h:form> 
     <h:commandButton action="#{bean.mySecondFunction}" value="Submit"> 
      <f:ajax render=":wrap" /> 
     </h:commandButton> 
     </h:form> 

     </h:panelGroup> 
    </panelGroup> 
    <h:body> 
</html> 

所以像上面我有两个按钮,第一个出来的包装作品,它必须显示其在开始时隐藏在包裹面板。第二个是包装,必须启动一个完全不同的行动。 (包裹必须保持显示)

这是豆。

@ManagedBean(name = "bean") 
@ViewScoped 
public class myBean { 
    private Object myVariable; 

    public void setMyVariable(Object o) { ... } 
    public Object getMyVariable() { ... } 

    @PostConstruct 
    public void init() { 
    this.myVariable = null; 
    } 

    public void myFirstFonction() { 
    this.myVariable = new Object(); 
    } 

    public void mySecondAction() { 
    System.out.println("here"); 
    } 
} 

当我启动时,我看到第一个按钮。我点击它,第二个出现。 (所以第一个函数被调用,对象被实例化并且与ajax标签一起呈现包装 但是,当单击第二个按钮时,没有任何反应。

我已经写了我的代码的一部分,我希望这个错误是在这里。

我认为这是对面板组的渲染选项,但我不能准确地找到它。

感谢

回答

0

我决定做一些简单。事实上,我不在我的页面中确实不需要Ajax,因为我的bean在ViewScope中,所以页面可以重新加载,它不是重新加载一个问题。所以我摆脱了我的f:ajax标签,一切都是正确的。

3

根据commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated的第9条,你是JSF spec issue 790的受害者。当你ajax更新一些包含一个表单的内容时,它会失去它的视图状态。作为一个快速的解决方法,只需给待更新的表单一个ID,并在<f:ajax render>中明确引用它即可。

<h:body> 
    <h:form> 
     <h:commandButton action="#{bean.myFirstFunction}" value="Submit"> 
     <f:ajax render=":wrap :wrapForm" /> 
     </h:commandButton> 
    </h:form> 

    <h:panelGroup id="wrap"> 
     <h:panelGroup id="content" rendered="#{bean.myVariable != null}"> 

     <h:form id="wrapForm"> 
     <h:commandButton action="#{bean.mySecondFunction}" value="Submit"> 
      <f:ajax render=":wrap" /> 
     </h:commandButton> 
     </h:form> 

     </h:panelGroup> 
    </panelGroup> 
    <h:body> 
+0

这是否适用于RequestScope托管bean? –

+0

点是9 https://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#212018 – Saljack

+0

@Saljack:是的,列表自从不断更新。 – BalusC

相关问题