这个问题在这里已经有了答案:





h:commandButton/h:commandLink does not work on first click, works only on second click

(2 个回答)


5年前关闭。




我想要做的只是一个简单的注销功能。主页面会调用一个模板页面;由标题和内容组成。标题包含注销按钮。页面中将有 2 个表单,一个在标题中(注销按钮),一个在内容中(几个按钮、链接等)。

我的问题是第一次点击注销链接时页面刷新,第二次点击后才继续。当我在内容表单中包含 primefaces 或 ajax 代码时,问题开始出现。当我删除该特定代码时,注销会按预期工作。

主模板中的注销表单 templateUser.xhtml :

<h:form id="logout"> 
    <h:commandLink action="#{tenantController.customLogout(e)}" 
        id="logoutBtn" immediate="true" value="Logout" /> 
</h:form> 

支持 bean :
public String customLogout(ActionEvent e) { 
    return "login.xhtml"; 
} 

仅供引用:customLogout 方法也包括 session 销毁,但我只是将页面重定向放在这里。

在模板客户端中,模板被指定为:
<ui:composition template="/templateUser.xhtml"> 

至于JSF库,目前使用jsf 2.0

我尝试过的解决方案:
  • immediate="true"
  • id用于表单和命令链接(模板表单和内容表单)
  • 使用primefaces ( <p:commandLink> with ajax false ==> this returb to the previous page; the method in backed bean is not running.

  • 以下是我在这里尝试的几个链接:
  • commandButton/commandLink/ajax action/listener method not invoked or input value not updated
  • How can I prevent page refresh on click of <h:commandLInk>
  • JSF PrimeFaces p:commandLink won't redirect to new page?
  • 请您参考如下方法:

    The problem starts to appear when I include primefaces or ajax code inside the content form. When I remove that particular code, the logout works as intended.



    您的问题是由第 7 点引起的,如 commandButton/commandLink/ajax action/listener method not invoked or input value not updated 中所述.当您使用 <f:ajax> 触发 ajax 请求时并执行也涵盖所有其他表单的更新,然后所有其他表单将丢失其 View 状态。这导致第一次提交将始终“什么都不做”,但下一次提交将起作用,这与您的问题症状完全匹配。

    您可以通过以下方式之一解决此问题:
  • <f:ajax render> 中明确指定所有其他表单的客户端 ID .您可以指定多个客户端 ID 以空格分隔。例如。
    <f:ajax ... render="someComponent otherComponent :loginForm" /> 
    
  • 使用此答案中建议的基于 JavaScript 的修复程序:h:commandButton/h:commandLink does not work on first click, works only on second click .

  • 评论关闭
    IT干货网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!