带有ajax的Commandlink仅在第二次单击时有效 [英] Commandlink with ajax works only on second click

查看:102
本文介绍了带有ajax的Commandlink仅在第二次单击时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的只是一个简单的注销功能.主页将称为模板页面;由标题和内容组成.标头包含注销按钮.页面中将有2种形式,一种是标题(注销按钮),一种是内容(少量按钮,链接等).

What I'm trying to do is just a simple logout function. The main page will call a template page; consist of header and content. The header contains the logout button. There will be 2 forms in the page, one in the header (logout button), one in the content (few buttons,links, etc).

我的问题是,第一次单击注销链接时页面刷新,而仅在第二次单击后才继续.当我在内容表单中包含撇号或Ajax代码时,该问题开始出现.当我删除该特定代码时,注销将按预期进行.

My problem is the page refresh when first click on the logout link and only proceed after second click. 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.

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

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

后备豆:

public String customLogout(ActionEvent e) {
    return "login.xhtml";
}

仅供参考:customLogout方法也包含会话销毁,但我只是将页面重定向放在此处.

FYI: the customLogout method also consist of session destroy, but I just put page redirect here.

在模板客户端中,模板指定为:

In template client, the template is specified as:

<ui:composition template="/templateUser.xhtml">

至于JSF库,当前使用jsf 2.0

As for the JSF library, currently use jsf 2.0

我尝试过的解决方案:

  • immediate="true"
  • id用于表单和commandLink(模板表单和内容表单)
  • 使用质数符号(<p:commandLink>带有ajax false ==>,此内容将返回到上一页;支持的bean中的方法未运行.
  • immediate="true"
  • put id for both form and commandLink (template form & content form)
  • use primefaces (<p:commandLink> with ajax false ==> this returb to the previous page; the method in backed bean is not running.

以下是我在此处尝试过的一些链接:

Below are few links I tried here:

  • 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?

推荐答案

当我在内容表单中包含撇号或Ajax代码时,问题开始出现.当我删除该特定代码时,注销将按预期进行.

您的问题是由第7点引起的,如 commandButton/commandLink/ajax action/未调用侦听器方法或输入值未更新.当您使用<f:ajax>发出ajax请求并执行还涵盖所有其他形式的更新时,所有其他形式都将失去其视图状态.这将导致第一个提交始终不执行任何操作",但是下一个提交将起作用,这与您的问题症状完全匹配.

Your problem is caused by point 7 as described in commandButton/commandLink/ajax action/listener method not invoked or input value not updated. When you fire an ajax request using <f:ajax> and performs an update which also covers all other forms, then all those other forms will lose their view state. This causes that the 1st submit will always "do nothing", but the next submits will work, which matches exactly your problem symptoms.

您可以通过以下方式之一解决此问题:

You can solve this problem in one of the following ways:

  1. <f:ajax render>中明确指定所有其他形式的客户端ID.您可以指定多个客户端ID,以空格分隔.例如

  1. Explicitly specify the client ID of all other forms in the <f:ajax render>. You can specify multiple client IDs space separated. E.g.

<f:ajax ... render="someComponent otherComponent :loginForm" />

  • 使用此答案中建议的基于JavaScript的修复程序: h:commandButton/h:commandLink不适用于第一次点击,仅适用于第二次点击.

    这篇关于带有ajax的Commandlink仅在第二次单击时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆