Redirecting to an external URL in a new tab and performing an action in backing bean at the same time

纵饮孤独 提交于 2019-12-04 14:57:22

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.

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>

And

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

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.

why don't you use action="someaction" and map it in faces-config.xml to go to another page?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!