重定向到新选项卡中的外部URL并同时在支持Bean中执行操作 [英] Redirecting to an external URL in a new tab and performing an action in backing bean at the same time

查看:115
本文介绍了重定向到新选项卡中的外部URL并同时在支持Bean中执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsf应用程序,该应用程序有时需要在新标签页中打开外部页面,使第一页保持活动状态.我需要找到一种方法来使应用程序执行,只需单击一个按钮:

I'm working at a jsf application that at a certain time need to open an external page in a new tab, leaving the first page active. I need to find a way to make the application perform, in a single button click:

  1. 在新标签页中重定向到外部URL
  2. 在原始页面上禁用按钮本身的操作

我尝试使用<outputLink />,但是它没有动作属性. 我尝试使用<commandLink />,但是无法重定向到外部. 我还尝试了<commandLink />target="_blank"以及在后备bean中编码的重定向:

I've tried using an <outputLink /> but it has no action attribute. I've tried using a <commandLink />but it's unable to redirect outside. I've also tried a <commandLink /> with target="_blank" and a redirection coded in the backing bean:

<h:commandLink onclick="submit();" target="_blank" action="#{myBean.execute}" disabled="#{myBean.linkDisabled}" value="external link" />

,然后在支持bean中:

and, in the backing bean:

public String execute() {
    linkDisabled = true;

    try{
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.redirect(Constants.EXTERNAL_URL);
    } catch(Exception e){
        throw new FacesException("Redirection failed");
    }

    return THIS_PAGE;
}

在新标签页中打开了一个页面,但它是当前页面,而不是URL Constants.EXTERNAL_URL的页面,并且该按钮仍处于启用状态.没有错误消息显示.有什么建议吗?

A page is opened in a new tab but it's the current page instead of the that with URL Constants.EXTERNAL_URLand the button is still enabled. No error message is shown. Any suggestion?

提前感谢,安德里亚

推荐答案

您的问题令人困惑.您正在混合链接"和按钮".您基本上需要通过JavaScript在客户端禁用该元素,因为响应不会返回到当前浏览器窗口/选项卡,而是返回到另一个浏览器窗口/选项卡.在JavaScript中,禁用按钮很容易,但不能禁用链接.为简单起见,我假设您的意思是可以使用<h:commandButton>,并且这是当前表单中的唯一按钮.

Your question is confusing. You're mixing "link" and "button". You basically need to disable the element in the client side by JavaScript because the response does not return to the current browser window/tab, but to a different browser window/tab. In JavaScript, it's easy to disable a button, but not a link. For simplicity, I'll assume that you mean and can use a <h:commandButton> and that this is the only button in the current form.

要解决您的问题,您需要删除onclick="submit()"(因为这会干扰默认行为),并且您需要删除导航结果(这无关紧要,但是您在某些JSF实施中并不知道),并且需要在客户端禁用JS按钮.

To solve your problem, you need to remove onclick="submit()" (because it's disturbing the default behaviour) and you need to remove the navigation outcome (it should not matter, but you never know in some JSF impls) and you need to disable the button by JS in the client side.

所有代码都应如下所示:

All with all your code should look just like this:

<h:form target="_blank">
    <h:commandButton
        value="external" 
        action="#{myBean.execute}" 
        onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);" />
</h:form>

还有

public void execute() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect(Constants.EXTERNAL_URL);
}

setTimeout()是必需的,因为否则在发送表单数据之前禁用了按钮,这将导致按钮自身的name = value对不会作为请求参数发送,因此JSF不会调用任何操作.

The setTimeout() is necessary because the button is otherwise disabled before the form data is sent which would cause that the button's own name=value pair won't be sent as request parameter and so JSF won't invoke any action.

这篇关于重定向到新选项卡中的外部URL并同时在支持Bean中执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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